简体   繁体   中英

How to make a grouped bar plot

How do I get this in a grouped barplot? Satisfaction the (1,2,2.5,3) on the x axis and a bar for each (yes,no). Delaytable

no yes
1 1293 1706
2 11350 12237
2.5 0 2
3 25002 11982
satisfaction <- c(1,2,2.5,3)
no <- c(1293, 11350,0,25002)
yes <- c(1706, 12237, 2, 11982)

df <- data.frame(satisfaction, no, yes)
df

df_tidy <- df %>% pivot_longer(!satisfaction, names_to = "type", values_to = "count")

ggplot(df_tidy, aes(x = satisfaction, y = count, fill = type)) + 
  geom_col(position = "dodge")

在此处输入图像描述

In base R you could do:

df <- read.table(text="     no  yes
1   1293    1706
2   11350   12237
2.5     0   2
3   25002   11982")

barplot(do.call(rbind, asplit(df, 2)), las=1, beside=TRUE, legend.text = TRUE,
        args.legend = list(x="topleft"), xlab="Satisfaction")

Created on 2021-05-16 by the reprex package (v2.0.0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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