ggplot2:如何将数学符号分配给两种不同美学的图例标签?
ggplot2: how to assign math symbols to legend labels for two different aesthetics?
我正在尝试制作一个带有数学符号的图例。我尝试了不同的选项,但 none 有效。我找不到直接将标签分配给 guide_legend
的方法,并且 ggplot 似乎不理解因子水平中的 expression
s。同样给因子级别 label_bquote
失败(无论如何,ggplot2 文档说它适用于面条)。
在下面的示例中,我想将 L1、L2 和 Linf 分别更改为下标为 1、2 和无穷大符号的 L。
a1 = exp(seq(1, 3))
a2 = exp(seq(1, 3) + 0.2)
a3 = exp(seq(1, 3) + 0.4)
df = data.frame(coefficients = c(a1, a2, a3), order = rep(1:3, 3),
norm = factor(rep(1:3, each = 3), labels = expression(L[1], L[2], L[inf])))
ggplot(df, aes(x = order, y = coefficients, colour = norm, shape = norm)) +
geom_line() + geom_point()
我忽略了 similar question 已经被问到并且答案给出了正确的提示:通过比例分配标签,如下所示。请注意,必须定义两个尺度,并且两者必须具有相同的名称和标签,以避免产生两个图例。
a1 = exp(seq(1, 3))
a2 = exp(seq(1, 3) + 0.2)
a3 = exp(seq(1, 3) + 0.4)
df = data.frame(coefficients = c(a1, a2, a3), order = rep(1:3, 3),
norm = factor(rep(1:3, each = 3), labels = c("L1", "L2", "Linf")))
ggplot(df, aes(x = order, y = coefficients, colour = norm, shape = norm)) +
geom_line() + geom_point() +
scale_colour_manual(name = "norm", values = c("blue", "red", "green"),
labels = expression(L[1], L[2], L[infinity])) +
scale_shape_discrete(name = "norm",
labels = expression(L[1], L[2], L[infinity]))
我正在尝试制作一个带有数学符号的图例。我尝试了不同的选项,但 none 有效。我找不到直接将标签分配给 guide_legend
的方法,并且 ggplot 似乎不理解因子水平中的 expression
s。同样给因子级别 label_bquote
失败(无论如何,ggplot2 文档说它适用于面条)。
在下面的示例中,我想将 L1、L2 和 Linf 分别更改为下标为 1、2 和无穷大符号的 L。
a1 = exp(seq(1, 3))
a2 = exp(seq(1, 3) + 0.2)
a3 = exp(seq(1, 3) + 0.4)
df = data.frame(coefficients = c(a1, a2, a3), order = rep(1:3, 3),
norm = factor(rep(1:3, each = 3), labels = expression(L[1], L[2], L[inf])))
ggplot(df, aes(x = order, y = coefficients, colour = norm, shape = norm)) +
geom_line() + geom_point()
我忽略了 similar question 已经被问到并且答案给出了正确的提示:通过比例分配标签,如下所示。请注意,必须定义两个尺度,并且两者必须具有相同的名称和标签,以避免产生两个图例。
a1 = exp(seq(1, 3))
a2 = exp(seq(1, 3) + 0.2)
a3 = exp(seq(1, 3) + 0.4)
df = data.frame(coefficients = c(a1, a2, a3), order = rep(1:3, 3),
norm = factor(rep(1:3, each = 3), labels = c("L1", "L2", "Linf")))
ggplot(df, aes(x = order, y = coefficients, colour = norm, shape = norm)) +
geom_line() + geom_point() +
scale_colour_manual(name = "norm", values = c("blue", "red", "green"),
labels = expression(L[1], L[2], L[infinity])) +
scale_shape_discrete(name = "norm",
labels = expression(L[1], L[2], L[infinity]))