使用dplyr,如何通过管道或链式连接到plot()? [英] Using dplyr, how to pipe or chain to plot()?
问题描述
我是dplyr()软件包的新手,正在尝试将其用于可视化任务.我可以将数据通过管道传输到 ggplot(),但不能通过 plot()来完成.我遇到了这篇文章和答案包括评论中的那一项,对我不起作用.
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.
代码1:
emission <- mynei %>%
select(Emissions, year) %>%
group_by(year) %>%
summarise (total=sum(Emissions))
emission %>%
plot(year, total,.)
我收到以下错误:
Error in plot(year, total, emission) : object 'year' not found
代码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.
有趣的是,我提到的帖子中的解决方案适用于同一数据集,但不适用于我自己的数据.但是,我可以使用 emission $ year 和 emission $ total 创建图.
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.
我想念什么吗?
推荐答案
plot.default
不采用数据参数,因此最好的选择是通过管道传输到with
:
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))
如果有人错过了@aosmith对这个问题的评论,plot.formula
确实有一个数据参数,但是当然formula
是第一个参数,因此我们需要使用.
将数据放在正确的位置.所以另一种选择是
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 = .)
当然,ggplot
将data
作为第一个参数,因此要使用ggplot
做:
Of course, ggplot
takes data
as the first argument, so to use ggplot
do:
... %>%
ggplot(aes(x = year, y = total)) + geom_point()
lattice::xyplot
就像plot.formula
:有一个数据参数,但不是第一个,所以:
lattice::xyplot
is likeplot.formula
: there is a data argument, but it's not first, so:
... %>%
xyplot(total ~ year, data = .)
只需查看文档,并确保如果data
不是第一个参数,则使用.
.如果根本没有data
参数,那么使用with
是一个很好的解决方法.
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.
这篇关于使用dplyr,如何通过管道或链式连接到plot()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!