R 在ggplot2的堆叠条形图上显示数据值

R 在ggplot2的堆叠条形图上显示数据值,r,graphics,ggplot2,R,Graphics,Ggplot2,我想在ggplot2的堆叠条形图上显示数据值。这是我的代码 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,

我想在ggplot2的堆叠条形图上显示数据值。这是我的代码

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") 
使用
geom\u text
中的
position=position\u stack(vjust=0.5)
可以轻松堆叠标签中的
Year

ggplot(Data, aes(x = Year, y = Frequency, fill = Category, label = Frequency)) +
  geom_bar(stat = "identity") +
  geom_text(size = 3, position = position_stack(vjust = 0.5))

还要注意,“
position\u stack()
position\u fill()
现在按分组的相反顺序堆叠值,这使得默认堆栈顺序与图例匹配。”


回答对较早版本的
ggplot
有效:

这里有一种方法,用于计算条形图的中点

library(ggplot2)
library(plyr)

# calculate midpoints of bars (simplified using comment by @DWin)
Data <- ddply(Data, .(Year), 
   transform, pos = cumsum(Frequency) - (0.5 * Frequency)
)

# library(dplyr) ## If using dplyr... 
# Data <- group_by(Data,Year) %>%
#    mutate(pos = cumsum(Frequency) - (0.5 * Frequency))

# plot bars and add text
p <- ggplot(Data, aes(x = Year, y = Frequency)) +
     geom_bar(aes(fill = Category), stat="identity") +
     geom_text(aes(label = Frequency, y = pos), size = 3)
库(ggplot2)
图书馆(plyr)
#计算条形的中点(使用@DWin注释简化)

数据正如哈德利所提到的,与堆叠条形图中的标签相比,有更有效的方式传达信息。事实上,堆叠图表不是很有效,因为条形图(每个类别)不共享一个轴,所以比较困难

在这些情况下,使用两个共享一个轴的图形几乎总是更好的。在您的示例中,我假设您希望显示总体总数,然后显示给定年份中每个类别所占的比例

library(grid)
library(gridExtra)
library(plyr)

# create a new column with proportions
prop <- function(x) x/sum(x)
Data <- ddply(Data,"Year",transform,Share=prop(Frequency))

# create the component graphics
totals <- ggplot(Data,aes(Year,Frequency)) + geom_bar(fill="darkseagreen",stat="identity") + 
  xlab("") + labs(title = "Frequency totals in given Year")
proportion <- ggplot(Data, aes(x=Year,y=Share, group=Category, colour=Category)) 
+ geom_line() + scale_y_continuous(label=percent_format())+ theme(legend.position = "bottom") + 
  labs(title = "Proportion of total Frequency accounted by each Category in given Year")

# bring them together
grid.arrange(totals,proportion)
库(网格)
图书馆(gridExtra)
图书馆(plyr)
#创建具有比例的新列

谢谢你的回答。我用它来做类似的事情,使用
数据.table
而不是
plyr
,所以类似这样:
data.dt[,list(Category,Frequency,pos=cumsum(Frequency)-0.5*Frequency),by=Year]
是否也要加上总频率?相关问题:不太适合辩论,但我想知道是否有可能对这一点过于规范,特别是对更普通的观众数字表示可以记住的百分比,这就不需要数字素养较低的读者可能会发现难以使用的刻度?