【问题标题】:How merge two different scale color gradient with ggplot如何将两种不同比例的颜色渐变与ggplot合并
【发布时间】:2020-02-10 05:33:31
【问题描述】:

通过使用 R,是否可以将 2 个 ggplot 放在一起(即在同一个图上)但颜色渐变条不同?我的代码,例如,

library(ggplot2)
ggplot(df1, aes(duration, slopes, col = color)) +
 geom_point(size = 3) +
 scale_color_gradient(low = "black", high = "red")
ggplot(df2, aes(duration, slopes, col = color)) +
 geom_point(size = 3) +
 scale_color_gradient(low = "blue", high = "green")

产生以下两张图片

我希望能够将它们整合到一个图中,一个红色和黑色的条形图和另一个蓝色和绿色的条形图。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    是的,如果您使用 ggnewscale 包,您可以:

    a <- sample(nrow(iris), 75)
    
    df1 <- iris[a,]
    df2 <- iris[-a,]
    
    library(ggnewscale)
    
    ggplot(mapping = aes(Sepal.Width, Sepal.Length)) +
      geom_point(data = df1, aes(colour = Petal.Length)) +
      scale_colour_gradientn(colours = c("red", "black")) +
      # Important: define a colour/fill scale before calling a new_scale_* function
      new_scale_colour() +
      geom_point(data = df2, aes(colour = Petal.Width)) +
      scale_colour_gradientn(colours = c("blue", "white"))
    

    替代方案是 relayer 包,或来自 ggh4xscale_colour_multi/scale_listed(完全免责声明:我写了 ggh4x)。

    编辑:以下是替代方案:

    library(ggh4x)
    
    # ggh4x scale_colour_multi (for gradientn-like scales)
    ggplot(mapping = aes(Sepal.Width, Sepal.Length)) +
      geom_point(data = df1, aes(length = Petal.Length)) +
      geom_point(data = df2, aes(width = Petal.Width)) +
      scale_colour_multi(colours = list(c("red", "black"), c("blue", "white")),
                         aesthetics = c("length", "width"))
    
    # ggh4x scale_listed (for any non-position scale (in theory))
    ggplot(mapping = aes(Sepal.Width, Sepal.Length)) +
      geom_point(data = df1, aes(length = Petal.Length)) +
      geom_point(data = df2, aes(width = Petal.Width)) +
      scale_listed(list(
        scale_colour_gradientn(colours = c("red", "black"), aesthetics = "length"),
        scale_colour_gradientn(colours = c("blue", "white"), aesthetics = "width")
      ), replaces = c("colour", "colour"))
    
    
    library(relayer)
    
    # relayer
    ggplot(mapping = aes(Sepal.Width, Sepal.Length)) +
      rename_geom_aes(geom_point(data = df1, aes(length = Petal.Length)), 
                      new_aes = c("colour" = "length")) +
      rename_geom_aes(geom_point(data = df2, aes(width = Petal.Width)),
                      new_aes = c("colour" = "width")) +
      scale_colour_gradientn(colours = c("red", "black"), aesthetics = "length", 
                             guide = guide_colourbar(available_aes = "length")) +
      scale_colour_gradientn(colours = c("blue", "white"), aesthetics = "width", 
                             guide = guide_colourbar(available_aes = "width"))
    

    所有替代方案都会发出关于未知美学的警告,但这对于生成的情节无关紧要。它只是 ggplot 的 layer() 函数中的一行代码产生了这个警告,如果不重新编码每个 geom 包装器,或者像 ggnewscale 那样重命名旧美学而不是提供新的审美的。这些情节看起来几乎相同,所以我想我不必再发布它们了。

    【讨论】:

    • 不错的答案(+1);出于好奇,您介意使用ggnomics 添加匹配解决方案吗?
    • 我已经发布了使用替代方案的匹配解决方案
    猜你喜欢
    • 2012-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-07
    • 1970-01-01
    • 2020-12-31
    • 1970-01-01
    相关资源
    最近更新 更多