apply jittering to outliers data in a boxplot with ggplot2

后端 未结 2 1839
醉酒成梦
醉酒成梦 2021-02-14 07:50

do you have any idea of how to apply jittering just to the outliers data of a boxplot? This is the code:

ggplot(data = a, aes(x = \"\", y = a$V8)) +
geom_boxplot         


        
2条回答
  •  天命终不由人
    2021-02-14 08:10

    Added a vector to your data set to indicate which points are and are not outliers. Then, Set the geom_boxplot to not plot any outliers and use a geom_point to plot the outliers explicity.

    I will use the diamonds data set from ggplot2 to illustrate.

    library(ggplot2)
    library(dplyr)
    
    diamonds2 <-
      diamonds %>%
      group_by(cut) %>%
      mutate(outlier = price > median(price) + IQR(price) * 1.5) %>%
      ungroup
    
    ggplot(diamonds2) +
      aes(x = cut, y = price) +
      geom_boxplot(outlier.shape = NA) +  # NO OUTLIERS
      geom_point(data = function(x) dplyr::filter_(x, ~ outlier), position = 'jitter') # Outliers
    

提交回复
热议问题