我想在 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")
https://i.stack.imgur.com/g5G5q.png
我想在每个部分的中间显示这些数据值。在这方面的任何帮助将不胜感激。谢谢
通过使用 geom_text
中的 position = position_stack(vjust = 0.5)
,可以轻松地从 ggplot 2.2.0
堆叠标签。
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))
https://i.stack.imgur.com/4wtsd.png
另请注意,“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)
https://i.stack.imgur.com/xi3uH.png
正如哈德利所说,有比堆叠条形图中的标签更有效的方式来传达您的信息。事实上,堆叠图表并不是很有效,因为条形图(每个类别)不共享一个轴,因此很难进行比较。
在这些情况下使用两个图表几乎总是更好,共享一个公共轴。在您的示例中,我假设您要显示总体总数,然后显示每个类别在给定年份中贡献的比例。
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 面板显示,如下所示:
https://i.stack.imgur.com/SfuXk.png
如果要添加频率值,最好使用表格格式。
data.table
而不是plyr
,所以是这样的:Data.dt[,list(Category, Frequency, pos=cumsum(Frequency)-0.5*Frequency), by=Year]