我有以下数据框:
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()
Run Code Online (Sandbox Code Playgroud)
如您所见,文本图例(男性、女性)出现在按键图例(蓝色和红色水平条)的右侧。由于语言原因,我想要相反的结果:关键图例应该位于文本图例的右侧。我只找到了将文本左对齐或右对齐的解决方案,但没有将键放在文本之前或之后。(请参阅此处在 ggplot 中对齐图例文本)
ggplot(df, aes(x=x, y=y, color = group)) +
geom_smooth() +
theme(
legend.text.align = 1)
Run Code Online (Sandbox Code Playgroud)
任何想法?
我希望这是你想要的
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'
Run Code Online (Sandbox Code Playgroud)

由reprex 包(v0.3.0)于 2020-01-08 创建