简体   繁体   中英

Edit the Barplot in ggplot2

I've created a barplot and I want to make some changes in the layout. I've tried a couple of things but I want to do the following:

  1. Change the Y-axis interval from 0 - 50%
  2. Make the size of the letters which present the percentage within the bars smaller
  3. Remove the gray background and change it for a white one

The plot can be found here: enter image description here

The code I provided is:

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©')

This should do what you want:

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. add limits = c(0,0.5) and if you wish expand = c(0,0) inside scale_y_continuous() .

  2. Change size in geom_text() . I set it to 2 at the moment

  3. Use theme_minimal() , theme_classic() or theme_light() . See ggplot themes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM