我需要为即将发布的出版物制作很多箱图.我想使用ggplot2,因为我认为它对未来的项目会更灵活,但我的PI坚持要以base-R的风格制作这些图.他特别想要虚线,这样它们就会像我们之前制作的那样.我使用iris数据集向您展示了一个示例,使用以下代码:
plot(iris$Species,
iris$Sepal.Length,
xlab='Species',
ylab='Sepal Length',
main='Sepal Variation Across Species',
col='white')
Run Code Online (Sandbox Code Playgroud)

我的问题是如何使用ggplot2制作类似的情节?
这是我的尝试:
library("ggplot2")
ggplot(iris) +
geom_boxplot(aes(x=Species,y=Sepal.Length),linetype="dashed") +
ggtitle("Sepal Variation Across Species")
Run Code Online (Sandbox Code Playgroud)

我需要虚线和实线的组合,但我无法做任何事情.我已经检查了https://stats.stackexchange.com/questions/8137/how-to-add-horizontal-lines-to-ggplot2-boxplot这是非常接近但没有虚线,我们需要它.异常值也是实心圆,与base-R不同.
Mar*_*ell 11
要使用ggplot2生成"基本R样式"箱图,我们可以将4个箱图对象叠加在一起.订单在这里很重要,因此如果您修改代码,请记住这一点.我强烈建议您通过绘制每个boxplot图层来探索此代码; 通过这种方式,您可以了解不同层的交互方式.
箱形图的排序方式如下(从下到上排序):
我还添加了自定义中断以匹配您的基本R图,您可以根据需要更改.panel.border用于创建基础R样式的细边框.要获得所需的空心圆,我们使用outlier.shape.
代码:
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))
Run Code Online (Sandbox Code Playgroud)
剧情:
不是很完全一样的,但它似乎是一个体面的近似.希望这足以满足您的需求.祝你好运,快乐的策划!
这是@Marcus 出色解决方案的包装器,以方便使用和提高灵活性:
geom_boxplot2 <- function(mapping = NULL, data = NULL, stat = "boxplot", position = "dodge2",
..., outlier.colour = NULL, outlier.color = NULL, outlier.fill = NULL,
outlier.shape = 1, outlier.size = 1.5, outlier.stroke = 0.5,
outlier.alpha = NULL, notch = FALSE, notchwidth = 0.5, varwidth = FALSE,
na.rm = FALSE, show.legend = NA, inherit.aes = TRUE,
linetype = "dashed"){
list(
geom_boxplot(mapping = mapping, data = data, stat = stat, position = position,
outlier.colour = outlier.colour, outlier.color = outlier.color,
outlier.fill = outlier.fill, outlier.shape = outlier.shape,
outlier.size = outlier.size, outlier.stroke = outlier.stroke,
outlier.alpha = outlier.alpha, notch = notch,
notchwidth = notchwidth, varwidth = varwidth, na.rm = na.rm,
show.legend = show.legend, inherit.aes = inherit.aes,
linetype = linetype, ...),
stat_boxplot(aes(ymin = ..lower.., ymax = ..upper..), outlier.shape = 1) ,
stat_boxplot(geom = "errorbar", aes(ymin = ..ymax..)) ,
stat_boxplot(geom = "errorbar", aes(ymax = ..ymin..)) ,
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))
)
}
ggplot(data = iris, aes(x = Species, y = Sepal.Length)) +
geom_boxplot2() +
scale_y_continuous(breaks = seq(4.5, 8.0, 0.5)) + # not sure how to generalize this
labs(title = "Sepal Variation Across Species", y = "Sepal Length")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
574 次 |
| 最近记录: |