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?
plot.default
doesn't take a data argument, so your best bet is to pipe towith
:In case anyone missed @aosmith's comment on the question,
plot.formula
does have a data argument, but of course theformula
is the first argument so we need to use the.
to put the data in the right place. So another option isOf course,
ggplot
takesdata
as the first argument, so to useggplot
do:lattice::xyplot
is likeplot.formula
: there is a data argument, but it's not first, so:Just look at the documentation and make sure you use a
.
ifdata
isn't the first argument. If there's nodata
argument at all, usingwith
is a good work-around.