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


112

我想在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") 

在此处输入图片说明

我想在每个部分的中间显示这些数据值。在这方面的任何帮助将不胜感激。谢谢



这不是真正的辩论场所,但我想知道是否有可能对此规定过于规范,特别是对于更广泛的受众而言。这是一个很好的例子 -数字表示可以记住的百分比,从而消除了对数字素养较少的读者可能会难以接近的标度的需求吗?
geotheory,2015年

Answers:


193

ggplot 2.2.0通过使用position = position_stack(vjust = 0.5)in 可以轻松地将From 标签堆叠起来geom_text

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_stack()并且position_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)

结果图


感谢您的回答。我用它data.table代替做了类似的plyr事情,所以是这样的:Data.dt[,list(Category, Frequency, pos=cumsum(Frequency)-0.5*Frequency), by=Year]
原子

反正还有总频率吗?
Pablo Olmos de Aguilera C.

26

正如哈德利(Hadley)提到的那样,与堆叠条形图中的标签相比,有更有效的信息传递方式。实际上,由于条形图(每个类别)不共享一个轴,所以堆积图不是很有效,因此比较很难。

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

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)

这将为您提供2个面板显示,如下所示:

垂直堆叠的2个面板图形

如果要添加频率值,则表格是最佳格式。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.