代码之家  ›  专栏  ›  技术社区  ›  marc

ggplot中的两个不同颜色键

  •  0
  • marc  · 技术社区  · 6 年前

    在这里的示例中,5个点被淹没,颜色被映射到两个协变量(cov1和cov2):cov1和cov2分别处于不同的尺度1到5和0.01到0.05。

    我希望有两个独立的颜色键,一个用于cov1,一个用于cov2, 有点像下图。然而,在下面的图中,我使用了“color=cov1”end“fill=cov2”以带来另一个颜色键。。。

    任何帮助都将不胜感激。

       gg1 <- ggplot(data = df1 , aes( x = x , y = y ) ) +
       geom_point( aes(x = x , y = y - 1 , color = cov1 ))  +
       geom_point( aes(x = x , y = y + 1 , color =  cov2  )) +
       scale_y_continuous(limits = c(-3,3)) 
    
      gg2 <- ggplot(data = df1 , aes( x = x , y = y ) ) +
      geom_point( aes(x = x , y = y - 1 , color = cov1 ))  +
      geom_point( aes(x = x , y = y + 1 , fill =  cov2  ), pch = 21 ) +
      scale_y_continuous(limits = c(-3,3)) 
    
    grid.arrange( gg1 , gg2 , ncol = 2 )
    

    enter image description here

    0 回复  |  直到 6 年前
        1
  •  6
  •   NelsonGon phoxis    6 年前

    在basic中 ggplot2 如果我记得正确,那是不可能的。但这个存储库可能是您的答案:

    https://github.com/eliocamp/ggnewscale

    https://github.com/clauswilke/relayer

    ggplot2图 很长一段时间以来,我对这两个词都不熟悉,但我记得我至少用过一次。

    d1 <- data.frame(x=1:5, y=1)
    d2 <- data.frame(x=1:5, y=2)
    
    library(ggplot2)
    library(ggnewscale)
    
    ggplot() +
      geom_point(data = d1, aes(x=x, y=y, color = x)) +
      scale_color_continuous(low = "#0000aa", high="#ffffff") +
      new_scale_color() +
      geom_point(data = d2, aes(x=x, y=y, color = x)) +
      scale_color_continuous(low = "#aa0000", high="#00aa00") 
    

    example

    它似乎能按你的意愿工作。

        2
  •  2
  •   Adela    6 年前

    我用了你关于组合的想法 col fill 和小黑客使用不同的形状 cov1 cov2

    # sample data
    my_data <- data.frame(x = 1:5,
                          cov1 = 1:5,
                          cov2 = seq(0.01, 0.05, 0.01))
    
    library(ggplot2)
    
    ggplot() + 
      geom_point(data = my_data, aes(x = x, y = 0.5, col = cov1), shape = 16) +
      scale_color_continuous(low = "red1", high = "red4") + 
      geom_point(data = my_data, aes(x = x, y = -0.5, fill = cov2), shape = 21, col = "white", size = 2) + 
      ylim(-1, 1)
    

    enter image description here

    希望有帮助。

    推荐文章