首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我的ggplot包括geom_boxplot和geom_line,但是它没有显示geom_line部件的图例。我该怎么加呢?

我的ggplot包括geom_boxplot和geom_line,但是它没有显示geom_line部件的图例。我该怎么加呢?
EN

Stack Overflow用户
提问于 2022-09-02 23:47:38
回答 1查看 65关注 0票数 0

我的ggplot包括geom_boxplot和geom_line,但是它只显示盒图部分的图例,而不显示geom_line部分的图例。我该怎么加呢?

注意:方框图和行是从两个不同的数据源生成的,但显示在一个图表中。

我真的需要帮助,谢谢!

以下是代码:

代码语言:javascript
复制
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

方格图和线条的图像

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-03 03:06:40

为了有一个传说,必须在审美中指定颜色。将colour移动到aes()中将自动生成一个图例。然后我们可以用scale_color_manual调整标签和颜色。使用iris数据再现代码。

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73588734

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档