使用ggplot2将抖动应用于boxplot中的异常值数据
问题描述:
您是否知道如何仅将抖动应用于箱线图的异常数据?这是代码:
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(outlier.size = 0.5)+
geom_point(data=a, aes(x="", y=a$V8[54]), colour="red", size=3) +
theme_bw()+
coord_flip()
谢谢!!
答
在数据集中添加了一个向量,以指示哪些点是异常点,哪些不是异常点。然后,将 geom_boxplot
设置为不绘制任何离群值,并使用 geom_point
显式绘制离群值。
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.
我将使用 ggplot2
中的钻石
数据集进行说明。 / p>
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