使用scale_color_manual控制图例中的多个数据组的颜色

djp7away  于 2023-05-11  发布在  其他
关注(0)|答案(1)|浏览(171)

我有一个关于多元回归线的后续问题:可以将多条回归线应用于facet_wrap中的同一点子集吗?
我正在努力使图例正确显示点、线和颜色。
显示两个组:适合和来源。我希望源点只有我选择的颜色(我使用的出版色盲调色板)。“Fit”只能是黑色线条。
但是如果我按原样运行代码,我会得到不同颜色的线穿过所有内容,包括蓝色线穿过Fit中的黑点,即使图中的拟合线是绿色的。如果我尝试告诉stat-smooth使用color=“black”,那么Fit中仍然有黑点,并且图例中通过每个点的每条线都是黑色的,包括Source。
下面是代码和一些输出:

output_df <- data.frame(year=c(1993,1993,1995,1995,1998,1998,2001,2001,
                               1993,1993,1995,1995,1998,1998,2001,2001),
                        treatment_scen=c("CR","CR","CR","CR","CR","CR","CR","CR",
                                         "NT","NT","NT","NT","NT","NT","NT","NT"),
                        val=c(7.16875,22.60000,23.03250,24.30000,
                              29.46125,23.60000,21.41375,24.80000,
                              9.11125,25.30000,27.13950,26.60000,
                              30.46950,24.90000,24.60500,26.10000),
                        Source=c("Observed","Model","Observed","Model",
                                 "Observed","Model","Observed","Model",
                                 "Observed","Model","Observed","Model",
                                 "Observed","Model","Observed","Model")
                        )


trendlines_df <- data.frame(year=c(1993,1993,1995,1995,
                                   1998,1998,2001,2001,
                                   1993,1993,1995,1995,
                                   1998,1998,2001,2001),
                            treatment_scen=c("CR","CR","CR","CR",
                                        "CR","CR","CR","CR",
                                        "NT","NT","NT","NT",
                                        "NT","NT","NT","NT"),
                            Fit=c("AllObs","OutlierRemoved","AllObs","OutlierRemoved",
                                  "AllObs","OutlierRemoved","AllObs","OutlierRemoved",
                                  "AllObs","OutlierRemoved","AllObs","OutlierRemoved",
                                  "AllObs","OutlierRemoved","AllObs","OutlierRemoved"),
                            value=c(7.16875,NA,23.03250,23.03250,
                                    29.46125,29.46125,21.41375,21.41375,
                                    9.11125,NA,27.13950,27.13950,
                                    30.46950,30.46950,24.60500,24.60500),
                            Source=c("Observed","Observed","Observed","Observed",
                                     "Observed","Observed","Observed","Observed",
                                     "Observed","Observed","Observed","Observed",
                                     "Observed","Observed","Observed","Observed"),
                            Model=c("Observed","Observed","Observed","Observed",
                                     "Observed","Observed","Observed","Observed",
                                     "Observed","Observed","Observed","Observed",
                                     "Observed","Observed","Observed","Observed")
)

# 9-color palette with grey and black. Colors in order are:
#[1]black, [2]dark blue, [3]green, [4]light blue, [5]grey,
#[6]pink, [7]red, [8]orange, [9]yellow
cbPalette9 <- c("#000000","#0072B2","#009E73","#56B4E9","#999999",
                "#CC79A7","#D55E00","#E69F00","#F0E442")
                                    
 
g1 <- output_df %>%
  ggplot(aes(x=year, y=val, color=Source, show.legend=TRUE)) +
  geom_point(show.legend=TRUE) +
  xlab("Year") +
  stat_smooth(
    data = trendlines_df, 
    aes(linetype = Fit, y=value), 
    #color="black", 
    method = lm,
    se = FALSE,
    show.legend = TRUE,
    fullrange=TRUE
  ) +
  scale_color_manual(labels=c("Model","Observed"),
                     values=cbPalette9[c(8,3)]) +
  facet_wrap(~treatment_scen,nrow=1) 

g1

下面是代码as-as的输出:x1c 0d1x下面是在stat_smooth中使用color=“black”的输出:

那么,我如何获得只有我选择的颜色中的点而没有线的Source,以及只有黑线而没有点的Fit?
TIA

eyh26e7m

eyh26e7m1#

只需从代码中删除show.legend = TRUE

library(ggplot2)
library(dplyr)

output_df %>%
  ggplot(aes(x = year, y = val, color = Source)) +
  geom_point() +
  xlab("Year") +
  stat_smooth(
    data = trendlines_df,
    aes(linetype = Fit, y = value),
    color = "black",
    method = lm,
    se = FALSE,
    fullrange = TRUE
  ) +
  scale_color_manual(
    labels = c("Model", "Observed"),
    values = cbPalette9[c(8, 3)]
  ) +
  facet_wrap(~treatment_scen, nrow = 1)

相关问题