简体   繁体   中英

side-by-side barplot with ggplot

I'm trying to display a side-by-side bar plot that compares the counts of each a letter grade between the 2 columns. (A's next to each other, B's next to each other etc.)

> dat = data.frame(grade1 = c('A','A','A','B','B','C'), grade2 = c('A','B','C','C','D','D'))
> dat
  grade1 grade2
1      A      A
2      A      B
3      A      C
4      B      C
5      B      D
6      C      D
> ggplot(dat, aes(x=grade1, fill=grade2)) +
   geom_bar(position=position_dodge())

I'm trying to get a result that looks something like this with 4 labels on the x-axis (A, B, C, D). Is there a particular dplyr function I should be using?

https://i0.wp.com/martinsbioblogg.files.wordpress.com/2014/03/means-barplot.png

You need to transform the data frame in a tidy form. For that you could use the tidyr package function gather . To ensure the correct sorting for the letter grade using an ordered factor is appropriate:

library(tidyr)
library(ggplot2)

dat <- data.frame(grade1 = c('A','A','A','B','B','C'), grade2 = c('A','B','C','C','D','D'))

tidy_dat <- gather(dat)
tidy_dat[,2] <- ordered(tidy_dat[,2], levels = c('A','B','C','D'))

ggplot(tidy_dat, aes(x= value, fill = key))+
   geom_bar(position = 'dodge')

在此处输入图片说明

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