条形图中的百分比标签
Percentage labels in a bar plot
我想生成类似这样的图形:
我从这段代码开始:
library(ggplot2)
library(scales) #needed for labels=percent
var1 <- sample(0:20,78,replace = TRUE)
var2 <- cut(var1, breaks = seq(0,20,5),include.lowest = TRUE)
df<-as.data.frame(var2)
ggplot(df, aes(x= var2)) +
geom_bar(aes(y = ..prop..,group=1),fill="dodgerblue3")+
scale_y_continuous(labels=percent)+
labs(x = NULL,y = NULL)+
theme(axis.ticks.x = element_blank(),
axis.text = element_text(size=7))
但是我不能把标签放在图中。
我试着关注:
ggplot(df, aes(x= var2, group=1)) +
geom_bar(aes(y = ..density..)) +
geom_text(aes( label = format(100*..density.., digits=2, drop0trailing=TRUE),
y= ..density.. ), stat= "bin", vjust = -.5) +
scale_y_continuous(labels=percent)
但是我得到了这个错误(我使用的是ggplot2-version 2.0.0):
Error: StatBin requires a continuous x variable the x variable is discrete. Perhaps you want stat="count"?
最后我用这段代码制作了情节:
per <- df %>% group_by(var2) %>% summarise(freq = n()/nrow(df))
ggplot(data=per, aes(x=var2,y=freq)) +
geom_bar(stat="identity",fill="dodgerblue3")+
geom_text(aes(label=percent(freq)),vjust=1.5,colour="white")+
scale_y_continuous(labels=percent)+
labs(x = NULL,y = NULL)+
theme(axis.ticks.x = element_blank(),
axis.text = element_text(size=7))
但是,是否可以像 一样,不需要 per
数据框,直接在 ggplot 中?
你可以试试这个,它取自 here 并经过定制。
ggplot(df, aes(factor(var2))) +
geom_bar(fill="dodgerblue3")+
labs(x = NULL,y = NULL)+
stat_bin(aes(label = paste(prop.table(..count..) * 100, "%", sep = "")),
vjust = 1, geom = "text", position = "identity", color ="white")
给予:
编辑:
在新的 ggplot 2.0.X 版本中,应该使用 stat_count
而不是 stat_bin
。来自 help
stat_count, which counts the number of cases at each x posotion,
without binning. It is suitable for both discrete and continuous x
data, whereas stat_bin is suitable only for continuous x data.
ggplot(df, aes(factor(var2))) +
geom_bar(fill="dodgerblue3")+
labs(x = NULL,y = NULL)+
stat_count(aes(label = paste(prop.table(..count..) * 100, "%", sep = "")),
vjust = 1, geom = "text", position = "identity", color ="white")
我想生成类似这样的图形:
我从这段代码开始:
library(ggplot2)
library(scales) #needed for labels=percent
var1 <- sample(0:20,78,replace = TRUE)
var2 <- cut(var1, breaks = seq(0,20,5),include.lowest = TRUE)
df<-as.data.frame(var2)
ggplot(df, aes(x= var2)) +
geom_bar(aes(y = ..prop..,group=1),fill="dodgerblue3")+
scale_y_continuous(labels=percent)+
labs(x = NULL,y = NULL)+
theme(axis.ticks.x = element_blank(),
axis.text = element_text(size=7))
但是我不能把标签放在图中。
我试着关注
ggplot(df, aes(x= var2, group=1)) +
geom_bar(aes(y = ..density..)) +
geom_text(aes( label = format(100*..density.., digits=2, drop0trailing=TRUE),
y= ..density.. ), stat= "bin", vjust = -.5) +
scale_y_continuous(labels=percent)
但是我得到了这个错误(我使用的是ggplot2-version 2.0.0):
Error: StatBin requires a continuous x variable the x variable is discrete. Perhaps you want stat="count"?
最后我用这段代码制作了情节:
per <- df %>% group_by(var2) %>% summarise(freq = n()/nrow(df))
ggplot(data=per, aes(x=var2,y=freq)) +
geom_bar(stat="identity",fill="dodgerblue3")+
geom_text(aes(label=percent(freq)),vjust=1.5,colour="white")+
scale_y_continuous(labels=percent)+
labs(x = NULL,y = NULL)+
theme(axis.ticks.x = element_blank(),
axis.text = element_text(size=7))
但是,是否可以像 per
数据框,直接在 ggplot 中?
你可以试试这个,它取自 here 并经过定制。
ggplot(df, aes(factor(var2))) +
geom_bar(fill="dodgerblue3")+
labs(x = NULL,y = NULL)+
stat_bin(aes(label = paste(prop.table(..count..) * 100, "%", sep = "")),
vjust = 1, geom = "text", position = "identity", color ="white")
给予:
编辑:
在新的 ggplot 2.0.X 版本中,应该使用 stat_count
而不是 stat_bin
。来自 help
stat_count, which counts the number of cases at each x posotion, without binning. It is suitable for both discrete and continuous x data, whereas stat_bin is suitable only for continuous x data.
ggplot(df, aes(factor(var2))) +
geom_bar(fill="dodgerblue3")+
labs(x = NULL,y = NULL)+
stat_count(aes(label = paste(prop.table(..count..) * 100, "%", sep = "")),
vjust = 1, geom = "text", position = "identity", color ="white")