r - 如何对齐ggplot中的图例键和文本?
问题描述
我有以下数据框:
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()
如您所见,文本图例(男性、女性)出现在关键图例(蓝色和红色水平条)的右侧。出于语言原因,我想要相反:关键图例应该在文本图例的右侧。我只找到了将文本向左或向右对齐的解决方案,但没有将键放在文本之前或之后。(请参阅此处在 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 包(v0.3.0)于 2020 年 1 月 8 日创建
推荐阅读
- algorithm - 解决特定情况下替换路径问题的算法
- ssl - OpenShift 3.11 是否会阻止安全路由上的任何 SSLv3 流量?
- javascript - 使用 .put 从 API 生成的数组中编辑项目
- javascript - 如何将 arrayUnion 与 AngularFirestore 一起使用?
- python - 如何优化计算分布相似度的函数
- python - 为什么 Fore.BLUE 使文本以随机字符开头而不是使文本变为蓝色?
- c++ - 不能包括
在我的 mfc 应用程序中,编译器错误 - java - 使用 Maven 和 Eclipse 测试 Java 模块的白盒单元测试依赖项
- c++ - 如何在数组中交换中心?
- node.js - 如何将googleapis npm包导入meteor