简体   繁体   English

为什么我的 `legend.text` 和颜色没有在使用 `scale_linetype_manual` 和 `guides` 的第二个图例中正确分配?

[英]Why my `legend.text` and colour is not assigned properly in a second legend using `scale_linetype_manual` and `guides`?

The plot below is to create a ggplot in which I show the relationship between two variables according to the raw data (points) and according to my model (dashed lines, each one for different ID).下面的图是创建一个 ggplot,其中我根据原始数据(点)和我的模型(虚线,每条线代表不同的 ID)显示两个变量之间的关系。

Plot_a <- ggplot(Todo.6min, aes(x=VeDBA.V13AP, y=VeDBA.X16, colour=ID)) + 
  geom_point(size=1.5,alpha=0.2) +
  geom_line(aes(y=predict(mod3.6min,df.6min), group=ID), size=3, alpha=0.6, linetype="dashed") +
  geom_abline(aes(slope=1,intercept=0),linetype="dashed",color="grey52",size=1.5) +
  theme_bw() + 
  theme(legend.text=element_text(size=18),
        legend.title = element_text(size=19, face = "bold"),
        legend.key=element_blank()) +
  guides(color=guide_legend(override.aes=list(fill=NA)))

Plot_a

在此处输入图片说明

Then, I add some extra lines from a different model and I colour those lines using a palette of colours from RColorBrewer .然后,我添加了一些来自不同模型的额外线条,并使用来自RColorBrewer的调色板为这些线条RColorBrewer As you can see, I include a second legend relative to this second model in which appears as legend.title "n.V13AP" and as legend.text "Low","Medium" and "High".如您所见,我包含了与第二个模型相关的第二个图例,其中显示为legend.title“n.V13AP”和legend.text“Low”、“Medium”和“High”。 The dark red colour should correspond to the nV13AP level High , and the less intense red colour should correspond to the nV13AP level Low .深红色应对应于nV13AP级别High ,较不强烈的红色应对应于nV13AP级别Low

line_colors <- RColorBrewer::brewer.pal(7, "Reds")[c(2,4,6)]
line_colors

Plot_a.2 <- Plot_a +
  geom_line(aes(y=predict(mod3.6min_gls,df.6min_low.n), linetype = "Low"), color = line_colors[1], size=1.5, alpha=1) +
  geom_line(aes(y=predict(mod3.6min_gls,df.6min_medium.n), linetype = "Medium"), color = line_colors[2], size=1.5, alpha=1) +
  geom_line(aes(y=predict(mod3.6min_gls,df.6min_high.n), linetype = "High"), color = line_colors[3], size=1.5, alpha=1) + 
  scale_linetype_manual(values = c(Low = "solid", Medium = "solid", High = "solid"),labels = c(Low = "Low", Medium = "Medium", High = "High")) +
  labs(color = "ID", linetype = "nV13AP") +
  guides(linetype = guide_legend(override.aes = list(color = line_colors)))

Plot_a.2

在此处输入图片说明

However, as you can observe, the colour assignment in the nV13AP legend is not correct, and I do not know why.但是,正如您所观察到的, nV13AP图例中的颜色分配不正确,我不知道为什么。 Additionaly, the order of the nV13AP levels is not as I indicated in scale_linetype_manual .此外, nV13AP级别的顺序与我在scale_linetype_manual指出的scale_linetype_manual The order in the legend is High / Low / Medium while I would like to have either High / Medium / Low or Low / Medium / High .图例中的顺序是High / Low / Medium而我想要High / Medium / LowLow / Medium / High

Does anyone know where is the mistake?有谁知道错误在哪里? I checked that the colours assignment was wrong changing the values of the High line and observing which line changed in the plot, and I observed that was the dark red line which changed.我检查了颜色分配是否错误,改变了High线的值并观察了图中哪条线发生了变化,我观察到的是深红色线发生了变化。 So the light red colour for High in the legend is wrong!.所以传说中High浅红色是错误的!。

The issue is that when using guide_legend you have to put pass color a vector of colors in the order they appear in the legend, ie the "High" color should come first, "Low" second, ... As you provided no data, I could not check but问题是,当使用guide_legend您必须按照它们在图例中出现的顺序将color向量放入color向量,即“高”颜色应该先出现,“低”第二,......因为你没有提供数据,我无法检查但是

guides(linetype = guide_legend(override.aes = list(color = line_colors[c(3, 1, 2)]))

should give you the right colors for your linetype legend.应该为您的线型图例提供正确的颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM