给定下面提供的数据和ggplot2
代码,我想在辅助轴上添加蓝色标题“x1”和“y1”,如下所示:
你知道吗?
多谢帮忙
数据
dat <-
structure(list(x0 = c(15, 30, 45, 15, 30, 45, 15, 30, 45, 15,
30, 45, 15, 30, 45, 15, 30, 45, 15, 30, 45, 15, 30, 45, 15, 30,
45, 15, 30, 45, 15, 30, 45, 15, 30, 45, 15, 30, 45, 15, 30, 45,
15, 30, 45, 15, 30, 45, 15, 30, 45, 15, 30, 45, 15, 30, 45, 15,
30, 45, 15, 30, 45, 15, 30, 45, 15, 30, 45, 15, 30, 45, 15, 30,
45, 15, 30, 45, 15, 30, 45), y0 = c(2, 2, 2, 7, 7, 7, 20, 20,
20, 2, 2, 2, 7, 7, 7, 20, 20, 20, 2, 2, 2, 7, 7, 7, 20, 20, 20,
2, 2, 2, 7, 7, 7, 20, 20, 20, 2, 2, 2, 7, 7, 7, 20, 20, 20, 2,
2, 2, 7, 7, 7, 20, 20, 20, 2, 2, 2, 7, 7, 7, 20, 20, 20, 2, 2,
2, 7, 7, 7, 20, 20, 20, 2, 2, 2, 7, 7, 7, 20, 20, 20), x1 = c(2,
2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
4, 4, 4, 4, 4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3,
3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3,
3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4), y1 = c(50,
50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50,
50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 250, 250, 250, 250, 250,
250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250,
250, 250, 250, 250, 250, 250, 250, 250, 250, 500, 500, 500, 500,
500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500,
500, 500, 500, 500, 500, 500, 500, 500, 500, 500), percent = c(0.99,
2.69, 4.91, -0.23, -0.22, 0.14, -1.5, -3.15, -4.61, 0.53, 1.65,
3.43, -0.56, -0.81, -0.36, -0.34, -0.79, -1.21, -0.2, -0.29,
-0.13, -0.64, -1.33, -1.59, 0.29, 0.68, 1.23, -1.16, -3.04, -4.94,
-0.46, -1.23, -2.19, -1.06, -2.53, -3.88, 0.06, 0.11, 0.2, -0.42,
-1.01, -1.38, -2.49, -5.71, -7.74, 0.09, 0.43, 1.4, -0.8, -1.38,
-0.84, -2.13, -4.74, -6.41, -0.27, -0.64, -1.05, -0.27, -0.71,
-1.22, -0.6, -1.46, -2.3, 0.12, 0.3, 0.43, 0.12, 0.46, 0.74,
-0.44, -1, -1.61, 0.88, 2.04, 3.11, 0.68, 1.64, 2.48, -0.98,
-2.14, -3.11)), class = "data.frame", row.names = c(NA, -81L))
代码
library(ggplot2)
ggplot(dat, aes(x = factor(x0), y = factor(y0))) +
geom_tile(aes(fill = percent)) +
scale_fill_gradientn(
colours=c("red2", "white", "green"),
values = scales::rescale(c(-10, 0, 5)),
limits=c(-10, 5)) +
geom_text(aes(label = round(percent, 0))) +
facet_grid(y1 ~ x1) +
theme_bw() +
theme(axis.text.x = element_text(colour="black",size=13.1, angle=0, margin=margin(b=4), hjust = 0.5, vjust = 0.5)) +
theme(axis.text.y = element_text(colour="black",size=13.1, margin=margin(l=6), hjust = -0.1, vjust = 0.5)) +
theme(panel.grid.major=element_line(colour="white",size=0.1))+
theme(axis.ticks=element_blank()) +
theme(plot.margin=unit(c(1.2,0.5,0.05,0.05),"cm")) +
theme(legend.position = "right", legend.box.margin=margin(-10,-10,-10,-10)) +
theme(strip.text = element_text(size = 12, color = "black")) +
xlab(~paste("x0")) +
ylab(~paste("y0")) +
theme(axis.title=element_text(size=18))
1条答案
按热度按时间ddrv8njm1#
要实现所需的结果,一个选项是切换到连续的
x
和y
刻度,这允许通过辅助刻度添加辅助轴标题。要获得离散刻度的外观,请使用as.numeric(factor(x))
将x0
和y0
列转换为数字。此外,您必须显式设置breaks
和labels
。