объясняя, где среднее в легенде о boxplot в г - PullRequest
0 голосов
/ 24 февраля 2020

Я готовлю статью, которая должна точно показать, что представляет собой прямоугольник, то есть усы, IQR, выбросы и т. Д. c. Я нашел следующий код на веб-сайте Геологической службы США, чтобы создать легенду для объяснения участков. см .: https://waterdata.usgs.gov/blog/boxplots/. Однако мой собственный график также показывает среднее значение (которое не показано в версии USGS).

boxplot with mean value shown

Как я могу показать это в коде легенды? См. ниже.

# Create the legend:
# The main elements of the plot (the boxplot, error bars, and count)
# are the easy part.
# The text describing each of those takes a lot of fiddling to
# get the location and style just right:

explain_plot <- ggplot() +     
stat_boxplot(data = sample_df,                  
             aes(x = parameter, y=values),                  
             geom ='errorbar', width = 0) +     
geom_boxplot(data = sample_df,                  
             aes(x = parameter, y=values),                   
             width = 0.3, fill = "white") + 
geom_text(aes(x = 1, y = 950, label = "500"), hjust = 0.5) +     
geom_text(aes(x = 1.17, y = 950,                   
              label = "Number of values"),               
          fontface = "bold", vjust = 0.4) +     
theme_minimal(base_size = 5, base_family = family) +     
geom_segment(aes(x = 2.3, xend = 2.3,                       
                 y = ggplot_output[["25th percentile"]],                       
                 yend = ggplot_output[["75th percentile"]])) +     
geom_segment(aes(x = 1.2, xend = 2.3,                       
                 y = ggplot_output[["25th percentile"]],                       
                 yend = ggplot_output[["25th percentile"]])) +     
geom_segment(aes(x = 1.2, xend = 2.3,                       
                 y = ggplot_output[["75th percentile"]],                       
                 yend = ggplot_output[["75th percentile"]])) +     
geom_text(aes(x = 2.4, y = ggplot_output[["50th percentile\n(median)"]]),                
          label = "Interquartile\nrange", fontface = "bold",              
          vjust = 0.4) +     
geom_text(aes(x = c(1.17,1.17),                    
              y = c(ggplot_output[["upper_whisker"]],                         
                    ggplot_output[["lower_whisker"]]),                    
              label = c("Largest value within 1.5 times\ninterquartile range above\n75th percentile",                             
                        "Smallest value within 1.5 times\ninterquartile range below\n25th percentile")),                   
          fontface = "bold", vjust = 0.9) +     
geom_text(aes(x = c(1.17),                    
              y =  ggplot_output[["lower_dots"]],                    
              label = "Outlier"),                
          vjust = 0.5, fontface = "bold") +     
geom_text(aes(x = c(1.9),                    
              y =  ggplot_output[["lower_dots"]],                    
              label = "-Value is >1.5 times and"),                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             vjust = 0.5) +
geom_text(aes(x = 1.17, 
              y = ggplot_output[["lower_dots"]], 
              label = "<3 times the interquartile range\nbeyond either end of the box"), 
          vjust = 1.5) +
geom_label(aes(x = 1.17, y = ggplot_output[["quartiles"]], 
               label = names(ggplot_output[["quartiles"]])),
           vjust = c(0.4,0.85,0.4), 
           fill = "white", label.size = 0) +
ylab("") + xlab("") +
theme(axis.text = element_blank(),
      axis.ticks = element_blank(),
      panel.grid = element_blank(),
      aspect.ratio = 4/3,
      plot.title = element_text(hjust = 0.5, size = 10)) +
coord_cartesian(xlim = c(1.4,3.1), ylim = c(-600, 900)) +
labs(title = "EXPLANATION")
...