(R) 如何在 ggplot2 中根据颜色、线型和点形状获取图例?
Posted
技术标签:
【中文标题】(R) 如何在 ggplot2 中根据颜色、线型和点形状获取图例?【英文标题】:(R) How to get a legend in ggplot2 based on color, linetype and point shape? 【发布时间】:2021-09-05 12:55:10 【问题描述】:如何根据点的颜色、线型和形状来区分 y1 和 y2,以获取以下数据的图例? (sd 只是表示标准差)
x <- c(2.5, 1.25, 0.625, 0.3125, 0.15625, 0.078, 0.039, 0)
y2<- c(1.422616667,
1.26845,
1.149133333,
0.842166667,
0.4697,
0.251566667,
0.118133333,
0.000233333)
y1<- c(1.4364,
1.342333333,
1.155216667,
0.88485,
0.502266667,
0.2673,
0.110783333,
-0.000233333)
sd1<- c(0.12252713,
0.09232539,
0.046399526,
0.078883881,
0.014853518,
0.039726687,
0.012708213,
0.005837722)
sd2<- c(0.183093342,
0.122781068,
0.088565849,
0.110920994,
0.036569081,
0.02125754,
0.012588791,
0.002873616)
这是我尝试过的:
data = data.frame(y1, y2, sd1, sd2, Conc, colors)
colors<- c("y1" = "red", "y2" = "blue")
ggplot(data = data, aes(x = Conc, y = y1))+
geom_point(aes(x = Conc, y = y1), shape = 15, size = 3)+
geom_line(aes(x = Conc, y = y1, color = "y1"), linetype = "solid", size = 1.3)+
geom_line(aes(x = Conc, y = y2, color = "y2"), linetype = "dashed", size = 1.3)+
geom_point(aes(x = Conc, y = y2), shape = 2, size = 3)+
geom_errorbar(aes(ymin =y1 - sd1, ymax = y1 + sd1), color = "red")+
geom_errorbar(aes(ymin = y2 - sd2, ymax = y2 + sd2), color = "blue")+
labs(x = "Concentration in micg per mL",
y = "Absorbance",
colors = "Legend",
title = "Absorbance as a function of Concentration")+
scale_color_manual(values = colors)+
theme(plot.title = element_text(hjust = 0.5))+
theme(plot.title = element_text(face = "bold"))
我可以根据线条颜色获得图例,但也不能获得线型和点形状。有什么想法吗?
【问题讨论】:
这能回答你的问题吗? Add legend to ggplot2 line plot 您的示例不可重现:您的数据包含 Conc 和颜色列,而您没有提供该列的值 【参考方案1】:当然,您也可以提供线型和形状的图例。最简单的方法是首先将数据重塑为长格式,例如通过将y1
和y2
的数据放入单独的数据帧中,添加一个 id 变量并将这些数据帧绑定到一个。这样做您将id
变量映射到color
、linetype
和shape
上,并且您只需要一个geom_point
、一个geom_line
...来创建您的绘图。线型和shape
的值可以通过scale_linetype/shape_manual
设置,就像您已经为颜色所做的那样。最后,要将所有图例合并为一组,为 labs
中的每个图例设置相同的标签,或者如果您更喜欢单独的图例,只需使用不同的标签:
library(ggplot2)
data = list(data.frame(y = y1, sd = sd1, Conc = x, id = "y1"), data.frame(y = y2, sd = sd2, Conc = x, id = "y2"))
data <- do.call("rbind", data)
colors<- c("y1" = "red", "y2" = "blue")
ltys <- c("y1" = "solid", "y2" = "dashed")
shapes <- c("y1" = 15, "y2" = 2)
ggplot(data = data, aes(x = Conc, y = y))+
geom_point(aes(shape = id), size = 3) +
geom_line(aes(color = id, linetype = id), size = 1.3) +
geom_errorbar(aes(ymin = y - sd, ymax = y + sd, color = id)) +
scale_color_manual(values = colors) +
scale_linetype_manual(values = ltys) +
scale_shape_manual(values = shapes) +
labs(x = "Concentration in micg per mL",
y = "Absorbance",
color = "Legend", shape = "Legend", linetype = "Legend",
title = "Absorbance as a function of Concentration")+
#scale_color_manual(values = colors)+
theme(plot.title = element_text(hjust = 0.5))+
theme(plot.title = element_text(face = "bold"))
由reprex package (v2.0.0) 于 2021-06-22 创建
【讨论】:
以上是关于(R) 如何在 ggplot2 中根据颜色、线型和点形状获取图例?的主要内容,如果未能解决你的问题,请参考以下文章
R语言使用ggplot2包的快速可视化函数qplot绘制分组密度图(分组色彩线型配置)实战