R ggplot2:在条形图顶部绘制百分比值,比例在 geom_bar 函数内计算
R ggplot2: Plot percentage values on top of Bar chart with proportion calculated within the geom_bar function
使用的数据:
structure(list(Year = c(2014L, 2014L, 2014L, 2014L, 2014L, 2014L,
2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L,
2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L,
2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L,
2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L,
2014L, 2014L, 2014L, 2014L, 2014L), Rating = c(4L, 4L, 4L, 3L,
4L, 4L, 4L, 3L, 4L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 4L, 5L, 5L, 5L, 5L, 4L, 4L, 4L, 5L,
5L, 4L, 5L, 5L, 5L, 5L, 4L, 4L, 4L, 5L, 5L)), .Names = c("Year",
"Rating"), class = "data.frame", row.names = c(NA, -47L))
下面是我用来绘制条形图的代码:
ggplot(subset(ws_inf, ws_inf$Year=="2014"), mapping = aes(x=Rating)) +
geom_bar(mapping = aes(y = ..prop.., group = 1)) +
ggtitle("2014") +
ylab("Percentage") +
xlab("Rating")
我已经在 geom_bar
函数本身中计算了比例。我想知道如何在栏的顶部显示相应的百分比值。条形图看起来像
我确实在堆栈溢出上进行了搜索,但其中大部分都已经计算出了百分比。我想知道我是否可以绘制百分比值。
我试过了
ggplot(subset(ws_inf, ws_inf$Year=="2014"), mapping = aes(x=Rating)) +
geom_bar(mapping = aes(y = (..prop..)*100, group = 1)) +
geom_text(stat='count', aes(label=round((..prop..)*100,2)), vjust=-0.5) +
ggtitle("2014") +
ylab("Percentage") +
xlab("Rating")
得到了下图,但是有没有办法调整文本:
使用vjust
。怎么样:
library(ggplot2)
ggplot(subset(ws_inf,ws_inf$Year=="2014"), mapping = aes(x=Rating)) +
geom_bar(aes(y = ..prop.., group = 1)) +
geom_text(aes( y = ..prop.., label = scales::percent(..prop..)), stat = "count", vjust = -1)
使用的数据:
structure(list(Year = c(2014L, 2014L, 2014L, 2014L, 2014L, 2014L,
2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L,
2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L,
2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L,
2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L,
2014L, 2014L, 2014L, 2014L, 2014L), Rating = c(4L, 4L, 4L, 3L,
4L, 4L, 4L, 3L, 4L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 4L, 5L, 5L, 5L, 5L, 4L, 4L, 4L, 5L,
5L, 4L, 5L, 5L, 5L, 5L, 4L, 4L, 4L, 5L, 5L)), .Names = c("Year",
"Rating"), class = "data.frame", row.names = c(NA, -47L))
下面是我用来绘制条形图的代码:
ggplot(subset(ws_inf, ws_inf$Year=="2014"), mapping = aes(x=Rating)) +
geom_bar(mapping = aes(y = ..prop.., group = 1)) +
ggtitle("2014") +
ylab("Percentage") +
xlab("Rating")
我已经在 geom_bar
函数本身中计算了比例。我想知道如何在栏的顶部显示相应的百分比值。条形图看起来像
我确实在堆栈溢出上进行了搜索,但其中大部分都已经计算出了百分比。我想知道我是否可以绘制百分比值。
我试过了
ggplot(subset(ws_inf, ws_inf$Year=="2014"), mapping = aes(x=Rating)) +
geom_bar(mapping = aes(y = (..prop..)*100, group = 1)) +
geom_text(stat='count', aes(label=round((..prop..)*100,2)), vjust=-0.5) +
ggtitle("2014") +
ylab("Percentage") +
xlab("Rating")
得到了下图,但是有没有办法调整文本:
使用vjust
。怎么样:
library(ggplot2)
ggplot(subset(ws_inf,ws_inf$Year=="2014"), mapping = aes(x=Rating)) +
geom_bar(aes(y = ..prop.., group = 1)) +
geom_text(aes( y = ..prop.., label = scales::percent(..prop..)), stat = "count", vjust = -1)