【问题标题】:r ggplot when two colors overlap两种颜色重叠时的r ggplot
【发布时间】:2020-06-09 20:57:28
【问题描述】:

我有一些代码可以生成绘图,唯一的问题是有很多重叠的颜色。

当两种颜色重叠时,如何指定主色? 例如,当指标 = 阈值时有 4 个黑点。它们相应地位于 4 x 轴。但是,“Wire”和“ACH”刻度处的黑点不显示,因为它与蓝点重叠。 “RDFI”刻度上的黑点几乎没有出现。当两种颜色重叠时,如何使黑色作为主色?先谢谢了!

ggplot(df, aes(a-axis, y-axis), color=indicator)) + 
  geom_quasirandom(groupOnX=TRUE, na.rm = TRUE) +
  labs(title= 'chart', x='x-axis', y= 'y-axis') +
  scale_color_manual(name = 'indicator', values=c("#99ccff","#000000" )) 

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    要指定主色,您应该使用函数 new_scale () 及其别名 new_scale_color () 和 new_scale_fill ()。

    作为一个例子,让我们使用 beloed volcano 在地形等高线图上叠加一些测量值

    library(ggplot2)
    library(ggnewscale)
    # Equivalent to melt(volcano)
    topography <- expand.grid(x = 1:nrow(volcano),
                              y = 1:ncol(volcano))
    topography$z <- c(volcano)
    
    # point measurements of something at a few locations
    set.seed(42)
    measurements <- data.frame(x = runif(30, 1, 80),
                               y = runif(30, 1, 60),
                               thing = rnorm(30))
    
    

    优势点:

    
    ggplot(mapping = aes(x, y)) +
      geom_contour(data = topography, aes(z = z, color = stat(level))) +
      # Color scale for topography
      scale_color_viridis_c(option = "D") +
      # geoms below will use another color scale
      new_scale_color() +
      geom_point(data = measurements, size = 3, aes(color = thing)) +
      # Color scale applied to geoms added after new_scale_color()
      scale_color_viridis_c(option = "A")
    

    主要轮廓:

    ggplot(mapping = aes(x, y)) +
      geom_point(data = measurements, size = 3, aes(color = thing)) +
      scale_color_viridis_c(option = "A")+
      new_scale_color() +
      geom_contour(data = topography, aes(z = z, color = stat(level))) +
      scale_color_viridis_c(option = "D") 
    

    【讨论】:

    • 感谢您的回答!我将您的代码放入 R 并运行它。但我对 R 比较陌生。你能根据我的代码进行修改吗?我知道我没有可复制的数据,所以伪代码很好。
    【解决方案2】:

    您的问题可能不在于哪种颜色占主导地位。您选择了经常出现的颜色。您可能会丢失 Y 轴的底部。您在示例中拥有的代码不可能产生它有错误的情节。

    这是一个简单的示例,它显示了一种解决问题的方法,方法是在绘制蜂群后简单地过度绘制阈值点。

    library(dplyr)
    library(ggbeeswarm)
    
    distro <- data.frame(
    'variable'=rep(c('runif','rnorm'),each=1000),
    'value'=c(runif(2000, min=-3, max=3))
    )
    
    distro$indicator <- "NA"
    distro[3,3] <- "Threshhold"
    distro[163,3] <- "Threshhold"
    
    
    
    ggplot2::ggplot(distro,aes(variable, value, color=indicator)) + 
      geom_quasirandom(groupOnX=TRUE, na.rm = TRUE, width=0.1) +
      scale_color_manual(name = 'indicator', values=c("#99ccff","#000000")) +
      geom_point(data = distro %>% filter(indicator == "Threshhold"))
    

    【讨论】:

      【解决方案3】:

      您根据颜色变量(您的指标)对数据进行排序。

      基本上你希望你的黑点最后绘制=在其他点之上。

      df$indicator <- sort(df$indicator, decreasing=T)
      
      #Tidyverse solution
      
      df <- df %>% arrange(desc(indicator))
      

      取决于您的级别,您可能需要或不进行反向排序。

      那你就画吧。

      pd <- tibble(x=rnorm(1000), y=1, indicator=sample(c("A","B"), replace=T, size = 1000))
      ggplot(pd, aes(x=x,y=y,color=indicator)) + geom_point()
      
      pd <- pd %>% arrange(indicator)
      ggplot(pd, aes(x=x,y=y,color=indicator)) + geom_point()
      
      pd <- pd %>% arrange(desc(indicator))
      ggplot(pd, aes(x=x,y=y,color=indicator)) + geom_point()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-31
        • 1970-01-01
        • 2022-01-13
        • 2016-01-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多