我正在尝试制作带有数学符号的图例。我尝试了其他选项,但是没有用。我找不到将标签直接分配给guide_legend
的方法,并且ggplot似乎无法理解因子水平的expression
。同样,将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()
答案 0 :(得分:1)
我忽略了已经问过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]))