ggplot geom_bar where x = multiple columns

后端 未结 4 1160
情深已故
情深已故 2020-12-07 03:21

How can I go about making a bar plot where the X comes from multiple values of a data frame?

Fake data:

data <- data.frame(col1 = rep(c(\"A\", \"         


        
4条回答
  •  失恋的感觉
    2020-12-07 04:06

    If you actually group your Y's and N's by the other three columns, there will be one observation in each group. However, if you had repeated Y's and N's you could recode them to 1's and 0's, and get the percentage. Here's an example:

     library(tidyverse)
    
     data <- data.frame(col1 = rep(c("A", "B", "C", "B", "C", "A", "A", "B", "B", "A", "C")), 
                   col2 = rep(c(2012, 2012, 2012, 2013, 2013, 2014, 2014, 2014, 2015, 2015, 2015)), 
                   col3 = rep(c("Up", "Down", "Up", "Up", "Down", "Left", "Right", "Up", "Right", "Down", "Up")), 
                   col4 = rep(c("Y", "N", "N", "N", "Y", "N", "Y", "Y", "Y", "N", "Y")))
    
    
     data %>%
        dplyr::group_by(col1,col2,col3) %>%
        mutate(col4 = ifelse(col4 == "Y", 1,0)) %>%
        dplyr::summarise(percentage = mean(col4)) %>%
        ggplot(aes(x = col1, y = percentage, color = as.factor(col2), fill = col3)) +
        geom_col(position = position_dodge(width = .5))
    

    Example

提交回复
热议问题