我有要执行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
我想对每种饮食的价值分别做不同的图。
预先感谢
答案 0 :(得分:1)
您可以尝试以下方法:
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)
答案 1 :(得分:0)
在绘制/刻面之前,您要查找的是在该列中拆分diet
值吗?
那么tidyr::separate_rows
也许可以解决您的问题。
library(dplyr)
library(tidyr)
library(ggplot2)
df %>%
separate_rows(diet, sep = ',') %>%
ggplot() + ...
这会将提供的分隔符处的diet
列拆分为多行,同时将所有其他列的值复制到每个这样生成的行中。