Rows As Stacked Bar Plot Using ggplot2 In R

后端 未结 1 1594
小蘑菇
小蘑菇 2021-01-25 04:35

I am just getting started with ggplot2() (data visualization) in R. The data I have has different workloads in row format. Each of these column has fou

相关标签:
1条回答
  • 2021-01-25 05:22

    Change to the "superior" long format (here I use tidyr::gather), then map your columns to aesthetics, with a column geom with stacked position (bar is a special case which counts number of observations).

    library(tidyverse)
    
    df <- read.table(text = "
    Workload P1   P2   P3   P4
    W1       0.3  0.2  0.4  0.1
    W2       0.5  0.1  0.3  0.1
    W3       0.2  0.3  0.4  0.1
    W4       0.3  0.2  0.5  0.1", header = TRUE)
    
    
    df_long <- df %>% 
      gather(P, value, P1:P4)
    
    ggplot(df_long, aes(x = Workload, y = value, fill = P)) + 
      geom_col(position = position_stack())
    

    0 讨论(0)
提交回复
热议问题