我正在努力为我的ggplot添加修改过的图例。
我有一个数据集:
birdspp smallsaplings mediumsaplings largesaplings
95 5.044642857 2.384615385 1.30952381
97 3.482269504 1.873684211 1.390625
63 6.285714286 2 2.4
57 5.216216216 1.666666667 1.125
我用:
创建了一个图表
使用代码:
library(ggplot2)
library(dplyr)
library(tidyr)
sapling %>%
gather(sapling_type, mean_number, -birdspp)
sapling %>%
gather(sapling_type, mean_number, -birdspp) %>%
ggplot(aes(mean_number, birdspp)) +
geom_point(aes(color = sapling_type)) +
geom_smooth(aes(group =sapling_type, color = sapling_type, fill = sapling_type), method = "lm", show.legend=FALSE) +
labs(x="Mean Sapling Density", y="Number of Bird Species") +
theme_classic()
正如你所看到的那样,传说并不是很整洁。
我尝试将show.legend = FALSE打开并添加:
+ scale_colour_discrete(name ="Sapling Size",
breaks=c("smallsaplings", "mediumsaplings", "largesaplings"),
labels=c("smallsaplings", "mediumsaplings", "largesaplings")) +
scale_shape_discrete(name ="Sapling Size",
breaks=c("smallsaplings", "mediumsaplings", "largesaplings"),
labels=c("smallsaplings", "mediumsaplings", "largesaplings"))
但是没有运气。任何建议将不胜感激。
谢谢。
答案 0 :(得分:1)
这应该有效
sapling %>%
gather(sapling_type, mean_number, -birdspp) %>%
ggplot(aes(mean_number, birdspp)) +
geom_point(aes(color = sapling_type)) +
geom_smooth(aes(group =sapling_type, color = sapling_type, fill = sapling_type), method = "lm", show.legend=FALSE) +
labs(x="Mean Sapling Density", y="Number of Bird Species") +
theme_classic() +
scale_colour_discrete(name="",
breaks=c("smallsaplings", "mediumsaplings", "largesaplings"),
labels=c("Small Saplings", "Medium Saplings", "Large Saplings"))