【问题标题】:Overlaying two scatter plots with different color scales in ggplot2在ggplot2中覆盖两个具有不同色标的散点图
【发布时间】:2018-09-30 13:06:54
【问题描述】:

我正在尝试在 ggplot2 中叠加两个散点图。目标是使点的外部根据一个变量(6个类别,因子)着色,内部填充另一个连续变量(数字)的渐变色。

我写了两段代码,每段都独立运行(请看下面的截图)。

    ggplot(PCA_isotopes_2, aes(x=PC1, y=PC2)) +
              theme_classic() +
              geom_point(aes(color = factor(subspecies)), shape = 1, size = 2.95, stroke=1, alpha=5/6) +
              scale_color_manual(breaks = c("gutturalis", "rg.hybrids", "rt", "rustica", "tg", "tytleri"), values=c("#0066CC", "#9933CC", "#FFCC99", "#CC0000", "#33CC99", "#FFFF00")) 

ggplot(PCA_isotopes_2, aes(x=PC1, y=PC2)) +
          theme_classic() +  
          geom_point(aes(color = carbon.ratio), size = 2.88, alpha=5/6) +
          scale_colour_gradient(low = "blue", high = "yellow")

当我尝试以这种方式覆盖它们时:

    p <- ggplot(PCA_isotopes_2, aes(x=PC1, y=PC2)) +
  theme_classic() +
  geom_point(aes(color = carbon.ratio), size = 2.88, alpha=5/6) +
  scale_colour_gradient(low = "blue", high = "yellow")

    p + geom_point(aes(color = factor(subspecies)), shape = 1, size = 2.95,         stroke=1, alpha=5/6) +
    scale_color_manual(breaks = c("gutturalis", "rg.hybrids", "rt", "rustica", "tg", "tytleri"), values=c("#0066CC", "#9933CC", "#FFCC99", "#CC0000", "#33CC99", "#FFFF00")) 

我收到错误消息:

“'color' 的比例已经存在。为 'color' 添加另一个比例,它 将取代现有的规模。 错误:提供给离散刻度的连续值”。

我花了几个小时试图弄清楚为什么它不起作用。我将非常感谢您的帮助!

谢谢, 乔治

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    一般而言,您只能映射一次美学。这是一种解决方法,它使用fill 美学作为连续变量的替代方法,使用shape = 21。但是,我更愿意完全映射到不同的审美,例如 shape,就像在第二个版本中一样。

    library(tidyverse)
    ggplot(iris, aes(x = Petal.Length, y = Petal.Width)) +
      theme_classic() +
      geom_point(
        mapping = aes(colour = Species),
        shape = 1,
        size = 3,
        stroke = 2,
        alpha = 5 / 6
        ) +
      geom_point(
        mapping = aes(fill = Sepal.Length, colour = NA),
        size = 2.88,
        alpha = 5 /6,
        shape = 21
      ) +
      scale_fill_gradient(low = "blue", high = "yellow")
    

    library(viridis)
    #> Loading required package: viridisLite
    ggplot(iris, aes(x = Petal.Length, y = Petal.Width)) +
      theme_classic() +
      geom_point(
        mapping = aes(colour = Sepal.Length, shape = Species),
        size = 3,
        alpha = 5 / 6
      ) +
      scale_colour_viridis()
    

    reprex package (v0.2.0) 于 2018 年 4 月 19 日创建。

    【讨论】:

    • 非常感谢,第一个例子正是我想要的。是否可以为第一个 geom_point 分配自定义颜色?
    • 是的,我把它们拿出来了,因为它看起来更难读,但欢迎你把 scale_colour_manual 放回去
    • 确实,第二个版本看起来更好。最后一个问题 - 如何在那里分配自定义形状?我试过这个:ggplot(PCA_isotopes_2, aes(x = PC1, y = PC2)) + theme_classic() + geom_point( mapping = aes(colour = carbon.ratio, shape = subspecies), size = 2, alpha = 5 / 6 , scale_shape_manual(breaks = c("gutturalis", "rg.hybrids", "rt", "rustica", "tg", "tytleri"), values = c(0, 1, 2, 5, 6, 3) ) ) + scale_colour_viridis() 并得到一个错误“ggplot2不知道如何处理ScaleDiscrete/Scale/ggproto类的数据”
    猜你喜欢
    • 1970-01-01
    • 2019-11-03
    • 2021-12-28
    • 2014-12-16
    • 1970-01-01
    • 2018-09-29
    • 2016-12-01
    • 1970-01-01
    • 2010-12-01
    相关资源
    最近更新 更多