R: barplot with n groups which stacks 2 values

我怕爱的太早我们不能终老 提交于 2019-12-12 03:28:18

问题


I need to generate a barplot which consists of the following:

I have a csv file with columns: Category Reason Time Value

  • Category has 7 possible names (c1 ... c7)
  • Reason has 8 possible names (n1 ... n8)
  • Time has 2 possible names (T1, T2)
  • Value is the time value

Example dataset:

Category    Reason  Time    Value
C1  R1  T1  1
C2  R1  T2  2
C1  R2  T1  3
C2  R2  T2  4
C1  R3  T1  8
C2  R3  T2  0

What i want to achieve: A bar plot which consists of 3 groups (i.e. one group per REASON), where each group consists of 2 stacked bars (i.e. a bar for each CATEGORY), where each bar depicts T1 and T2 on top of it.

I guess i need something similar as R: bar plot with two groups, of which one is stacked, but unfortunately i'm very new to R.

Similar to this picture, which has in terms of my example:

  • 5 categories
  • 3 reasons
  • 4 times values
  • % as a time value

Any help is appreciated.


回答1:


I am going to introduce you to the ggplot package in R which may give you a better way of visualizing your problem. Even if it does not solve your current problem, ggplot would be the easiest for starting your visualizations in R.

First the code:

library will load ggplot and scales package

library(ggplot2)
library(scales)

Generating your dummy data set

df = expand.grid(factor(c("C1","C2","C3","C4","C5","C6","C6","C8")),
                 factor(c("R1","R2","R3","R4","R5","R6","R7")),
                 factor(c("T1","T2")))

Plot with Category on x-axis, Value on y-axis, Time as the stacked bar which needs geom_bar(). Instead of grouping on the bar chart itself, ggplot can use facets, which produces cleaner results. scale_y_continuous() converts your y-axis to percent.

ggplot(data = df, aes(x=Reason, y=Value, fill = Time)) + 
  geom_bar(stat='identity') + 
  facet_wrap(~Category) +
  scale_y_continuous(labels = percent) +
  theme(panel.background = element_rect(fill = "white"))




回答2:


Would you mind sharing a version of your .csv file? Without that my guess looks something like this...

p_csv <- read.table(file.csv, header = T, sep = ",")

library(ggplot2)
#using mtcars 
ggplot(data = mtcars, aes(x = as.factor(cyl))) +
geom_bar(aes(fill = as.factor(gear)))

I"m happy help more if I can see some test data :)

Still not 100% on what you are trying to do with the time value, but this might be sufficient. I adjusted you sample data slightly so time and category wouldn't always be in sync.

df <- data.frame("category" = c("C1", "C2", "C1", "C2", "C1", "C2"),
             "reason" = c("R1", "R1", "R2", "R2", "R3", "R3"),
             "time" = c("T1", "T1", "T1", "T2", "T2", "T2"),
             "value" = c(1,2,3,4,8,0))

ggplot(data = df, aes(x = as.factor(reason), y = value)) +
geom_bar(aes(fill = as.factor(category)), stat = "identity") +
facet_grid(~time)


来源:https://stackoverflow.com/questions/37228134/r-barplot-with-n-groups-which-stacks-2-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!