在ggplot 2中显示堆叠条形图上的数据值

在ggplot 2中显示堆叠条形图上的数据值

我想在ggplot 2中的堆叠条形图上显示数据值。这是我尝试的代码

Year      <- c(rep(c("2006-07", "2007-08", "2008-09", "2009-10"), each = 4))

Category  <- c(rep(c("A", "B", "C", "D"), times = 4))

Frequency <- c(168, 259, 226, 340, 216, 431, 319, 368, 423, 645, 234, 685, 166, 467, 274, 251)

Data      <- data.frame(Year, Category, Frequency)

library(ggplot2)

p <- qplot(Year, Frequency, data = Data, geom = "bar", fill = Category,     theme_set(theme_bw()))

p + geom_text(aes(label = Frequency), size = 3, hjust = 0.5, vjust = 3, position =     "stack") 

我想在每个部分的中间显示这些数据值。在这方面的任何帮助都将受到高度赞赏。谢谢


慕桂英546537
浏览 2263回答 2
2回答

Qyouu

从…ggplot 2.2.0标签可以通过使用position = position_stack(vjust = 0.5)在……里面geom_text.ggplot(Data,&nbsp;aes(x&nbsp;=&nbsp;Year,&nbsp;y&nbsp;=&nbsp;Frequency,&nbsp;fill&nbsp;=&nbsp;Category,&nbsp;label&nbsp;=&nbsp;Frequency))&nbsp;+ &nbsp;&nbsp;geom_bar(stat&nbsp;=&nbsp;"identity")&nbsp;+ &nbsp;&nbsp;geom_text(size&nbsp;=&nbsp;3,&nbsp;position&nbsp;=&nbsp;position_stack(vjust&nbsp;=&nbsp;0.5))还请注意“position_stack()和position_fill()现在,堆栈值按分组的相反顺序排列,这使得默认堆栈顺序与图例匹配。“的旧版本有效的答案。ggplot:这里有一种方法,用来计算条形图的中点。library(ggplot2)library(plyr)#&nbsp;calculate&nbsp;midpoints&nbsp;of&nbsp;bars&nbsp;(simplified&nbsp;using&nbsp;comment&nbsp;by&nbsp;@DWin)Data&nbsp;<-&nbsp;ddply(Data,&nbsp;.(Year),&nbsp; &nbsp;&nbsp;&nbsp;transform,&nbsp;pos&nbsp;=&nbsp;cumsum(Frequency)&nbsp;-&nbsp;(0.5&nbsp;*&nbsp;Frequency))#&nbsp;library(dplyr)&nbsp;##&nbsp;If&nbsp;using&nbsp;dplyr...&nbsp;#&nbsp;Data&nbsp;<-&nbsp;group_by(Data,Year)&nbsp;%>%#&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;mutate(pos&nbsp;=&nbsp;cumsum(Frequency)&nbsp;-&nbsp;(0.5&nbsp;*&nbsp;Frequency))#&nbsp;plot&nbsp;bars&nbsp;and&nbsp;add&nbsp;textp&nbsp;<-&nbsp;ggplot(Data,&nbsp;aes(x&nbsp;=&nbsp;Year,&nbsp;y&nbsp;=&nbsp;Frequency))&nbsp;+ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;geom_bar(aes(fill&nbsp;=&nbsp;Category),&nbsp;stat="identity")&nbsp;+ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;geom_text(aes(label&nbsp;=&nbsp;Frequency,&nbsp;y&nbsp;=&nbsp;pos),&nbsp;size&nbsp;=&nbsp;3)

隔江千里

正如Hadley所提到的,与堆叠条形图中的标签相比,有更有效的方式来传递您的消息。事实上,堆叠的图表并不是很有效,因为条形图(每个类别)不共享一个轴,所以比较是很困难的。在这些实例中使用两个图几乎总是更好的,共享一个公共轴。在您的示例中,我假设您希望显示总体总数,然后显示每个类别在给定年份中所占的比例。library(grid)library(gridExtra)library(plyr)#&nbsp;create&nbsp;a&nbsp;new&nbsp;column&nbsp;with&nbsp;proportionsprop&nbsp;<-&nbsp;function(x)&nbsp;x/sum(x)Data&nbsp;<-&nbsp;ddply(Data,"Year", transform,Share=prop(Frequency))#&nbsp;create&nbsp;the&nbsp;component&nbsp;graphicstotals&nbsp;<-&nbsp;ggplot(Data,aes(Year,Frequency))&nbsp;+&nbsp;geom_bar(fill="darkseagreen", stat="identity")&nbsp;+&nbsp; &nbsp;&nbsp;xlab("")&nbsp;+&nbsp;labs(title&nbsp;=&nbsp;"Frequency&nbsp;totals&nbsp;in&nbsp;given&nbsp;Year")proportion&nbsp;<-&nbsp;ggplot(Data,&nbsp;aes(x=Year,y=Share,&nbsp;group=Category,&nbsp;colour=Category))&nbsp;+ &nbsp;&nbsp;&nbsp;geom_line()&nbsp;+&nbsp;scale_y_continuous(label=percent_format())+&nbsp;theme(legend.position&nbsp;=&nbsp;"bottom")&nbsp;+&nbsp; &nbsp;&nbsp;labs(title&nbsp;=&nbsp;"Proportion&nbsp;of&nbsp;total&nbsp;Frequency&nbsp;accounted&nbsp;by&nbsp;each&nbsp;Category&nbsp;in&nbsp;given&nbsp;Year")#&nbsp;bring&nbsp;them&nbsp;togethergrid.arrange(totals,proportion)如果要添加频率值,表是最佳格式。
打开App,查看更多内容
随时随地看视频慕课网APP