Using dplyr, how to connect or chain ()?

I am new to dplyr () package and trying to use it for my render destination. I can pass my data to ggplot () , but could not do it with plot () . I came across this post , and the answers, including comments, did not help 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 also did not work and returned the same error.

Interestingly, the solution from the post I mentioned works for the same data set, but does not work for my own data. However, I can create a plot using the $ year emission and the $ total emission .

Did I miss something?

+6
source share
1 answer

plot.default does not accept a data argument, so it is best to go to with :

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

If someone missed @aosmith's comment on the question, plot.formula has a data argument, but of course formula is the first argument, so we need to use . to put data to the right place. So another option

 ... %>% 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 similar to plot.formula : there is a data argument, but it is not the first, therefore:

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

Check the documentation and make sure you are using it . if data not the first argument. If there is no data argument at all, using with is a good job.

+7
source

Source: https://habr.com/ru/post/978218/


All Articles