ggplot geom_bar where x = multiple columns

后端 未结 4 1165
情深已故
情深已故 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条回答
  •  萌比男神i
    2020-12-07 03:59

    You need to first convert your data frame into a long format, and then use the created variable to set the facet_wrap().

    data_long <- tidyr::gather(data, key = type_col, value = categories, -col4)
    
    ggplot(data_long, aes(x = categories, fill = col4)) +
      geom_bar() + 
      facet_wrap(~ type_col, scales = "free_x")
    

提交回复
热议问题