使用geom_boxplot时如何更改离群值的颜色

时间:2018-09-10 13:39:11

标签: r ggplot2

我作了几种不同的尝试来更改我正在构建的箱形图的异常值的填充颜色,但无济于事。任何帮助表示赞赏。到目前为止,这是我的代码...

# Load the dataset.
data <- read.csv("C:\\School\\Statistics\\bearWeight.csv")

# Initialize dataframe variable for both WEIGHT & Relative Frequency
WEIGHT <- data$WEIGHT
RelativeFrequency <- data$Relative.Frequency

# Load ggplot2
library(ggplot2)

bearWeightBoxPlot <- ggplot(data, aes(x=WEIGHT, y=RelativeFrequency, group = 1))

bearWeightBoxPlot <- bearWeightBoxPlot + 
  geom_boxplot(colour = "#3366FF", outlier.colour = "black", 
               outlier.shape = 24, outlier.fill = "red", outlier.size = 3)

bearWeightBoxPlot <- bearWeightBoxPlot + 
  geom_jitter(width = 0.2) + 
  coord_flip()

plot(bearWeightBoxPlot)

2 个答案:

答案 0 :(得分:2)

我们需要参数outlier.fill,其形状从21到25。

library(ggplot2)
p <- ggplot(mpg, aes(class, hwy))
p + geom_boxplot(outlier.colour = "black",
                 outlier.shape = 24,
                 outlier.fill = "red",
                 outlier.size = 3 # not actually needed
                 ) 

enter image description here


有关可能的形状的概述,请参见:www.cookbook-r.com

答案 1 :(得分:0)

通过填充美学更改离群值的形状(形状= 21以使用选定的填充来填充形状)

ggplot(iris,aes(Species,Sepal.Width,fill =“ skyblue1”))+   geom_boxplot(outlier.shape = 21) enter image description here