【问题标题】:What does "attempt to replicate an object of type 'language'" mean?\"试图复制类型为\'语言\'\的对象"是什么意思?
【发布时间】:2023-02-22 16:39:18
【问题描述】:

我正在尝试使用 geom_pointgeom_segment() 在 ggplot 中绘制一个点和一条线段。复杂的是,线段在 x 轴上从 2019 年或 2020 年或 2021 年开始,具体取决于该年是否有数据。有数据的第一年(即,存在该年的 year 变量的行)是线段起点的水平位置。数据看起来像这样。 (实际上,有 43 个 network_id;我正在为每个 id 画一个点和一个线段。)

  network_id   intrcpt       slope interceptx  sim2019  sim2020   sim2021   
1          1 0.9008887 -0.05032728       2021 1.001543 0.951216 0.9008887
2          1 0.9008887 -0.05032728       2021 1.001543 0.951216 0.9008887
3          1 0.9008887 -0.05032728       2021 1.001543 0.951216 0.9008887
  sim2022   year    
1 0.8505614 2019 
2 0.8505614 2021 
3 0.8505614 2022 

我想我会这样绘制:

    resfil0 <- foo %>%
         group_by(network_id) %>%
         mutate(left.seg.y=ifelse(any(year==2019), quo(sim2019),
                                    ifelse(any(year==2020), quo(sim2020), quo(sim2021))),
                left.seg.x=ifelse(any(year==2019), 2019,
                                  ifelse(any(year==2020), 2020, 2021))
                )

ggplot(resfil0, aes(x=2021, y=intrcpt, label=network_id)) +
    geom_point() +
    geom_segment(aes(x=left.seg.x, y=!!left.seg.y, xend=2022, yend=sim2022)

但出于某种原因,我收到此错误消息:

Error in `mutate()`:
! Problem while computing `left.seg.end = ifelse(...)`.
i The error occurred in group 1: network_id = 1.
Caused by error in `rep()`:
! attempt to replicate an object of type 'language'

我不知道这意味着什么,问题是什么,或者如何解决它。有人可以帮忙吗?

【问题讨论】:

  • 请提供您的测试数据作为dput 的输出。你为什么quo你的列名?这看起来很奇怪。您的数据框不是 tidy,因为您的列名包含绘图所需的信息(年份)。 ggplot 旨在处理整洁的数据。这样做可能有助于解决您的问题。
  • 是的,我不知道为什么我认为我需要把列名放在 quosures 中。将列名放在 mutate 语句中效果很好。只是让它变得比需要的更复杂。感谢@Limey 的建议。

标签: r tidyverse


【解决方案1】:

除了mutate中的quo可以省略,aes中的!!也可以省略。 ggplot 可以直接访问提供的data.frame 中的变量,不需要弄得比现在更复杂。

foo <- read.table(text = "network_id   intrcpt       slope interceptx  sim2019  sim2020   sim2021   sim2022   year
1          1 0.9008887 -0.05032728       2021 1.001543 0.951216 0.9008887  0.8505614 2019 
2          1 0.9008887 -0.05032728       2021 1.001543 0.951216 0.9008887  0.8505614 2019
3          1 0.9008887 -0.05032728       2021 1.001543 0.951216 0.9008887  0.8505614 2022")

library(dplyr)
library(ggplot2)

resfil0 <- foo %>%
  group_by(network_id) %>%
  mutate(left.seg.y=ifelse(any(year==2019), sim2019,
                           ifelse(any(year==2020), sim2020, sim2021)),
         left.seg.x=ifelse(any(year==2019), 2019,
                           ifelse(any(year==2020), 2020, 2021))
  )

ggplot(resfil0, aes(x=2021, y=intrcpt, label=network_id)) +
  geom_point() +
  geom_segment(aes(x=left.seg.x, y=left.seg.y, xend=2022, yend=sim2022))

【讨论】:

    猜你喜欢
    • 2014-05-22
    • 2015-09-06
    • 2011-07-10
    • 2022-01-08
    • 1970-01-01
    • 2014-02-01
    • 2015-05-09
    • 2020-06-29
    相关资源
    最近更新 更多