删除异常值并为 ggplot2 中的每个方面适当减少 yLim

rsh*_*hah 1 r ggplot2 boxplot

我目前正在使用 制作一个ggplot2多面体图,我已经清除了异常值并将 yLim 设置为 5000。

但是,并非所有箱线图(下图开头的箱线图)都接近 5000。如何仅减少图像中选定的少数箱线图的 y 轴?我已经尝试了来自社区的多个答案,但它们似乎已经过时了。

在此处输入图片说明

这是我正在使用的代码:

require(reshape2)
require(ggplot2)

data_frame <- read.csv("results.csv", header=T)

p <- ggplot(data=data_frame, aes(x='', y=value)) + geom_boxplot(outlier.shape=NA, aes(fill=policy))
p <- p + facet_wrap( ~ level, scales="free") + coord_cartesian(ylim = c(0, 5000))
p <- p + xlab("") + ylab("Authorisation Time (ms)") + ggtitle("Title")
ggsave("bplots.png", plot=last_plot(), device=png())
Run Code Online (Sandbox Code Playgroud)

And*_*ter 5

如上所述,您几乎必须在绘图之前进行过滤,但这不需要通过编辑任何文件甚至通过创建新数据框来完成。使用dplyr您可以将其链接到数据处理中。我已经用一些虚构的数据做了一个希望可重复的例子(因为我没有你的)。我创建了一个函数,通过与箱线图使用的过程相同的过程进行过滤。这有点hacky,但希望可以作为一种潜在的解决方案:

require(ggplot2)
require(dplyr)

data_frame <- data.frame(value = c(rnorm(2000, mean = 100, sd = 20), rnorm(2000, mean = 1000, sd = 500)),
           level = c(rep(1,2000), rep(2, 2000)),
           policy = factor(c(rep(c(rep(1, 500), rep(2, 500), rep(3, 500), rep(4, 500)), 2))))

# filtering function - turns outliers into NAs to be removed
filter_lims <- function(x){
  l <- boxplot.stats(x)$stats[1]
  u <- boxplot.stats(x)$stats[5]

  for (i in 1:length(x)){
    x[i] <- ifelse(x[i]>l & x[i]<u, x[i], NA)
  }
  return(x)
}

data_frame %>% 
  group_by(level, policy) %>%  # do the same calcs for each box
  mutate(value2 = filter_lims(value)) %>%  # new variable (value2) so as not to displace first one)
  ggplot(aes(x='', y=value2, fill = policy)) + 
  geom_boxplot(na.rm = TRUE, coef = 5) +  # remove NAs, and set the whisker length to all included points
  facet_wrap( ~ level, scales="free") + 
  xlab("") + ylab("Authorisation Time (ms)") + ggtitle("Title")
Run Code Online (Sandbox Code Playgroud)

产生以下(简化的)图:

来自合成数据的图表