【问题标题】:plot from a dataframe containing lists as column values从包含列表作为列值的数据框中绘制
【发布时间】:2019-04-14 10:54:42
【问题描述】:

我一直在使用 purrr 包,现在遇到了一个独特的问题。我想绘制数据框,其中值是包含列内值的列表。

结构:

a b x           y
1 1 c(1,2,3,4) c(4,5,6,7)
1 2 c(1,2,3,4) c(5.4,6,6.5,7)

尝试的解决方案:

library("tidyverse")

# Define a named list of parameter values
temp = list(a = seq(1,5,1),
           b = seq(0.1,3,1)) %>% cross_df()

# create two new columns
x <- seq(1,5,0.1)
y <- 2.3 * x

# add these as a list
temp$x <- list(x)
temp$y <- list(y)

ggplot(data=temp, aes(x=unlist(x),y=unlist(y),color=a)) + 
  geom_point() + 
  ggtitle("Plot of Y vs. X shown by colour of a")

错误:

  1. 错误:美学必须是长度 1 或与数据 (15) 相同:x, y, colour 使用 unlist
  2. 错误:不使用 unlist 时将离散值提供给连续刻度

【问题讨论】:

    标签: r ggplot2 tibble


    【解决方案1】:

    您需要将列表元素分成单独的行,这可以使用unnest 完成

    library(tidyverse)
    
    temp %>%
      unnest() %>%
      ggplot() + 
      aes(x,y,color=a) + 
      geom_point() + 
      ggtitle("Plot of Y vs. X shown by colour of a")
    

    【讨论】:

    • 完美!非常感谢!
    猜你喜欢
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    • 2020-02-17
    • 1970-01-01
    • 2018-03-28
    • 1970-01-01
    • 2021-08-18
    • 2021-10-03
    相关资源
    最近更新 更多