R 条形图中的百分比标签

R 条形图中的百分比标签,r,ggplot2,label,R,Ggplot2,Label,我想制作一幅与此类似的图形: 我从以下代码开始: 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)) +

我想制作一幅与此类似的图形:

我从以下代码开始:

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)) 
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))
但是我得到了这个错误(我使用的是ggplot2版本2.0.0):

错误:StatBin需要一个连续的x变量。x变量是离散的。也许你想要stat=“count”

最后,我用以下代码绘制了图:

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)) 
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%group\u by(var2)%%>%summary(freq=n()/nrow(df))
ggplot(数据=per,aes(x=var2,y=freq))+
几何图形栏(stat=“identity”,fill=“dodgerblue3”)+
几何图形文本(aes(标签=百分比(频率)),vjust=1.5,颜色=“白色”)+
连续缩放(标签=百分比)+
实验室(x=NULL,y=NULL)+
主题(axis.ticks.x=element_blank(),
axis.text=元素\文本(大小=7))

但是,是否有可能使其像这样,而不需要每个数据帧直接在ggplot中使用

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\u count
,而不是
stat\u bin
。从

统计计数,统计每个x位置的病例数, 没有装箱。它适用于离散和连续x 数据,而stat_bin仅适用于连续x数据


谢谢但它没有起作用。至少对我来说。我错过了一些东西,因为它对你有用。但是我试过你的代码,得到了同样的错误:StatBin需要一个连续的x变量,x变量是离散的。也许您想要stat=“count”。“我不知道我做错了什么。”克里斯蒂安·冈萨雷斯·马特尔·斯特兰奇说。尝试R/Rstudio(不保存工作区映像),然后重试?它仍然不起作用。也许问题出在ggplot2的版本上。我使用2.0.0。你用的是相同的吗?@ChristianGonzalez Martel我用的是旧版本。已更新,现在得到与您相同的错误!缺陷请将您的所有版本信息添加到您的问题中,并说明它在ggplot版本1中工作。X@ChristianGonzalez-Martel我找到了问题并将其添加到我的答案中