【发布时间】:2020-09-22 14:54:23
【问题描述】:
我正在尝试在 R 中创建一些箱线图。我一直在使用 ggboxplot 和 ggplot。到目前为止,这是我的代码和输出:
ggboxplot:
ggboxplot(shp_PA@data, x = "hei_1998to2007_cat", y = "adjrate.2008to2017",
xlab = "Hazardous Exposure Index Jenks",
ylab = "Lung Cancer Incidence Rate",
color = "red",
add = c("jitter", "mean"),
add.params = list(color = "black", shape=20))
ggplot:
shp_PA@data %>%
ggplot(aes(x=hei_1998to2007_cat, y=adjrate.2008to2017)) +
geom_boxplot(colour = "red") +
geom_jitter(color="black", size=0.75) +
stat_summary(fun=mean, geom="point", shape=4, size=3, color="black") +
xlab("Hazardous Exposure Index Jenks") +
ylab("Lung Cancer Incidence Rate")
我现在的主要兴趣是在每个箱线图上放置一个图例,其中包含用于描述平均值的符号,以及旁边的“平均值”一词。在base R中,它就像放置类似的东西一样简单
legend("topright", legend=c("Mean"),pch=5, col="red")
但我无法在 ggboxplot 或 ggplot 中弄清楚。我在网上看到的大部分内容都在讨论修改已经存在的图例。
我想知道如何做的另一件事是特定于 ggboxplot。我希望能够使抖动点的颜色和形状与均值符号不同。我尝试将 add.params 代码更改为
add.params = list(color = c("black", "blue"), shape=c(20,4))
但我得到了错误
Error: Aesthetics must be either length 1 or the same as the data (213): shape and colour
非常感谢任何帮助!
编辑:在 R 中添加使用 iris 数据集的可重现示例
ggboxplot:
ggboxplot(iris, x = "Species", y = "Sepal.Length",
color = "red",
add = c("jitter", "mean"),
add.params = list(color = "black", shape=20))
ggplot:
ggplot(data=iris, aes(x=Species, y=Sepal.Length)) +
geom_boxplot(colour = "red") +
geom_jitter(color="black", size=0.75) +
stat_summary(fun=mean, geom="point", shape=4, size=3, color="black")
再次,我想添加一个图例,其中包含用于描述均值的符号和“均值”一词,并且能够使用 ggboxplot 具有抖动的颜色和形状以及不同的均值。
【问题讨论】: