R 如何在ggplot图形栏上方添加百分比?

R 如何在ggplot图形栏上方添加百分比?,r,ggplot2,R,Ggplot2,如何添加data.frame中条形图上方的百分比 我怎么能在逗号后用两个小数位呢 我尝试应用ggplot,但没有弄错: a <- c("até 50","50-150","150-500","500-2000") b <- c(107,38,14,3) pcent <- c(66.049383, 23.456790, 8.641975, 1.851852) df <- data.frame(

如何添加data.frame中条形图上方的百分比

我怎么能在逗号后用两个小数位呢

我尝试应用ggplot,但没有弄错:

a <- c("até 50","50-150","150-500","500-2000")
b <- c(107,38,14,3)
pcent <- c(66.049383, 23.456790, 8.641975, 1.851852)

df <- data.frame(a,b,pcent)

ggplot(df, aes (x=reorder(a, -b), y=b, label=pcent)) +
  geom_col(fill="#70A2E7") +
  labs(x = "Faixa de desmatamento (ha)", y="Quantidade de polígonos de desmatamento", title = "Distribuição dos polígonos de desmatamento") +
  geom_text(aes(y = pcent, label = scales::percent(pcent)), vjust = -0.2)+
   scale_y_continuous(labels = scales::percent)

a您的y轴是原始计数,您希望在条形图顶部绘制百分比,这可能有点误导。无论如何,您可以:

library(ggplot2)
ggplot(df, aes (x=reorder(a, -b), y=b, label=pcent)) +
  geom_col(fill="#70A2E7") +
  labs(x = "Faixa de desmatamento (ha)", 
       y="Quantidade de polígonos de desmatamento", 
       title = "Distribuição dos polígonos de desmatamento") +
  geom_text(aes(label = paste0(round(pcent,2), '%')), vjust = -0.5)

或者最好在y轴上显示百分比

ggplot(df, aes (x=reorder(a, -b), y=pcent, label=pcent)) +
  geom_col(fill="#70A2E7") +
  labs(x = "Faixa de desmatamento (ha)", 
       y="Quantidade de polígonos de desmatamento", 
       title = "Distribuição dos polígonos de desmatamento") +
  geom_text(aes(label = paste0(round(pcent,2), '%')), vjust = -0.5) + 
  scale_y_continuous(labels = function(x) paste0(x, '%'))

hello@Ronak Shah,我实际上注意到Y轴在图表上是错误的,因为它不应该以百分比的形式出现。我运行了你的代码并成功了。这正是我所需要的。谢谢你的帮助!!!