如何将标签放在ggplot2中的堆积条形图上?

时间:2016-07-05 09:24:05

标签: ggplot2 label bar-chart

我正在按照here提到的有关如何将标签放在堆积条形图中间的说明。 标签放在正确的位置,但条形图完全缩小。我在这里做错了什么?

在没有标签的情况下运行此脚本时,它看起来没问题。

library(ggplot2)
library(plyr)
library(dplyr)

df <- structure(list(variable = c("name", "name", "name", "name", "name"
), score = structure(1:5, .Label = c("1", "2", "3", "4", "5"), class = "factor"), 
    N = c(97, 82, 116, 131, 116), percentage = c(18, 15, 21, 
    24, 21)), row.names = c(NA, 5L), class = "data.frame", .Names = c("variable", 
"score", "N", "percentage"))

df <- ddply(df,"variable ", mutate, label=cumsum(percentage) - .5*percentage)

ggplot(as.data.frame(df),aes(x = variable , y = percentage, fill = score)) + 
  geom_bar(stat = 'identity', position = 'fill')+
  #geom_text(aes(y=label,label=N), color='black') +
   coord_flip()

但是如果添加标签,它看起来并不好看:

ggplot(as.data.frame(df),aes(x = variable , y = percentage, fill = score)) + 
  geom_bar(stat = 'identity', position = 'fill')+
  geom_text(aes(y=label,label=N), color='black') +
   coord_flip()

0 个答案:

没有答案
相关问题