r - 将多个图合并为循环颜色,每个图按数据框中的不同列进行着色

标签 r ggplot2

我正在尝试根据thread组合多个图。这里我的问题是我想根据具有不同值的彩色图进行分离,任何人都可以帮助我缺少什么,(注意:我的原始数据有大约 30 个列值)

data(iris)
list_data=c("Petal.Length", "Petal.Width")
# Make plots.
plot_list = list()
for (i in list_data) {
  p = ggplot(iris, aes(y=Sepal.Length, x=Sepal.Width)) +
    geom_point(size=3, aes(colour=i))
  plot_list[[i]] = p
}

pdf("plots.pdf")
for (i in list_data) {
  print(plot_list[[i]])
}

dev.off()

最佳答案

我们可以在颜色中使用.data[[i]]

for (i in list_data) {
  p = ggplot(iris, aes(y=Sepal.Length, x=Sepal.Width)) +
    geom_point(size=3, aes(colour=.data[[i]]))
  plot_list[[i]] = p
}

-输出

> plot_list[[1]]

enter image description here

关于r - 将多个图合并为循环颜色,每个图按数据框中的不同列进行着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75114227/

相关文章:

r - 使 R 返回访问全局的错误

rbindlist 用于缺少水平的因子

r - 无法使用scale_x_discrete向我的ggplot的x轴添加标签

r - 扩大 x Axis 上刻度线之间的间距

r - R 中带有字符串的 geom_smooth

R 在函数内使用临时选项设置

R 使用 Rmarkdown/tufte_handout 产生全宽

r - 在事先不知道变量名的情况下访问 R6 类变量?

r - 在 R 中按值传递

r - 使用 ggplot 固定填充密度图的不同部分