簡體   English   中英

用與箱線圖相同的顏色填充異常值 在ggplot R 中填充顏色?

[英]Fill outliers with same color as Boxplots Fill color in ggplot R?

我希望異常值以與箱線圖填充顏色(而不是輪廓)相同的顏色出現。 我嘗試了不同的方法,但都沒有奏效。 任何幫助將非常感激。

ToothGrowth$dose <- as.factor(ToothGrowth$dose)
p <- ggplot(ToothGrowth, aes(x=dose, y=len))+
     geom_boxplot(width=0.3,aes(fill=dose),outlier.colour = NA)+
     geom_point()+
     scale_fill_manual(values=c("firebrick", "royalblue","yellow"))
p

不同劑量的箱線圖

我們可以將outlier.shape設置為“21”,這樣它將繼承填充 colors 和 plot 箱線圖之前的點:

ToothGrowth$dose <- as.factor(ToothGrowth$dose)
ggplot(ToothGrowth, aes(x=dose, y=len))+
  geom_point()+
  geom_boxplot(width=0.3,aes(fill=dose),outlier.shape = 21)+
  scale_fill_manual(values=c("firebrick", "royalblue","yellow"))

在此處輸入圖像描述 它在圖片中幾乎看不到,但劑量 0.5 的異常值是紅色的。


編輯還要在框內顯示點:

is_outlier <- function(x) {
  return(x < quantile(x, 0.25) - 1.5 * IQR(x) | x > quantile(x, 0.75) + 1.5 * IQR(x))
}

ToothGrowth %>% 
  group_by(dose) %>% 
  mutate(outlier = ifelse(is_outlier(len), as.numeric(NA), as.numeric(len))) %>% 
ggplot(aes(x=dose, y=len))+
  geom_boxplot(width=0.3,aes(fill=dose),outlier.shape = 21)+
  geom_point(aes(x = dose, y = outlier))+
  scale_fill_manual(values=c("firebrick", "royalblue","yellow"))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM