最初的情节是:
rect <- data.frame(x = 100, y = 50)
line <- data.frame(x = c(1, 200), y = c(100, 1))
library(ggplot2)
plot1 <- ggplot(mapping = aes(x, y)) +
scale_x_continuous(limits=c(1, 200), expand=c(0,0), breaks = c(1,50,100,150,200)) +
scale_y_continuous(limits=c(1, 100), expand=c(0,0), breaks = c(1,25,50,75,100)) +
theme(axis.title=element_text(colour="black",size=22)) +
theme(axis.text=element_text(colour="black",size=20)) +
theme(plot.margin=unit(c(0.3, 0.8, 0.1, 0.2),"cm")) +
geom_tile(data = rect, aes(width = 100, height = 50), alpha = 0.4) +
geom_line(data = line) +
theme(panel.grid.major=element_line(colour="grey60"))
plot1
目标是将轴zoom 到modulus_trans(0.3)
,以使黑线跟随这些变换,而不是灰色框,灰色框必须根据未变换坐标的比率保持位置(即,从x和y的25%开始,然后覆盖50%的宽度和高度).
不幸的是,coord_trans
函数不仅可以适当地转换刻度和线条,还可以转换方框:
library(scales)
plot2 <- ggplot(mapping = aes(x, y)) +
scale_x_continuous(limits=c(1, 200), expand=c(0,0), breaks = c(1,50,100,150,200)) +
scale_y_continuous(limits=c(1, 100), expand=c(0,0), breaks = c(1,25,50,75,100)) +
theme(axis.title=element_text(colour="black",size=22)) +
theme(axis.text=element_text(colour="black",size=20)) +
theme(plot.margin=unit(c(0.3, 0.8, 0.1, 0.2),"cm")) +
geom_tile(data = rect, aes(width = 100, height = 50), alpha = 0.4) +
geom_line(data = line) +
theme(panel.grid.major=element_line(colour="grey60")) +
coord_trans(x = modulus_trans(0.3), y = modulus_trans(0.3))
plot2
有没有一种简单的方法可以根据x和y的百分比定位长方体,而不是手动定位,从而生成以下所需的图形?
谢谢你的帮助