【问题标题】:Excluding outliers when plotting a Stripchart with ggplot2使用 ggplot2 绘制条形图时排除异常值
【发布时间】:2018-01-12 22:50:16
【问题描述】:

我正在尝试创建组合箱线图/散点图。到目前为止,我做得很好,但有一个问题让我非常困扰,我一直无法弄清楚。我在 R 中,我已经安装了 ggplot2 包。这是我正在使用的代码:

  #(xx= stand in for my data set, which I imported from excel with the 
      #  column labels as the X-axis values)
  > boxplot(xx, lwd = 1.5, ylab = 'Minutes', xlab = "Epoch")
  > stripchart(xx, vertical = TRUE, 
  +            method = "jitter", add = TRUE, pch = 20, col = 'blue')

这给了我一个非常接近我想要的图,但问题是异常值在图表上放置了两次。如果可能的话,我想让条形图排除它们(最高的蓝点组),并且只使用箱线图中的那些(黑色轮廓圆圈),这样它们就可以脱颖而出并且看起来不那么草率。

我试图通过将许多不同的异常值参数放入 stripchart 命令来改变有问题的点,不幸的是没有运气。我尝试将 y-limits 设置为低于其值,尝试使用 outline=false(它完全删除了条形图),尝试更改异常颜色、outpch 等。该命令对任何这些尝试都不起作用。下面是一个 ylim 的例子:

 > stripchart(xx, vertical = TRUE, 
+       method = "jitter", add = TRUE, pch = 20, col = 'blue', ylim = true, 
ylim (0,20))

ylim(0, 20) 中的错误:找不到函数“ylim”

这里有一个异常颜色的例子:

> stripchart(xx vertical = TRUE, 
+   method = "jitter", add = TRUE, pch = 20, col = 'blue', outcol = "black")

警告信息:
1:在 plot.xy(xy.coords(x, y), type = type, ...) 中:“outcol”不是 图形参数
.......# 警告消息照此继续。

条形图是否能够排除异常值?还是我对它们(以及整个 R,就此而言)还不够了解,无法有效地编写代码?

如果可以做到这一点,我应该如何进行?只要图上的视觉效果相同,我完全可以接受不直接解决数据方面异常值问题的解决方案。

感谢您的宝贵时间和您能提供的任何帮助!

编辑:这里有一些数据可供使用。顶行是列标签,下面是数据。抱歉,如果这种格式不好。第 9 行数据中的 29 和 30 等,总体第 10 行,是我的图表中绘制为异常值的一些点的示例,我想保留在箱线图中但不保留在散点图中/条形图。

1   5   10  15  30  60
7.233333333 8.166666667 9.666666667 7.75    9   7
7.133333333 9.25    9.333333333 9.75    10  11
0.733333333 0.5 0.833333333 1   1   0
1.766666667 1.166666667 1   0.75    1   0
1.75    2.25    2.333333333 2.25    1   1
6.75    7   7.166666667 7.75    6.5 7
1.516666667 1.75    1.333333333 2   2   2
1.533333333 1.5 2   1.25    1.5 2
27.3    28.33333333 29.33333333 30.25   28.5    29
6.35    6   6.333333333 7   6   6
7.083333333 8.333333333 8.833333333 8.75    8   8
8.533333333 10.08333333 10.5    12  10.5    11
7.65    8.416666667 9   10.75   9   12
6.85    7.333333333 8   7.25    6   8
4.433333333 5   5.5 5   6.5 6
8.616666667 10  11.66666667 12.25   13  12
3.633333333 3.75    3.5 3.25    3   2
0.8 0.75    0.833333333 1   1   0
7.283333333 8.583333333 9.666666667 9.75    12  8
7.483333333 8.75    8.333333333 7.75    6.5 7
3.466666667 2.916666667 3.166666667 2.5 2   0
5.483333333 6.416666667 6.833333333 6.75    7   8

【问题讨论】:

  • 你可能已经安装了ggplot2,但是stripchart和boxplot不是ggplot函数。你能提供一个可重现的例子吗?即,您要绘制的一些数据?
  • 当然。我会尝试在编辑中添加一些数据。

标签: r ggplot2 boxplot outliers stripchart


【解决方案1】:

这里发生了一些事情。如果您想坚持使用基本绘图功能(boxplot() 和stripchart()),您可以简单地告诉stripchart 仅绘制某些标准内的点。异常值的通用标准是偏离平均值 3 个或更多标准差的任何点。我们没有将未修改的数据集传递给stripchart,而是将该数据集子集化(注意[ ] 括号)。

boxplot(xx)
stripchart(xx[xx <= mean(xx) + sd(xx) * 3], vertical = T, method = 'jitter', add = T, pch = 20, col = 'blue')

当然,如果你真的想使用ggplot2(我建议不仅安装那个包,还安装带有install.packages('tidyverse') 的整个tidyverse),你可以产生一个可以说更好的情节:

生成 ggplot 版本所需的数据格式和命令与基本图形版本完全不同,超出了此答案的范围。下面是可重现的代码。

library(tidyverse)

df <- structure(list(X1 = c(7.233333333, 7.133333333, 0.733333333, 1.766666667, 1.75, 6.75, 1.516666667, 1.533333333, 27.3, 6.35, 7.083333333, 8.533333333, 7.65, 6.85, 4.433333333, 8.616666667, 3.633333333, 0.8, 7.283333333, 7.483333333, 3.466666667, 5.483333333 ), X5 = c(8.166666667, 9.25, 0.5, 1.166666667, 2.25, 7, 1.75, 1.5, 28.33333333, 6, 8.333333333, 10.08333333, 8.416666667, 7.333333333, 5, 10, 3.75, 0.75, 8.583333333, 8.75, 2.916666667, 6.416666667 ), X10 = c(9.666666667, 9.333333333, 0.833333333, 1, 2.333333333, 7.166666667, 1.333333333, 2, 29.33333333, 6.333333333, 8.833333333, 10.5, 9, 8, 5.5, 11.66666667, 3.5, 0.833333333, 9.666666667, 8.333333333, 3.166666667, 6.833333333), X15 = c(7.75, 9.75, 1, 0.75, 2.25, 7.75, 2, 1.25, 30.25, 7, 8.75, 12, 10.75, 7.25, 5, 12.25, 3.25, 1, 9.75, 7.75, 2.5, 6.75), X30 = c(9, 10, 1, 1, 1, 6.5, 2, 1.5, 28.5, 6, 8, 10.5, 9, 6, 6.5, 13, 3, 1, 12, 6.5, 2, 7), X60 = c(7L, 11L, 0L, 0L, 1L, 7L, 2L, 2L, 29L, 6L, 8L, 11L, 12L, 8L, 6L, 12L, 2L, 0L, 8L, 7L, 0L, 8L)), .Names = c("X1", "X5", "X10", "X15", "X30", "X60"), class = "data.frame", row.names = c(NA, -22L))

df.long <- gather(df, x, value) %>% 
  mutate(x = as.factor(as.numeric(gsub('X', '', x)))) %>% 
  group_by(x) %>% 
  mutate(is.outlier = value > mean(value) + sd(value) * 3)

plot.df <- ggplot(data = df.long, aes(x = x, y = value, group = x)) +
  geom_boxplot() +
  geom_point(data = filter(df.long, !is.outlier), color = '#0000ff88', position = position_jitter(width = 0.1))
print(plot.df)

【讨论】:

    猜你喜欢
    • 2013-06-08
    • 2017-09-27
    • 1970-01-01
    • 2013-04-08
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 2020-05-02
    • 1970-01-01
    相关资源
    最近更新 更多