我的ggplot包括geom_boxplot和geom_line,但它只显示boxplot部分的图例,而不显示geom_line部分的图例。我该怎么加?
注:方框图和直线是由两个不同的数据源绘制的,但显示在一张图中。
我真的需要帮助,谢谢!
这是代码:
library(ggnewscale)
bplot6 <- ggplot(seasonalmerge_neale1989) +
geom_boxplot(aes(x = date, y = values, group = date, fill = `Data Source`), width = 2, outlier.shape = NA,
lwd = 0.1) +
xlab("") +
ylab("") +
ylim(0,1.2) +
theme(
axis.text.x = element_text(family="serif", size = 8),
axis.text.y = element_text(family="serif", size = 8),
legend.title = element_text(family="serif", size = 8),
legend.text = element_text(family="serif", size = 8),
plot.title = element_text(family="serif", face="bold", size = 8, hjust = 0.5)) +
ggtitle("(f)")+
new_scale_color() +
geom_line(data=pointframe, aes(x= pointdate, y=pointvar), colour="gold", size=1, method = "lm", se=FALSE)
# +theme_classic()+scale_linetype_manual(values=bplot6, name="Data Source: ", labels=c("Ceres", "Landsat", "FAO"))+ theme(legend.position = "bottom")
bplot6
箱线图和线的图像
必须在美学内部指定颜色才能有图例。在aes()
内部移动colour
将自动生成图例。然后我们可以使用scale_color_manual
来调整标签和颜色。使用iris
数据再现代码。
library(ggnewscale)
bplot6 <- ggplot(iris) +
geom_boxplot(aes(x = Petal.Length, y = Petal.Width , group = Species, fill = `Species`), width = 2, outlier.shape = NA,lwd = 0.1) +
xlab("") +
ylab("") +
#ylim(0,1.2) + you can use it as per your requirement
theme(
axis.text.x = element_text(family="serif", size = 8),
axis.text.y = element_text(family="serif", size = 8),
legend.title = element_text(family="serif", size = 8),
legend.text = element_text(family="serif", size = 8),
plot.title = element_text(family="serif", face="bold", size = 8, hjust = 0.5)) +
ggtitle("(f)")+
new_scale_color() +
geom_line(data=iris, aes(x= Sepal.Length, y=Sepal.Width, color="Gold"), size=1)+
scale_color_manual(name = "Colour", values = c("Gold" = "gold"))
# +theme_classic()+scale_linetype_manual(values=bplot6, name="Data Source: ", labels=c("Ceres", "Landsat", "FAO"))+ theme(legend.position = "bottom")
bplot6