首页 > 解决方案 > 在 ggplot2 中编辑条形图

问题描述

我创建了一个条形图,我想对布局进行一些更改。我已经尝试了几件事,但我想做以下事情:

  1. 将 Y 轴间隔从 0 更改为 50%
  2. 使表示条形内百分比的字母的大小变小
  3. 删除灰色背景并将其更改为白色

该图可以在这里找到:在此处输入图像描述

我提供的代码是:

beoordeling <- ggplot(data=etadam, aes(x = beoordeling)) + 
  geom_bar(aes(y = (..count..)/sum(..count..)), colour = "black", width = 0.6, fill = '#ffd308') +
  geom_text(aes(y = ((..count..)/sum(..count..)), label = scales::percent((..count..)/sum(..count..))), stat = "count", vjust = -0,2) + 
  scale_y_continuous(labels = percent) + 
  scale_x_continuous(breaks = seq(0, 10, by = 1)) +
  labs(x = NULL, y = 'percentage', title = 'Welk cijfer geven jullie de examentraining?', subtitle = 'N = 400 | Alle trainingen', caption = 'Leren voor de Toekomst©')

标签: rggplot2plot

解决方案


这应该做你想要的:

beoordeling <- ggplot(data=etadam, aes(x = beoordeling)) + 
  theme_minimal() +
  geom_bar(aes(y = (..count..)/sum(..count..)), colour = "black", width = 0.6, fill = '#ffd308') +
  geom_text(aes(y = ((..count..)/sum(..count..)), label = scales::percent((..count..)/sum(..count..))), stat = "count", vjust = -0,2, size = 2) + 
  scale_y_continuous(limits=c(0.5), expand = c(0,0),labels = percent) + 
  scale_x_continuous(breaks = seq(0, 10, by = 1)) +
  labs(x = NULL, y = 'percentage', title = 'Welk cijfer geven jullie de examentraining?', subtitle = 'N = 400 | Alle trainingen', caption = 'Leren voor de Toekomst©')
  1. 添加limits = c(0,0.5),如果你愿意expand = c(0,0)的话scale_y_continuous()

  2. 改变。size_ geom_text()我现在设置为 2

  3. 使用或。theme_minimal()_ 查看ggplot 主题theme_classic()theme_light()


推荐阅读