我正在绘制一个水平堆叠条形图.我wine 吧的标签很长.有没有办法把它们放在相应的条上?否则,我的页面上的空间没有得到很好的利用,绘图将显示得非常小.以下是我的代码和绘图:

I'd like to put

I'd like to put "Zivildienst" and "Katastrophenschutz" above the bars, not to the left of them.

data3 <- data.frame(System=rep(c('Zivildienst', 'Katastrophenschutz'), each=5),
                    Einstellung=rep(c('Sehr unwahrscheinich','Eher unwahrscheinlich','Weiss nicht','Eher wahrscheinlich','Sehr wahrscheinlich'), times=2),
                    Anzahl=c(131, 142, 283, 421, 981, 54, 140, 490, 418, 856))
level_order <- c('Zivildienst', 'Katastrophenschutz') 


# Get the levels for "Einstellung" in the required order
data3$Einstellung = factor(data3$Einstellung, levels = c('Weiss nicht','Sehr unwahrscheinich','Eher unwahrscheinlich','Eher wahrscheinlich','Sehr wahrscheinlich'))
data3 = arrange(data3, System, desc(Einstellung))

# Calculate the percentages
data3 = ddply(data3, .(System), transform, percent = Anzahl/sum(Anzahl) * 100)

# Format the labels and calculate their positions
data3 = ddply(data3, .(System), transform, pos = (cumsum(Anzahl) - 0.5 * Anzahl))
data3$label = paste0(sprintf("%.0f", data3$percent), "%")

cbPalette <- c("#999999", "#2A7AD4", "#5C96D7", "#D3A253", "#D48F1D")

# Plot
ggplot(data3, aes(x = factor(System), y = Anzahl, fill = Einstellung)) +
  geom_bar(stat = "identity", width = .3) +
  geom_text(aes(y = pos, label = label), size = 4) +
  theme(panel.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.title.y = element_blank(),
        aspect.ratio = .3) +
  scale_fill_manual(values=cbPalette)+
  coord_flip()+
  ggtitle("Werden Sie sich für die Alternative entscheiden?", subtitle = "2000 Männer befragt") + labs(x = NULL)

推荐答案

使用facet:

#fix the order of factors
data3$System <- factor(data3$System, levels = c("Zivildienst", "Katastrophenschutz"))

#plot with facets
ggplot(data3, aes(x = System, y = Anzahl, fill = Einstellung)) +
  geom_bar(stat = "identity") +
  geom_text(aes(y = pos, label = label), size = 4) +
  coord_flip() +
  facet_wrap(vars(System), ncol = 1, scales = "free_y") +
  scale_x_discrete(expand = c(0, 0)) +            # make bar "fuller"
  scale_y_continuous(expand = c(0, 0)) +          # make bar "fuller"
  scale_fill_manual(values = cbPalette) +
  ggtitle("Werden Sie sich für die Alternative entscheiden?", 
          subtitle = "2000 Männer befragt") + 
  theme(panel.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.title.y = element_blank(),
        aspect.ratio = .3,
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        strip.background = element_rect(fill = NA),# remove facet strip background
        strip.text = element_text(hjust = 0)       # left align the facet strip text
        )

enter image description here

R相关问答推荐

使用gggplot 2在R中重新调整面板和y轴文本大小

在特定列上滞后n行,同时扩展框架的长度

在R中查找每个组不同时间段的总天数

大规模重新标记haven标签数据

有没有一种方法可以从函数中创建一个值的列表,然后将这些值变成R中的直方图?我一直觉得不行

随机森林回归:下拉列重要性

如何写一个R函数来旋转最后n分钟?

使用较长的查询提取具有部分匹配的列表中的较短目标,

方法::slotName如何处理非类、非字符的参数?

如何在PackageStatus()中列出&q;不可用的包&q;?

从R中的对数正态分布生成随机数的正确方法

R -在先前group_by级别汇总时获取最大大小子组的计数

悬崖三角洲超大型群数计算导致整数溢出

使用shiny 中的所选要素行下拉菜单

我是否可以使用多个变异项来构建顺序列(标记多个问题)

循环遍历多个变量,并将每个变量插入函数R

从线的交点创建面

使用ifElse语句在ggploy中设置aes y值

当y为负值时,无法使stat_cor正确定位到底部?

根据部分名称匹配获取多组列的行求和