![](/img/trans.png)
[英]How do I add a legend to a ggplot with multiple groups that have multiple columns of data in a dataframe
[英]How to create groups with multiple columns in a dataframe
如何創建具有多列的組?
條形圖:如何創建組(使用列)以根據組為條形着色。
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"))
我有一個列名 A、B、C 等的數據框。我想將 A 和 B 分配到組 1; C、D、E 到第 2 組。這樣我就可以根據組給圖上色。
您可以利用 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')
由reprex 包(v0.2.1) 於 2019 年 4 月 30 日創建
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.