我有以下数据框:
df = data.frame(
x = c(1:10, 1:10),
y = 1:20,
group = rep(c('male', 'female'), each = 10))
ggplot(df, aes(x=x, y=y, color = group)) +
geom_smooth()
如您所见,文本图例(男性,女性)出现在关键图例的右侧(蓝色和红色的水平条)。出于语言原因,我要相反:键图例应位于文本图例的右侧。我只找到了一种解决方案,可以将文本向左或向右对齐,但不能将键放在文本之前或之后。 (请参见Align legend text in ggplot)
ggplot(df, aes(x=x, y=y, color = group)) +
geom_smooth() +
theme(
legend.text.align = 1)
任何想法?
我希望这是您想要的
library(ggplot2)
df = data.frame(
x = c(1:10, 1:10),
y = 1:20,
group = rep(c('male', 'female'), each = 10))
ggplot(df, aes(x=x, y=y, color = group)) +
geom_smooth() +
theme(legend.position = 'right') +
guides(color = guide_legend(title.position = "top",
# hjust = 0.5 centres the title horizontally
title.hjust = 0.5,
label.position = "left"))
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'
由reprex package(v0.3.0)在2020-01-08创建