r - 图例中的平方形状点变化 - ggplot R
问题描述
我正在绘制几个点,ggplot
我想用一个空方形点突出显示其中一个点。我通过以下方式进行操作:
ggplot(data.frame(x=rnorm(7), y=rnorm(7)), aes(x,y))+
geom_point(aes(shape = "Points"), size=1.4)+
geom_point(data = data.frame(x=rnorm(1),y=rnorm(1)), aes(shape="Square"), size = 1.9, stroke = 1.7) +
coord_cartesian(xlim = c(-3,3),ylim = c(-3,3))+
scale_shape_manual(name = "Shape", values = c(16,0))
情节中的方形点很好,但图例中的那个比情节中的点厚。更准确地说,它看起来更厚,但它实际上有第二个薄的内部正方形,当我使用 tikzDevice 导出绘图时可以看到它(见下图)。
即使尝试通过以下方式直接修改图例中的形状,问题仍然存在:
guides(shape = guide_legend(override.aes = list(shape=c(16,0))))
你能帮我看看如何让图例中的点看起来像点形状 0 吗?
解决方案
您可以覆盖图例中的笔划宽度;如果将其设置为 1,则会得到两个完全重叠的正方形(如果将其设置为 NULL、0 或 NA,则根本不会得到):
library(ggplot2)
set.seed(1)
ggplot(data.frame(x = rnorm(7), y = rnorm(7)), aes(x, y)) +
geom_point(aes(shape = "Points"), size = 1.4) +
geom_point(
data = data.frame(x = rnorm(1), y = rnorm(1)),
aes(shape = "Square"),
size = 1.9,
stroke = 1.7
) +
coord_cartesian(xlim = c(-3, 3), ylim = c(-3, 3)) +
scale_shape_manual(name = "Shape", values = c(16, 0)) +
guides(shape = guide_legend(override.aes = list(size=c(1, 1.4), stroke = 1)))
由reprex 包于 2020-03-10 创建(v0.3.0)
推荐阅读
- postgresql - 如果在目标表中已经检测到 n 个重复值,如何在插入时引发 SQL 错误
- html - 我应该使用数据库中的“是/否”选项填写下拉列表还是直接使用 HTML 来填写?
- excel - 为 n 列中的值生成所有可能的组合 - 其中 n 可以变化(VBA)
- javascript - 以正确的方式格式化 Jquery 脚本
- python - 多维状态动作空间平铺比使用 np.meshgrid 更有效的方法?
- php - sqlsrv_connect 工作正常,但 laravel 失败:TCP Provider: No connection could be made because the target machine positively denied it
- docker - Docker不保存使用python创建的文件 - Flask应用程序
- redis - 如何使用 Redis 5.x 禁用持久性
- authorization - Apache AuthzSendUnauthorizedOnFailure?
- google-cloud-platform - 在 GCP 上,如何授予对另一个项目中服务帐户的访问权限?