【问题标题】:Color outlier dots above a specific value in RR中特定值上方的颜色异常点
【发布时间】:2020-12-12 09:43:35
【问题描述】:

如何在 R 中使用 ggplot2 为高于特定值的异常值着色?

(对不起,这个看似简单的问题,我是初学者。原因是这些是值为 0 的频率,然后我通过采用 -log10() 来转换这一列数据。所以任何有然后将频率为 0 转换为 Inf。附件是我的绘图的屏幕截图,本质上我想让 y 轴上 10 以上的所有异常点都变成不同的颜色。

boxplots <- function(df){
    
    df$'frequency'[is.na(df$'frequency')] <- 0.00
    
    df$'-log10(frequency)' <- -log10(df$'frequency')
    
    x <- data.frame(group = 'x', value = df$'-log10(frequency)'[df$'Type'=='x'])
    y <- data.frame(group = 'y', value = df$'-log10(frequency)'[df$'Type'=='y'])
    z <- data.frame(group = 'z', value = df$'-log10(frequency)'[df$'Type'=='c=z'])
    
    plot.data <<- rbind(x, y, z)
    
    
    labels <- c("z", "y", "z")
    
   t<-plot.data %>%
        ggplot(aes(x = group, y = value, fill = group))+
        geom_boxplot()+
        scale_fill_viridis(discrete = TRUE, alpha = 0.6)+
        geom_jitter(color="black", size=0.4, alpha=0.9) +
        theme_ipsum() +
        theme(
          legend.position="none",
          plot.title = element_text(size=11)
        ) +
        ggtitle("Distribution of -log10(frequency) by Type") +
        xlab("Type")+
        ylab("-log10(frequency)")+
        scale_x_discrete(labels=labels)+
        scale_y_continuous(limits = c(0, 10), breaks = seq(0, 10, by = 2))
    
    print(t)


    s<<-t
    

    ggsave("frequency_by_type.png", plot = t) 
}

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    您可以创建一个新列来指示它是否是异常值并将其映射到 geom_jitter 颜色。我在一个较小的示例中恢复了答案,但您应该能够相应地适应它:

    library(ggplot2)
    library(viridis)
    
    plot.data <- data.frame(group = c("1","1","1","1","1","2","2","2","2","2"),
                            value = c(1,5,10,6,3,1,5,10,6,3))
    
    t<-plot.data %>%
      mutate(outlier = ifelse(value >9, "YES", "NO")) %>% 
      ggplot(aes(x = group, y = value, fill = group))+
      geom_boxplot()+
      geom_jitter(aes(group, value, color = outlier) , size=2, alpha=0.9)+
      scale_fill_viridis(discrete = TRUE, alpha = 0.6)
    
    t
    

    【讨论】:

      【解决方案2】:
      library(ggplot2)
      # Basic box plot
      p <- ggplot(ToothGrowth, aes(x=dose, y=len)) + 
        geom_boxplot()
      p
      # Rotate the box plot
      p + coord_flip()
      # Notched box plot
      ggplot(ToothGrowth, aes(x=dose, y=len)) + 
        geom_boxplot(notch=TRUE)
      # Change outlier, color, shape and size
      ggplot(ToothGrowth, aes(x=dose, y=len)) + 
        geom_boxplot(outlier.colour="red", outlier.shape=8,
                      outlier.size=4)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-06
        • 2012-12-09
        • 1970-01-01
        • 1970-01-01
        • 2020-10-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多