R qplot - 在 y 轴上绘制总和而不是计数的两个方面
R qplot - plotting two facets with sums on the y-axis instead of counts
是否可以根据位置 "A" 和 "B" 绘制两个条形面,其中在每个面中,我在 x 轴上显示 "year" 并在上显示年度总和y 轴?
这是我构建的玩具示例:
DF<-data.frame(place=c("A", "A", "A", "B", "B"), amt=c(1, 1, 1, 5, 2), year = c(2000, 2000, 2002, 2001, 2002))
DF$year<-as.factor(DF$year)
我试过使用 ddply 来计算总和:
sums<-ddply(DF,.(year,place), summarise, sumAmt=sum(amt))
但我不知道如何在 qplot 中使用它
qplot(??? , data=sums, facets=.~place)
如果我更换 ??? = year 然后我只是显示计数。我读过我应该尝试使用 geom_bar(stat = "identity") 和 aes(x = factor(year), y = sumAmt)),但我不明白如何。
如果您提供 'x' 和 'y' 变量,它将相应地绘制。
qplot(year, sumAmt, data=sums, facets=.~place)
要获取条形图,请指定 geom
qplot(year, sumAmt, data=sums, facets=.~place, geom='bar', stat='identity')
您也可以在不创建 'sums' 数据集的情况下执行此操作
qplot(year, amt, data=DF, stat='summary', fun.y='sum', facets=.~place)
如果你使用ggplot
,语法是
ggplot(DF, aes(x=year, y=amt))+
stat_summary(fun.y='sum', geom='point')+
facet_wrap(~place)
是否可以根据位置 "A" 和 "B" 绘制两个条形面,其中在每个面中,我在 x 轴上显示 "year" 并在上显示年度总和y 轴?
这是我构建的玩具示例:
DF<-data.frame(place=c("A", "A", "A", "B", "B"), amt=c(1, 1, 1, 5, 2), year = c(2000, 2000, 2002, 2001, 2002))
DF$year<-as.factor(DF$year)
我试过使用 ddply 来计算总和:
sums<-ddply(DF,.(year,place), summarise, sumAmt=sum(amt))
但我不知道如何在 qplot 中使用它
qplot(??? , data=sums, facets=.~place)
如果我更换 ??? = year 然后我只是显示计数。我读过我应该尝试使用 geom_bar(stat = "identity") 和 aes(x = factor(year), y = sumAmt)),但我不明白如何。
如果您提供 'x' 和 'y' 变量,它将相应地绘制。
qplot(year, sumAmt, data=sums, facets=.~place)
要获取条形图,请指定 geom
qplot(year, sumAmt, data=sums, facets=.~place, geom='bar', stat='identity')
您也可以在不创建 'sums' 数据集的情况下执行此操作
qplot(year, amt, data=DF, stat='summary', fun.y='sum', facets=.~place)
如果你使用ggplot
,语法是
ggplot(DF, aes(x=year, y=amt))+
stat_summary(fun.y='sum', geom='point')+
facet_wrap(~place)