Using dplyr, how to pipe or chain to plot()?
Asked Answered
S

2

9

I am new to dplyr() package and trying to use it for my visualization assignment. I am able to pipe my data to ggplot() but unable to do that with plot(). I came across this post and the answers including the one in comments, didn't work for me.

Code 1:

emission <- mynei %>%
    select(Emissions, year) %>%
    group_by(year) %>%
    summarise (total=sum(Emissions))

emission %>%
    plot(year, total,.)

I get the following error:

Error in plot(year, total, emission) : object 'year' not found

Code 2:

mynei %>%
    select(Emissions, year) %>%
    group_by(year) %>%
    summarise (total=sum(Emissions))%>%
    plot(year, total, .)

This didn't work either and returned the same error.

Interestingly, the solution from the post I mentioned works for the same dataset but doesn't work out for my own data. However, I am able to create the plot using emission$year and emission$total.

Am I missing anything?

Solley answered 14/11, 2014 at 23:32 Comment(2)
As shown in the answer to the question you linked, it is plot.formula that has the data argument. So you'd need to use plot(total ~ year, .).Lift
This also works without specifying the . - so you could use e.g. plot(total ~ year).Jenny
C
17

plot.default doesn't take a data argument, so your best bet is to pipe to with:

mynei %>%
    select(Emissions, year) %>%
    group_by(year) %>%
    summarise (total=sum(Emissions))%>%
    with(plot(year, total))

In case anyone missed @aosmith's comment on the question, plot.formula does have a data argument, but of course the formula is the first argument so we need to use the . to put the data in the right place. So another option is

... %>%
  plot(total ~ year, data = .)

Of course, ggplot takes data as the first argument, so to use ggplot do:

... %>%
  ggplot(aes(x = year, y = total)) + geom_point()

lattice::xyplot is likeplot.formula: there is a data argument, but it's not first, so:

... %>% 
  xyplot(total ~ year, data = .)

Just look at the documentation and make sure you use a . if data isn't the first argument. If there's no data argument at all, using with is a good work-around.

Chorus answered 14/11, 2014 at 23:36 Comment(0)
O
8

As an alternative, you can use the %$% operator from magrittr to be able to access the columns of a dataframe directly. For example:

iris %$%
  plot(Sepal.Length~Sepal.Width)

This is useful many times when you need to feed the result of a dplyr chain to a base R function (such as table, lm, plot, etc). It can also be used to extract a column from a dataframe as a vector, e.g.:

iris %>% filter(Species=='virginica') %$% Sepal.Length

This is the same as:

iris %>% filter(Species=='virginica') %>% pull(Sepal.Length)

Ordzhonikidze answered 20/7, 2018 at 7:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.