r - 如何在 aes() 中设置 ggplot2 默认点形状?
问题描述
我将默认点形状设置为 17:
update_geom_defaults("point", list(shape = 17))
当我在没有指定内部形状的情况下aes()
进行绘图时,绘图与预期一致。但是当我把图例标签放在里面时aes()
,点的形状就完全不同了。我该如何解决这个问题,以便默认形状在所有地块上重复?
library(ggplot2)
update_geom_defaults("point", list(shape = 17))
X <- 1
Y <- 2
DF <- data.frame(X, Y)
# OK
ggplot(data = DF,
aes(x = X,
y = Y)) +
geom_point()
#Not OK
ggplot(data = DF,
aes(x = X,
y = Y)) +
geom_point(aes(shape = "Legend label"))
解决方案
update_geom_defaults
仅更改调用时使用的单个默认形状,geom_point
而无需将某些内容映射到shape
内部美学aes()
。
但是,当您添加shape
美学时,ggplot 使用默认的形状调色板,它的第一个值是形状 16。需要更改形状调色板以获得所需的形状 17 作为第一个值。您可以通过添加例如+ scale_shape_manual(values=c(17, 16))
到您的绘图代码来为单个绘图执行此操作,这将使形状 17 成为第一个形状,形状 16 成为第二个形状(当您将具有两个不同值的变量映射到shape
美学时)。但是你必须为每个情节都这样做。相反,您可以更改一次默认形状调色板,然后它将自动应用于每个后续绘图。在下面的示例中,为了说明,我使用新的分类列扩充了您的示例数据。
library(ggplot2)
library(patchwork)
theme_set(theme_bw(base_size=9))
update_geom_defaults("point", list(shape = 17))
X <- 1:2
Y <- 2:3
group = c("A","B")
DF <- data.frame(X, Y, group)
p1 = ggplot(data = DF,
aes(x = X,
y = Y)) +
geom_point(size=3) +
labs(title="No shape aesthetic")
p2 = ggplot(data = DF,
aes(x = X,
y = Y,
shape=group)) +
geom_point(size=3) +
labs(title="Add shape aesthetic")
p3 = ggplot(data = DF,
aes(x = X,
y = Y)) +
geom_point(aes(shape = "Legend label"), size=3) +
labs(title="Add dummy shape aesthetic")
p1 + p2 + p3
在下面的第二个和第三个图中,请注意实心圆仍然用作形状调色板中的第一个形状。
如果我们查看默认的形状调色板,我们可以看到它仍然将形状 16(实心圆)作为第一个值。
# View default shape palette
scales:::shape_pal()
#> function (n)
#> {
#> if (n > 6) {
#> msg <- paste("The shape palette can deal with a maximum of 6 discrete ",
#> "values because more than 6 becomes difficult to discriminate; ",
#> "you have ", n, ". Consider specifying shapes manually if you ",
#> "must have them.", sep = "")
#> warning(paste(strwrap(msg), collapse = "\n"), call. = FALSE)
#> }
#> if (solid) {
#> c(16, 17, 15, 3, 7, 8)[seq_len(n)]
#> }
#> else {
#> c(1, 2, 0, 3, 7, 8)[seq_len(n)]
#> }
#> }
#> <bytecode: 0x7fa674bbcfb8>
#> <environment: 0x7fa6762a8c60>
下面我们将重新定义scale_shape_discrete
(这是默认的形状比例)以使用形状 17(实心三角形)作为第一个值。然后当我们重做绘图时,我们会看到填充三角形在我们使用shape
美学时首先出现。
# Redefine default shape palette
scale_shape_discrete = function(...) {
scale_shape_manual(values = c(17, 16, 15, 3, 7, 8))
}
p1 = ggplot(data = DF,
aes(x = X,
y = Y)) +
geom_point(size=3) +
labs(title="No shape aesthetic")
p2 = ggplot(data = DF,
aes(x = X,
y = Y,
shape=group)) +
geom_point(size=3) +
labs(title="Add shape aesthetic")
p3 = ggplot(data = DF,
aes(x = X,
y = Y)) +
geom_point(aes(shape = "Legend label"), size=3) +
labs(title="Add dummy shape aesthetic")
p1 + p2 + p3
推荐阅读
- sql - NotesSQL 到 Notes 9 ODBC 连接导致 IM005(0)[Microsoft][ODBC Driver Manager] Driver's SQLAllocHandle on SQL_HANDLE_DBC failed
- php - 如何升级 symfony 次要版本(4.1 到 4.2)
- azure-ad-b2c - Azure AD B2C 审核跟踪 - 客户端 ID
- ionic-framework - 为离子切换添加边框
- python - 使用随机函数后如何选择特定元素?
- ios - 使用 cocoapods 的子规范构建 ios 框架的问题
- c# - 突出显示具有相同文本的所有文本块
- javascript - 为什么 .getMonth() 在一月份返回 11?
- javascript - FormBuilder - 自定义验证器 - 许多字段和承诺
- javascript - JavaScript:折叠无限流(生成器函数)