r - 使用ggsave时遇到 "Error: $ operator is invalid for atomic vectors"

标签 r ggplot2 ggsave

<分区>

下面是我准备的代码作为示例。在我运行 ggsave 之前一切都很好。错误是关于 $ 运算符的,这很令人困惑。我见过很多人在不同的情况下都有类似的错误信息。是我的错还是内部 R 错误?

library(tidyverse)

xx <- seq(0,2,0.01); yy <- seq(0,2,0.02); zz <- seq(0,2,0.04)

dat <- data.frame(
  times = c(xx, yy, zz),
  val = c(sin(xx), sin(yy)+rnorm(length(yy),sd=0.2), sin(zz)+rnorm(length(zz),sd=0.3)),
  method = c(rep("A", length(xx)), rep("B", length(yy)), rep("C", length(zz)))
)

fig <- ggplot(dat) +
  geom_line(aes(x = times, y = val, col = method, linetype = method)) + 
  theme_minimal() +
  scale_color_manual(values = c("#29BDCE", "#ED8534", "#625559")) +
  scale_linetype_manual(values=c("solid", "longdash", "dashed")) +
  theme(legend.title = element_blank(),
        legend.position = "none") 

ggsave(fig, "D:/ResearchesAndProjects/2021_2_ODEadditive/code-ode-additive/examples/plots/test.png",
       device = "png", type ="cairo")
#> Saving 7 x 5 in image
#> Error: $ operator is invalid for atomic vectors
Created on 2021-07-01 by the reprex package (v2.0.0)

最佳答案

来自 ?ggsave 第一个参数是文件名,而第二个参数是绘图。尝试-

ggsave("D:/ResearchesAndProjects/2021_2_ODEadditive/code-ode-additive/examples/plots/test.png",
       fig, device = "png", type ="cairo")

关于r - 使用ggsave时遇到 "Error: $ operator is invalid for atomic vectors",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68204587/

相关文章:

python - 将文本文件解析为字典列表

r - 如何说服 ggplot2 geom_text 在时间序列图中标记指定日期?

r - 为ggplot2中的每个方面设置不同的轴限制,不使用尺度= "free"

python - 如何在plotnine中移动轴标签?

r - Extrafont和ggsave:字符最终在另一个字符之上

r - 3.3.6 更新 : cannot reproduce old plots 中 ggsave() 的重大更改

r - 如何在 roxygen 识字编程中转义 %?

r - 为什么在更新键列时删除键?

r - 使用 purrr::map (unlist, t, as_tibble) 在 R 中取消嵌套 JSON 时丢失名称