MYa*_*208 97 graphics r 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, 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")
Run Code Online (Sandbox Code Playgroud)

我想在每个部分的中间显示这些数据值.在这方面的任何帮助将受到高度赞赏.谢谢
Ram*_*ath 167
从ggplot 2.2.0标签可以很容易地使用堆叠position = position_stack(vjust = 0.5)在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))
Run Code Online (Sandbox Code Playgroud)
另外请注意," 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)
Run Code Online (Sandbox Code Playgroud)

And*_*nCH 22
正如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)
Run Code Online (Sandbox Code Playgroud)
这将为您提供如下所示的2面板显示:

如果要添加频率值,表格是最佳格式.
正如 @Henrik 编辑的 @Ramnath 的答案所示,通过将参数传递给可以调整标签相对位置的vjust参数position_stack(),这对于居中标签非常有效。在问题本身中,@MYaseen208 展示了如何使用垂直对齐来替换标签的位置。在 R 中,对齐是相对于文本标签的边界框而言的,这可能会导致标签的位置略有不同,具体取决于标签中的字符(带有“g”等下降部分或不带有“a”等下降部分),或者当文本大小或图形设备更改。根据具体情况,这可能是优点,也可能是缺点。
在这里,我提供了一个示例,作为在某些情况下可能更可取的替代答案,将文本标签定位为从原始位置向下微移数据单元中的恒定距离。这相当于组合position_stack()and ,可以通过包“ggpp”position_nudge()来实现。position_stacknudge()
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)
library(ggpp)
ggplot(Data, aes(x = Year, y = Frequency, fill = Category, label = Frequency)) +
geom_bar(stat = "identity") +
geom_text(size = 3, position = position_stacknudge(y = -60))
Run Code Online (Sandbox Code Playgroud)

创建于 2022-09-03,使用reprex v2.0.2