Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot与ddply相结合_R_Ggplot2_Plyr - Fatal编程技术网

R ggplot与ddply相结合

R ggplot与ddply相结合,r,ggplot2,plyr,R,Ggplot2,Plyr,我有样本数据test.data如下 income expend id 9142.7 1576.2 1 23648.75 2595 2 9014.25 156 1 4670.4 604.4 3 6691.4 3654.4 3 14425.2 66 2 8563.45 1976.2 2 2392 6 1 7915.95 619.2 3 4424.2 504.2 2 我首先使用ddply获得每个id的平均收入和支出 library(plyr) group

我有样本数据
test.data
如下

income  expend  id
9142.7  1576.2  1
23648.75 2595   2
9014.25 156 1
4670.4  604.4   3
6691.4  3654.4  3
14425.2 66  2
8563.45 1976.2  2
2392    6   1
7915.95 619.2   3
4424.2  504.2   2
我首先使用
ddply
获得每个id的平均收入和支出

library(plyr)
group<-ddply(test.data, .id,summarize, income.mean=mean(income),expend.mean=mean(expend))
虽然上面的代码运行时没有任何错误,但我正在寻找在ddply中组合qplot函数的有效方法,反之亦然。另外,如果我需要合并这两个图,我该怎么做


谢谢

为了合并这两个图,我必须将
restrape2
包放入
melt
数据中:

library(ggplot2)
library(plyr)
library(reshape2)

test.data <- read.table(text="income  expend  id
                 9142.7  1576.2  1
                 23648.75 2595   2
                 9014.25 156 1
                 4670.4  604.4   3
                 6691.4  3654.4  3
                 14425.2 66  2
                 8563.45 1976.2  2
                 2392    6   1
                 7915.95 619.2   3
                 4424.2  504.2   2", header=TRUE)

qplot(data=melt(ddply(test.data, .(id), colwise(mean)), id.vars="id"), x=id, y=value, colour=variable)
库(ggplot2)
图书馆(plyr)
图书馆(E2)

test.data嗯,你的问题不是很精确,因为我们不知道你到底想做什么。但这里有一个猜测:

d <- read.table(textConnection("income  expend  id
9142.7  1576.2  1
23648.75 2595   2
9014.25 156 1
4670.4  604.4   3
6691.4  3654.4  3
14425.2 66  2
8563.45 1976.2  2
2392    6   1
7915.95 619.2   3
4424.2  504.2   2"), header=TRUE)

library(reshape2)
d2 <- melt(d, id.var="id")
ggplot(data=d2, aes(x=id,y=value)) + stat_summary(fun.y="mean", geom="bar") + facet_grid(.~variable)

d我想你想要得到的是需要从'qplot'函数切换到'ggplot'函数。在“ddply”函数中包含图形函数不会很漂亮,反之亦然。你最好把它们分开,所以我只想把重点放在组合图上。在我看来,有两种好方法可以做到这一点:

选项1:只需在同一个“ggplot”对象上以单独的几何图形绘制两个图形即可。这并不难做到,工作原理如下:

ggplot(group) + geom_point(aes(x=id, y=income.mean), colour="red") + geom_point(aes(x=id, y=expend.mean), colour="blue")
这是一个快速的选项,可以用最少的计算完成任务。但是,需要为每列指定新的几何图形。在示例数据中,这不是一个问题,但在许多情况下,您希望使用代码来完成这项工作,而不是手工完成

选项2:重塑数据,将两组数据合并到一个绘图中。然后,我们可以通过变量着色来指定分组

library(reshape2)
plot_Data <- melt(group, id="id")

# Output of plot_Data
#   id    variable     value
# 1  1 income.mean  6849.650
# 2  2 income.mean 12765.400
# 3  3 income.mean  6425.917
# 4  1 expend.mean   579.400
# 5  2 expend.mean  1285.350
# 6  3 expend.mean  1626.000

ggplot(plot_Data, aes(x=id, y=value, col=variable)) + geom_point()
library(重塑2)

绘图数据虽然可以(有时也很有用)将如下所示的摘要和绘图步骤与
stat\u summary
相结合,但通常最好像最初那样在ggplot之外进行摘要。将两个步骤分开通常会更清晰,也更不容易出现错误。@rrs:谢谢您提供更多的解决方案@juba:谢谢,但我实际上是在寻找散点图。
library(reshape2)
plot_Data <- melt(group, id="id")

# Output of plot_Data
#   id    variable     value
# 1  1 income.mean  6849.650
# 2  2 income.mean 12765.400
# 3  3 income.mean  6425.917
# 4  1 expend.mean   579.400
# 5  2 expend.mean  1285.350
# 6  3 expend.mean  1626.000

ggplot(plot_Data, aes(x=id, y=value, col=variable)) + geom_point()