如何用ggplot中的符号替换轴上的数字?
How can I replace numbers on an axis with symbols in ggplot?
我正在制作散点图,想用符号替换 y 轴的数字。具体来说,我想将 20 换成减号,将 0 换成加号。
我看过 但我创建 y 轴的方式有点不同。
谢谢。
这是我的绘图代码:
library(tidyverse)
ggplot(exampledf, aes(x = male_diff, y = index, color = as.factor(expt_in_ms))) +
geom_point(aes(alpha = 0.5, size = 3)) +
geom_point(aes(x = female_diff),alpha = 0.8, shape =24, size = 5) +
scale_y_continuous(breaks = 1:nrow(exampledf), labels = exampledf$nloci) +
xlab("difference in proportion") +
ylab("correlation") +
geom_vline(
xintercept = 0,
color = 'black',
linetype = 'dashed',
alpha = .5
) +
theme_minimal() + theme(text = element_text(size = 12)) + guides(alpha=FALSE) + guides(size=FALSE) +
guides(shape = FALSE) + guides(color = FALSE)
这是绘图代码中名为 exampledf
的数据集:
structure(list(male_diff = c(0.4668, -0.03299, 0.702, -0.11544,
0.689, 0.511, -0.0725, -0.12844, -0.0827, 0.6515, -0.01077, 0.006,
0.0041, -0.00856, 0.4181, -0.02765), female_diff = c(-0.459,
0.022, -0.155, 0.00800000000000001, -0.156, -0.326, -0.0224,
0.00700000000000001, 0.0399999999999999, 0.2182, -0.08458, 0.8844,
-0.8459, 0.122, -0.506, 0.03), expt_in_ms = c(1, 1, 2, 2, 3,
3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4), nloci = c(0, 20, 0, 20, 0,
0, 0, 20, 20, 20, 0, 20, 0, 20, 0, 20), index = 1:16), row.names = c(NA,
-16L), class = c("tbl_df", "tbl", "data.frame"))
您可以在 labels
参数中使用嵌套的 gsub()
。这里,\u2212
是 unicode 减号,但您也可以使用连字符。正则表达式位 ^
表示 'starts with',而 $
表示 'ends with',因此 ^0$
应该匹配精确的 0。
library(tidyverse)
exampledf <- structure(
list(male_diff = c(0.4668, -0.03299, 0.702, -0.11544, 0.689, 0.511, -0.0725,
-0.12844, -0.0827, 0.6515, -0.01077, 0.006, 0.0041,
-0.00856, 0.4181, -0.02765),
female_diff = c(-0.459, 0.022, -0.155, 0.00800000000000001, -0.156,
-0.326, -0.0224, 0.00700000000000001, 0.0399999999999999,
0.2182, -0.08458, 0.8844, -0.8459, 0.122, -0.506, 0.03),
expt_in_ms = c(1, 1, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4),
nloci = c(0, 20, 0, 20, 0, 0, 0, 20, 20, 20, 0, 20, 0, 20, 0, 20),
index = 1:16),
row.names = c(NA, -16L), class = c("tbl_df", "tbl", "data.frame")
)
ggplot(exampledf, aes(x = male_diff, y = index, color = as.factor(expt_in_ms))) +
geom_point(aes(alpha = 0.5, size = 3)) +
geom_point(aes(x = female_diff),alpha = 0.8, shape =24, size = 5) +
scale_y_continuous(
breaks = 1:nrow(exampledf),
labels = gsub("^0$", "+", gsub("^20$", "\u2212", exampledf$nloci))
) +
xlab("difference in proportion") +
ylab("correlation") +
geom_vline(
xintercept = 0,
color = 'black',
linetype = 'dashed',
alpha = .5
) +
theme_minimal() +
theme(text = element_text(size = 12)) +
guides(alpha="none", size = "none", shape = "none", color = "none")
由 reprex package (v1.0.0)
于 2021-08-31 创建
我正在制作散点图,想用符号替换 y 轴的数字。具体来说,我想将 20 换成减号,将 0 换成加号。
我看过
谢谢。
这是我的绘图代码:
library(tidyverse)
ggplot(exampledf, aes(x = male_diff, y = index, color = as.factor(expt_in_ms))) +
geom_point(aes(alpha = 0.5, size = 3)) +
geom_point(aes(x = female_diff),alpha = 0.8, shape =24, size = 5) +
scale_y_continuous(breaks = 1:nrow(exampledf), labels = exampledf$nloci) +
xlab("difference in proportion") +
ylab("correlation") +
geom_vline(
xintercept = 0,
color = 'black',
linetype = 'dashed',
alpha = .5
) +
theme_minimal() + theme(text = element_text(size = 12)) + guides(alpha=FALSE) + guides(size=FALSE) +
guides(shape = FALSE) + guides(color = FALSE)
这是绘图代码中名为 exampledf
的数据集:
structure(list(male_diff = c(0.4668, -0.03299, 0.702, -0.11544,
0.689, 0.511, -0.0725, -0.12844, -0.0827, 0.6515, -0.01077, 0.006,
0.0041, -0.00856, 0.4181, -0.02765), female_diff = c(-0.459,
0.022, -0.155, 0.00800000000000001, -0.156, -0.326, -0.0224,
0.00700000000000001, 0.0399999999999999, 0.2182, -0.08458, 0.8844,
-0.8459, 0.122, -0.506, 0.03), expt_in_ms = c(1, 1, 2, 2, 3,
3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4), nloci = c(0, 20, 0, 20, 0,
0, 0, 20, 20, 20, 0, 20, 0, 20, 0, 20), index = 1:16), row.names = c(NA,
-16L), class = c("tbl_df", "tbl", "data.frame"))
您可以在 labels
参数中使用嵌套的 gsub()
。这里,\u2212
是 unicode 减号,但您也可以使用连字符。正则表达式位 ^
表示 'starts with',而 $
表示 'ends with',因此 ^0$
应该匹配精确的 0。
library(tidyverse)
exampledf <- structure(
list(male_diff = c(0.4668, -0.03299, 0.702, -0.11544, 0.689, 0.511, -0.0725,
-0.12844, -0.0827, 0.6515, -0.01077, 0.006, 0.0041,
-0.00856, 0.4181, -0.02765),
female_diff = c(-0.459, 0.022, -0.155, 0.00800000000000001, -0.156,
-0.326, -0.0224, 0.00700000000000001, 0.0399999999999999,
0.2182, -0.08458, 0.8844, -0.8459, 0.122, -0.506, 0.03),
expt_in_ms = c(1, 1, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4),
nloci = c(0, 20, 0, 20, 0, 0, 0, 20, 20, 20, 0, 20, 0, 20, 0, 20),
index = 1:16),
row.names = c(NA, -16L), class = c("tbl_df", "tbl", "data.frame")
)
ggplot(exampledf, aes(x = male_diff, y = index, color = as.factor(expt_in_ms))) +
geom_point(aes(alpha = 0.5, size = 3)) +
geom_point(aes(x = female_diff),alpha = 0.8, shape =24, size = 5) +
scale_y_continuous(
breaks = 1:nrow(exampledf),
labels = gsub("^0$", "+", gsub("^20$", "\u2212", exampledf$nloci))
) +
xlab("difference in proportion") +
ylab("correlation") +
geom_vline(
xintercept = 0,
color = 'black',
linetype = 'dashed',
alpha = .5
) +
theme_minimal() +
theme(text = element_text(size = 12)) +
guides(alpha="none", size = "none", shape = "none", color = "none")
由 reprex package (v1.0.0)
于 2021-08-31 创建