在R中使用ggplot2可视化数据的强大方法或建议

cnwbcb6i  于 2023-04-09  发布在  其他
关注(0)|答案(1)|浏览(85)

我想使用ggplot2在R中可视化我的数据。数据集有Group是参与团队,locat是他们竞争的地方,以及他们失败的原因(%)。我试图可视化Group,locat和reason_type1到reason_type4。该图需要通过locat显示每个团队的fail_day原因组。我首先尝试在excel中简单绘图。

但我想group_by每个组,例如team1颜色由locat和显示4原因。下面是我的数据集看起来像:

df <- data.frame(Group = c(rep(paste0('team',1:7), each = 3), 'team7'),
                 locat= c(rep(c("AL","MD","OK"), 7), "NY"),
                 Days =c(653 ,855 ,232 ,321 ,796 ,818 ,636 ,701 ,799 ,559 ,5 ,905 ,861 ,2 ,1425 ,1131 ,192 ,95 ,380 ,542 ,610, 799),
                 survial_days=c(651 ,852 ,230 ,266 ,392 ,767 ,579 ,491 ,734 ,503 ,1 ,846 ,405 ,2 ,1133 ,822 ,192 ,94 ,379 ,452 ,587, 701),
                 fail_days=c(2 ,3 ,2 ,55 ,404 ,51 ,57 ,210 ,65 ,56 ,4 ,59 ,456 ,0 ,292 ,309 ,0 ,1 ,1 ,90 ,23, 98),
                 fail_P=c(0.31 ,0.35 ,0.86 ,17.13 ,50.75 ,6.23 ,8.96 ,29.96 ,8.14 ,10.02 ,80.00 ,6.52 ,52.96 ,0.00 ,20.49 ,27.32 ,0.00 ,1.05 ,0.26 ,16.61 ,3.77, 12.26),
                 reason_type1=c(0 ,0 ,0 ,0 ,6.56 ,0 ,0 ,0 ,0 ,0 ,0 ,0.01 ,26.83 ,0 ,20 ,4.69 ,0 ,0 ,0 ,1.51 ,0, 2.3),
                 reason_type2=c(0 ,0 ,0 ,0 ,0 ,0 ,5.5 ,0 ,7.51 ,0 ,0 ,1.51 ,0 ,0 ,0 ,8.4 ,0 ,0 ,0 ,3.79 ,0, 0),
                 reason_type3=c(0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0.31 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0, 0.8),
                 reason_type4=c(0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0.77 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0, 0))

我真的很感激这里的一些帮助。我如何重组我的情节或任何建议如何显示我的数据更形象?任何帮助都非常感谢。谢谢。

iqjalb3h

iqjalb3h1#

一种方法可以是:

library(tidyverse)

df %>% 
  dplyr::select(Group, locat, starts_with("reason")) %>% 
  pivot_longer(starts_with("reason")) %>% 
  ggplot(aes(x = locat, y=value, fill=name))+
  geom_col(position = position_dodge())+
  facet_wrap(~ Group) + 
  labs(title="Fail Days by Reason Type and Location", x="Location", y="Fail Days") +
  theme_bw()

相关问题