简体   繁体   English

R:堆积条形图

[英]R: Stacked bar chart

I am trying to make a stacked bar chart with each mark in stacked bar corresponds to each row in the data. 我正在尝试制作一个堆积的条形图,堆积的条形图中的每个标记都对应于数据中的每一行。 Each bar corresponds to one hour thus contains 60 marks for each minute. 每个条对应一个小时,因此每分钟包含60个标记。 I think barplot function will be used but able to figure it out how? 我认为将使用barplot函数,但如何弄清楚呢? Also there's another column with some values. 另外还有一列包含一些值。 If there's value in that column I want that value in place of mark. 如果该列中有值,我希望该值代替标记。 Here's the data i am using 这是我正在使用的数据

RowNo   State   Bar
  1        a    1
  2             1
  3             1
  .
  .
 59             1
 60             1
 61             2
 62             2
 .
 .
 1199           20
 1200           20
 1201      c    21

And this is what I am looking for 这就是我想要的 在此处输入图片说明

Any Help would be appreciated. 任何帮助,将不胜感激。

Something along the line of this: 与此类似:

library(tidyverse)
data = expand.grid(list(row = 1:20, bar=1:20)) %>% 
        as_tibble() %>% 
        mutate(state = ifelse(row == 1 & bar == 1, "a", ifelse(row == 20 & bar == 1, "c", NA)),
               row = factor(row))

data  %>% 
    ggplot(aes(x=row, y=bar, fill=state)) + 
    geom_tile(colour="white", width=.6) + 
    geom_text(aes(label=state)) +
    scale_y_reverse()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM