【问题标题】:ggplot plotting bars that show the sum of other columnsggplot 绘图条显示其他列的总和
【发布时间】:2021-12-29 15:26:45
【问题描述】:

你好,节日快乐 我的数据如下:

ID <- c(1,2,3,4)
time_1 <- c(12,20,31,18)
time_2 <- c(5,8,11,7)
time_3 <- c(15,5,9,17)
total_time <- c(32,33,51,42)
data_test<- data.frame(ID,time_1,time_2,time_3,total_time)
data_test %>% glimpse()

$ ID         <dbl> 1, 2, 3, 4
$ time_1     <dbl> 12, 20, 31, 18
$ time_2     <dbl> 5, 8, 11, 7
$ time_3     <dbl> 15, 5, 9, 17
$ total_time <dbl> 32, 33, 51, 42

变量total_time 是其他time 列的sum。 我需要为每个ID 绘制带有条形的total_time,以便在每个条形中我可以看到所有三个time 列的数量,以便查看链接到每个@ 的 total_time 条形中的每个时间列987654329@.

我知道这对你们大多数人来说是一个简单的问题,我很抱歉耽误了您的时间。现在只是烦我。

谢谢您,期待您的回音!

【问题讨论】:

    标签: r ggplot2 dplyr bar-chart


    【解决方案1】:

    您想要一个堆积条形图。在 ggplot 中,这很容易与 pivot_longer() 结合使用

    data_test %>% pivot_longer(time_1:time_3)  %>%
      ggplot() + 
      geom_bar(aes(ID, value, fill=name),position = "stack",stat = "identity" )
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-18
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多