使用ggplot2和geom_text在比例条形图中插入标签 [英] insert labels in proportional bar chart with ggplot2 and geom_text
问题描述
我试图在成比例的条形图中插入标签:每个片段一个标签,其中每个片段的百分比为文本。在thothal的帮助下,我设法做到了这一点:
var1 < - factor(as.character(c(1,1 ,2,3,1,4,3,2,3,2,1,4,2,3,2,1,4,3,1,2)))
var2 < - factor(as .character(c(1,4,2,3,4,2,1,2,3,4,2,1,1,3,2,1,2,4,3,2)))$ b $ (data,。(var1),function(。){
res< - data.frame(var1,var2) - cumsum(prop.table(table(factor(。$ var2))))
data.frame(lab = names(res),y = c(res))
})
ggplot(data,aes(x = var1))+ geom_bar(aes(fill = var2),position ='fill')+
geom_text(aes(label = lab,x = var1,y = y),data = dat)
我想为标签分配每个级别的百分比,而不是级别名称。
任何帮助表示感谢!
$ c> geom_text 使用 var2
作为您的 y
va可变结构。这实际上是 as.numeric(数据$ var2)
,它转换为 1-4
的范围。但是,您的barplot会使用累计百分比。
因此,您必须在计算这些头寸之前:
pre > library(ggplot2)
library(plyr)#只是为了方便
var1 < - factor(as.character(c(1,1,2,3,1,4) ,3,2,3,2,1,4,2,3,2,1,4,3,1,2)))
var2 < - factor(as.character(c(1,4 ,2,3,4,2,1,2,3,4,2,1,1,3,2,1,2,4,3,2)))
data< - data.frame (var1,var2)
$ b $ dat < - ddply(data,。(var1),function(。){
res < - cumsum(prop.table(table(factor(。 $ var2))))#重新使用仅使用的级别
res2 < - prop.table(table(factor(。$ var2)))#重新使用仅使用的级别
data.frame(lab = names(res),y = c(res),lab2 = c(res2))
})
ggplot(data,aes(x = var1)) + geom_bar(aes(fill = var2),position ='fill')+
geom_text(aes(label = round(lab2,2),x = var1,y = y),data = dat)
这将实验室放置在每个小节的末尾。如果你想让它们略微偏移一下,你应该在创建 dat
时创建一个圆形。
I am trying to insert labels into a proportional barchart: one label per segment, with as text the percentage of each segment. With the help of thothal I managed to do this:
var1 <- factor(as.character(c(1,1,2,3,1,4,3,2,3,2,1,4,2,3,2,1,4,3,1,2)))
var2 <- factor(as.character(c(1,4,2,3,4,2,1,2,3,4,2,1,1,3,2,1,2,4,3,2)))
data <- data.frame(var1, var2)
dat <- ddply(data, .(var1), function(.) {
res <- cumsum(prop.table(table(factor(.$var2))))
data.frame(lab = names(res), y = c(res))
})
ggplot(data, aes(x = var1)) + geom_bar(aes(fill = var2), position = 'fill') +
geom_text(aes(label = lab, x = var1, y = y), data = dat)
I would like to have for labels the percentage of each level, and not the level name.
Any help appreciated!
You are telling geom_text
to use var2
as your y
variable. That is in fact as.numeric(data$var2)
, which translates to a range of 1-4
. However, your barplot uses the cumulative percentages.
Hence you have to calculate these positions before:
library(ggplot2)
library(plyr) # just for convenience
var1 <- factor(as.character(c(1,1,2,3,1,4,3,2,3,2,1,4,2,3,2,1,4,3,1,2)))
var2 <- factor(as.character(c(1,4,2,3,4,2,1,2,3,4,2,1,1,3,2,1,2,4,3,2)))
data <- data.frame(var1, var2)
dat <- ddply(data, .(var1), function(.) {
res <- cumsum(prop.table(table(factor(.$var2)))) # re-factor to use only used levels
res2 <- prop.table(table(factor(.$var2))) # re-factor to use only used levels
data.frame(lab = names(res), y = c(res), lab2 = c(res2))
})
ggplot(data, aes(x = var1)) + geom_bar(aes(fill = var2), position = 'fill') +
geom_text(aes(label = round(lab2, 2), x = var1, y = y), data = dat)
This places the labs at the end of each bar. If you want to have them slightly offset, you should play arround in the creation of dat
.
这篇关于使用ggplot2和geom_text在比例条形图中插入标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!