如何将 facet_wrap(~list) 与列表一起使用?
how to use the facet_wrap(~list) with a list?
我有数据正在执行 EDA。
其中一列包含字符列表。
我想对列表的每个唯一值而不是整个列表使用 facet_wrap
。
我该怎么做?
Exp:
heights diet
14.3 cw,ce,la
12.3 cw
11.2 ap, la
10.0 lp
12.3 ce,cw
我想分别对饮食的每个值做不同的绘图。
提前致谢
您要在绘图/分面之前拆分该列中的 diet
值吗?
那么 tidyr::separate_rows
可能是您问题的解决方案。
library(dplyr)
library(tidyr)
library(ggplot2)
df %>%
separate_rows(diet, sep = ',') %>%
ggplot() + ...
这会将提供的分隔符处的 diet
列拆分为多行,同时将所有其他列值复制到每个这样生成的行中。
你可以试试这个:
library(tidyverse)
library(tidyr)
#Data
df <- structure(list(heights = c(14.3, 12.3, 11.2, 10, 12.3), diet = c("cw,ce,la",
"cw", "ap, la", "lp", "ce,cw"), z = c(-0.940250088024965, -0.916085008989365,
0.346802165714023, 0.416379800831458, -0.284455862566136)), row.names = c(NA,
-5L), class = "data.frame")
#Separate cols, we place 3 because the largest number of diets is 3 in first row
#If you have more you would have to expand such quantity
df2 <- separate(df,col = diet,into = paste0('diet',1:3),sep = ',')
#Pivot
df3 <- pivot_longer(df2,cols = names(df2)[which(grepl('diet',names(df2)))])
#Prepare plot
df4 <- df3 %>% mutate(value=trimws(value)) %>% filter(!is.na(value))
#Plot
ggplot(df4,aes(x=heights,y=z))+
geom_point()+
facet_wrap(.~value)
我有数据正在执行 EDA。
其中一列包含字符列表。
我想对列表的每个唯一值而不是整个列表使用 facet_wrap
。
我该怎么做?
Exp:
heights diet
14.3 cw,ce,la
12.3 cw
11.2 ap, la
10.0 lp
12.3 ce,cw
我想分别对饮食的每个值做不同的绘图。
提前致谢
您要在绘图/分面之前拆分该列中的 diet
值吗?
那么 tidyr::separate_rows
可能是您问题的解决方案。
library(dplyr)
library(tidyr)
library(ggplot2)
df %>%
separate_rows(diet, sep = ',') %>%
ggplot() + ...
这会将提供的分隔符处的 diet
列拆分为多行,同时将所有其他列值复制到每个这样生成的行中。
你可以试试这个:
library(tidyverse)
library(tidyr)
#Data
df <- structure(list(heights = c(14.3, 12.3, 11.2, 10, 12.3), diet = c("cw,ce,la",
"cw", "ap, la", "lp", "ce,cw"), z = c(-0.940250088024965, -0.916085008989365,
0.346802165714023, 0.416379800831458, -0.284455862566136)), row.names = c(NA,
-5L), class = "data.frame")
#Separate cols, we place 3 because the largest number of diets is 3 in first row
#If you have more you would have to expand such quantity
df2 <- separate(df,col = diet,into = paste0('diet',1:3),sep = ',')
#Pivot
df3 <- pivot_longer(df2,cols = names(df2)[which(grepl('diet',names(df2)))])
#Prepare plot
df4 <- df3 %>% mutate(value=trimws(value)) %>% filter(!is.na(value))
#Plot
ggplot(df4,aes(x=heights,y=z))+
geom_point()+
facet_wrap(.~value)