如何使用ggplot2创建基本的R风格箱形图? [英] How to make a base R style boxplot using ggplot2?

查看:178
本文介绍了如何使用ggplot2创建基本的R风格箱形图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为即将出版的出版物制作很多箱型图。我想使用ggplot2,因为我认为它对于将来的项目将更加灵活,但是我的PI坚持要求我以base-R的样式进行绘制。他特别想要虚线,以便它们看起来与我们之前绘制的情节相似。我通过以下代码使用虹膜数据集向您展示了一个示例:

  plot(iris $ Species,
iris $ Sepal.Length,
xlab ='Species',
ylab ='Sepal Length',
main ='跨物种的Sepal变化',
col ='white' )



我的问题是如何使用ggplot2绘制相似外观的图?



这是我的尝试:

  library( ggplot2)
ggplot(iris)+
geom_boxplot(aes(x = Species,y = Sepal.Length),linetype = dashed)+
ggtitle(跨物种的Sepal变化)



我需要虚线和实线的组合,但是我无法执行任何操作。我已经检查过


不完全完全相同,但这似乎是一个不错的近似值。希望这足够满足您的需求。祝你好运,快乐的密谋!


I need to make a lot of boxplots for an upcoming publication. I would like to use ggplot2 because I think it will be more flexible for future projects, but my PI is insisting that I make these plots in the style of base-R. He specifically wants the dashed lines, so that they will appear similar to previous plots we made. I have made an example using the iris dataset to show you, using this code:

plot(iris$Species,
     iris$Sepal.Length,
     xlab='Species',
     ylab='Sepal Length',
     main='Sepal Variation Across Species',
     col='white')

My question is how to make a similar looking plot using ggplot2?

Here is my attempt:

library("ggplot2")
ggplot(iris) +
  geom_boxplot(aes(x=Species,y=Sepal.Length),linetype="dashed") +
  ggtitle("Sepal Variation Across Species")

I need the combination of dashed and solid lines, but I cannot make anything work. I have already checked https://stats.stackexchange.com/questions/8137/how-to-add-horizontal-lines-to-ggplot2-boxplot which is very very close but no dashed lines, which we need. Also the outliers are filled circles, which is not the same as base-R.

解决方案

To generate a "base R style" boxplot using ggplot2, we can layer 4 boxplot objects over top of one another. The order does matter here, so please keep this in mind if you modify the code. I strongly suggest that you explore this code by plotting each boxplot layer on its own; that way you can get a feel for how the different layers interact.

The ordering of the boxplots works like this (ordered from bottom to top):

  • (1) vertical dashed lines are placed first
  • (2) a solid box containing a median line, which covers the dashed box from (1)
  • (3) & (4) solid whisker lines, created by using errorbars with the minima set to the maxima, and vice versa.

I also added custom breaks to match your base R plot, which you can change depending on your needs. panel.border is used to create a thin border in the style of base R. To get the open circles that you want, we use outlier.shape.

The code:

library("ggplot2")

ggplot(data = iris, aes(x = Species, y = Sepal.Length)) +
  geom_boxplot(linetype = "dashed", outlier.shape = 1) +
  stat_boxplot(aes(ymin = ..lower.., ymax = ..upper..), outlier.shape = 1) +
  stat_boxplot(geom = "errorbar", aes(ymin = ..ymax..)) +
  stat_boxplot(geom = "errorbar", aes(ymax = ..ymin..)) +
  scale_y_continuous(breaks = seq(4.5, 8.0, 0.5)) +
  labs(title = "Sepal Variation Across Species",
       x = "Species",
       y = "Sepal Length") +
  theme_classic() + # remove panel background and gridlines
  theme(plot.title = element_text(hjust = 0.5,  # hjust = 0.5 centers the title
                                  size = 14,
                                  face = "bold"),
        panel.border = element_rect(linetype = "solid",
                                    colour = "black", fill = "NA", size = 0.5))

The plot:

Not quite exactly the same, but it seems to be a decent approximation. Hopefully this is close enough for your needs. Good luck, and happy plotting!

这篇关于如何使用ggplot2创建基本的R风格箱形图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆