如何在geom_box图上自定义whisker线与框本身的线不同

时间:2015-03-31 15:17:29

标签: r ggplot2 boxplot

由于要求最终用户,我需要了解geom_box图上的胡须线是否可以与盒子本身不同地着色或打字?

刚刚考虑boxplot with colored and dotted lines,我创建了一个最小的例子。

year <- rep("2014", 10)
total <- c(seq(55, 90, 5), 100, 40)
df <- data.frame(year = as.factor(year), total = total)

ggplot(df, aes(x=factor(year), y=total)) + 
  geom_boxplot(linetype = "dotted", color = "red") +
  theme_bw()

下面的情节是否有绿色胡须,保留红色框或固体胡须保持虚线框?

enter image description here

这个问题告诉我们,基地R允许大量的晶须线定制。 bxp has several parameters

评论后编辑: 我没有发现用户20650慷慨指出的SO问题。这是它的答案 - 两次绘制箱线图。

ggplot(df, aes(x=factor(year), y=total)) + 
  geom_boxplot(linetype = "dotted", color = "red") +
  geom_boxplot(aes(ymin=..lower.., ymax=..upper..)) + 
  theme_bw()

1 个答案:

答案 0 :(得分:4)

评论后编辑:我没有发现用户20650慷慨指出的SO问题。这是它的答案 - 两次绘制箱线图。

ggplot(df, aes(x=factor(year), y=total)) + 
  geom_boxplot(linetype = "dotted", color = "red") +
  geom_boxplot(aes(ymin=..lower.., ymax=..upper..)) + 
  theme_bw()