简体   繁体   English

如何在数据框中创建具有多列的组

[英]How to create groups with multiple columns in a dataframe

How to create groups with multiple columns?如何创建具有多列的组?

Bar plot: How do I create groups(using columns) to color the bars according to groups.条形图:如何创建组(使用列)以根据组为条形着色。

structure(list(DH105 = c(0.95238, 0.8922, 
0.8232, 0.2323), DH106 = c(0, 0.5327, 
0.5337, 0.8232), DH107 = c(0.2736, 
0.2321, 0.7382, 0.8923), 
    DH108 = c(0.2332, 0, 0, 0.3213), 
    DH112 = c(0.0315, 0.2639, 0.0321, 
    0.2673), DH113 = c(0.2372, 0.2871, 
    0.7222, 0)), row.names = c(NA, -4L), class = c("tbl_df", 
"tbl", "data.frame"))

I have a data frame with column names A, B, C etc. I want to assign A and B to group 1;我有一个列名 A、B、C 等的数据框。我想将 A 和 B 分配到组 1; C, D, E to group 2. So that I can color the plots according to group. C、D、E 到第 2 组。这样我就可以根据组给图上色。

You can leverage tidyverse to gather the data and then categorize it into groups:您可以利用 tidyverse 收集数据,然后将其分类:

library(tidyverse)

dat <- structure(list(A = c(0.9523, 0.06944, 
                     0.53061, 0.11111, 0.03125, 0.64794, 
                     0.10763, 0.02782, 0.0374149659863946, 0.8439), 
               B = c(0, 0.2378, 0.0068, 0.8328, 0.7292, 0.7539, 0.7439, 0.0742, 0.5272, 0.6822), 
               C = c(0.0273, 0.0901, 0.7778, 0.9462, 0.5327, 0.2744, 0.5327, 0.4262, 0.6821, 0.03125), 
               D = c(0.0297, 0, 0, 0.03462, 0.0272, 0.0325, 0.6282, 0.6282, 0.6329, 0.8925), 
               E = c(0.0325, 0.0829, 0.6328, 0.5237, 0.5722, 0.7283, 0.6382, 0.5637, 0.5632, 0.0532)), 
row.names = c(NA, -10L), class = c("tbl", "data.frame"))

dat_clean <- dat %>% 
  gather(key = "col", value = "value") %>% 
  mutate(group = case_when(
    col %in% c("A","B") ~ "group1",
    col %in% c("C", "D", "E") ~ "group2"
  ))

ggplot(dat_clean, aes(x = col, y = value, fill = group)) + 
  geom_bar(stat = 'identity') + 
  theme(legend.position = 'None')

Created on 2019-04-30 by the reprex package (v0.2.1)reprex 包(v0.2.1) 于 2019 年 4 月 30 日创建

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

相关问题 如何将图例添加到具有多个组的 ggplot 中,这些组在 dataframe 中具有多列数据 - How do I add a legend to a ggplot with multiple groups that have multiple columns of data in a dataframe 在数据框上创建多列应用 - create multiple columns with apply on a dataframe 如何基于以特定前缀开头并包含特定字符串的列组创建多个新列? - How to create multiple new columns based of off groups of columns that start with a certain prefix and also contain a certain string? 按组平均数据框中的列 - Average columns in a dataframe by groups 如何从单个数据框的列自动创建多个数据框? - How to automatically create multiple dataframes from columns of a single dataframe? 如何从数据框和cbind()的一列创建多列到数据框 - How to create multiple columns from a column in data frame and cbind() to dataframe 如何跨数据框列创建具有多个if条件的循环? - How do I create a loop with multiple if criteria across dataframe columns? 如何从 R 中的多列创建合并值的新数据框 - How to a create a new dataframe of consolidated values from multiple columns in R 如何通过实现一些查询条件在r dataframe中创建多列 - How to create multiple columns in r dataframe by implementing some query conditions 如何创建两组数据框的所有组合? - How to create two groups of all combinations of a dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM