当我使用 ggnewscale 包创建多个比例时如何更改图例标题位置?
How to change legend title position when I have multiple scales created with ggnewscale package?
我创建了一个图,其中包含使用 ggnewscale
包制作的 2 个色标和 2 个线型标尺。我希望图例出现在图的底部,图例标题出现在标签上方。我做了一半,因为在我的尝试中,第一个音阶(来自 geom_vline()
)没有按照我想要的方式出现。在用 new_scale()
和 new_scale_color()
声明下一个比例之前,我什至试图指出这个比例的所需位置。有人知道我该如何解决这个问题吗?
library(ggplot2)
library(ggnewscale)
mtcars$cyl <- factor(mtcars$cyl)
# Attempt 1
ggplot(data = mtcars,
aes(x = wt,
y = mpg)) +
geom_vline(aes(xintercept = gear,
color = "Important line",
linetype = "Important line")) +
scale_linetype_manual(name = "Important lines",
values = 1) +
scale_color_manual(name = "Important lines",
values = "red") +
new_scale("linetype") +
new_scale_color() +
geom_line(aes(linetype = cyl,
color = cyl)) +
theme(legend.position = "bottom") +
guides(linetype = guide_legend(title.position = "top"),
color = guide_legend(title.position = "top"))
# Attempt 2
ggplot(data = mtcars,
aes(x = wt,
y = mpg)) +
geom_vline(aes(xintercept = gear,
color = "Important line",
linetype = "Important line")) +
scale_linetype_manual(name = "Important lines",
values = 1) +
scale_color_manual(name = "Important lines",
values = "red") +
guides(linetype = guide_legend(title.position = "top",
label.position = "bottom"), # Before new scales
color = guide_legend(title.position = "top",
label.position = "bottom")) +
new_scale("linetype") +
new_scale_color() +
geom_line(aes(linetype = cyl,
color = cyl)) +
theme(legend.position = "bottom") +
guides(linetype = guide_legend(title.position = "top"),
color = guide_legend(title.position = "top"))
两次尝试都产生相同的情节,这不是我想要的。
不确定是否还有其他选择,但在使用 ggnewscale
时对我有用的是通过比例尺的 guide
参数设置指南的样式。为了完成这项工作,我添加了一个´scale_color_discrete`:
library(ggplot2)
library(ggnewscale)
mtcars$cyl <- factor(mtcars$cyl)
ggplot(data = mtcars,
aes(x = wt,
y = mpg)) +
geom_vline(aes(xintercept = gear,
color = "Important line",
linetype = "Important line")) +
scale_linetype_manual(name = "Important lines",
values = 1, guide = guide_legend(title.position = "top") ) +
scale_color_manual(name = "Important lines",
values = "red", guide = guide_legend(title.position = "top")) +
new_scale("linetype") +
new_scale_color() +
geom_line(aes(linetype = cyl,
color = cyl)) +
scale_color_discrete(guide = guide_legend(title.position = "top")) +
theme(legend.position = "bottom")
我创建了一个图,其中包含使用 ggnewscale
包制作的 2 个色标和 2 个线型标尺。我希望图例出现在图的底部,图例标题出现在标签上方。我做了一半,因为在我的尝试中,第一个音阶(来自 geom_vline()
)没有按照我想要的方式出现。在用 new_scale()
和 new_scale_color()
声明下一个比例之前,我什至试图指出这个比例的所需位置。有人知道我该如何解决这个问题吗?
library(ggplot2)
library(ggnewscale)
mtcars$cyl <- factor(mtcars$cyl)
# Attempt 1
ggplot(data = mtcars,
aes(x = wt,
y = mpg)) +
geom_vline(aes(xintercept = gear,
color = "Important line",
linetype = "Important line")) +
scale_linetype_manual(name = "Important lines",
values = 1) +
scale_color_manual(name = "Important lines",
values = "red") +
new_scale("linetype") +
new_scale_color() +
geom_line(aes(linetype = cyl,
color = cyl)) +
theme(legend.position = "bottom") +
guides(linetype = guide_legend(title.position = "top"),
color = guide_legend(title.position = "top"))
# Attempt 2
ggplot(data = mtcars,
aes(x = wt,
y = mpg)) +
geom_vline(aes(xintercept = gear,
color = "Important line",
linetype = "Important line")) +
scale_linetype_manual(name = "Important lines",
values = 1) +
scale_color_manual(name = "Important lines",
values = "red") +
guides(linetype = guide_legend(title.position = "top",
label.position = "bottom"), # Before new scales
color = guide_legend(title.position = "top",
label.position = "bottom")) +
new_scale("linetype") +
new_scale_color() +
geom_line(aes(linetype = cyl,
color = cyl)) +
theme(legend.position = "bottom") +
guides(linetype = guide_legend(title.position = "top"),
color = guide_legend(title.position = "top"))
两次尝试都产生相同的情节,这不是我想要的。
不确定是否还有其他选择,但在使用 ggnewscale
时对我有用的是通过比例尺的 guide
参数设置指南的样式。为了完成这项工作,我添加了一个´scale_color_discrete`:
library(ggplot2)
library(ggnewscale)
mtcars$cyl <- factor(mtcars$cyl)
ggplot(data = mtcars,
aes(x = wt,
y = mpg)) +
geom_vline(aes(xintercept = gear,
color = "Important line",
linetype = "Important line")) +
scale_linetype_manual(name = "Important lines",
values = 1, guide = guide_legend(title.position = "top") ) +
scale_color_manual(name = "Important lines",
values = "red", guide = guide_legend(title.position = "top")) +
new_scale("linetype") +
new_scale_color() +
geom_line(aes(linetype = cyl,
color = cyl)) +
scale_color_discrete(guide = guide_legend(title.position = "top")) +
theme(legend.position = "bottom")