如何指定 x & y 轴刻度之间的距离?

时间:2021-02-05 13:12:19

标签: r ggplot2

我想创建一个带有自定义轴刻度间距的图。我想要实现的类似于这个情节: enter image description here

我可以使用 break 参数指定轴刻度位置,但我无法更改它们之间的距离。

dat <- data.frame(x = runif(100), y = runif(100))
ggplot(dat, aes(x,y)) + geom_point() + scale_x_continuous(breaks=c(0,0.1,0.2,0.4,0.8,1)) + scale_y_continuous(breaks=c(0,0.1,0.2,0.4,0.8,1))

enter image description here

我本质上想要的是专注于特定的间隔(比如 0:0.2),并为这个间隔设置更大的间距,然后压缩其余的(0.2:1)。 现在我通过为我想要的间隔创建两个图形来做到这一点,然后用 grid.arrange 将它们粘在一起,但我想知道是否有一种解决方案可以让我一次性生成绘图。

这是我目前的解决方案:

q1<-ggplot(dat, aes(x,y)) + geom_point() + ylim(c(0.2,1)) + xlim(c(0,0.2))+ theme(axis.text.x = element_blank(), axis.title.x = element_blank(), axis.ticks.x=element_blank())
q2<-ggplot(dat, aes(x,y)) + geom_point() + ylim(c(0.2,1)) + xlim(c(0.2,1))+ theme(axis.text = element_blank(), axis.title = element_blank(), axis.ticks=element_blank())
q3<-ggplot(dat, aes(x,y)) + geom_point() + ylim(c(0,0.2)) + xlim(c(0,0.2))
q4<-ggplot(dat, aes(x,y)) + geom_point() + ylim(c(0.2,1)) + xlim(c(0.2,1))+ theme(axis.text.y = element_blank(), axis.title.y = element_blank(), axis.ticks.y=element_blank())

grid.arrange(q1,q2,q3,q4)

enter image description here

2 个答案:

答案 0 :(得分:1)

好的,首先我必须做出强制性评论,即以将绘图上的位置与数据的直接连接断开的方式压缩部分数据通常不是一个好主意。

也就是说,您可以这样做。我们可以创建一个函数工厂,它产生一个具有比例因子的转换对象。函数工厂接受它应该压缩的范围和压缩数据的因子。我还没有对其进行详尽的测试,但我认为它可以正常工作。

const objectDepth = (o) =>
  Object (o) === o ? 1 + Math .max (-1, ... Object .values(o) .map (objectDepth)) : 0

console .log (objectDepth ({foo: {bar: {baz: 'baa'}}}))
console .log (objectDepth ({abc: 'xyz'}))

现在我们只需将函数工厂作为 library(ggplot2) library(scales) squish_trans <- function(range, factor = 10) { force(range) force(factor) forward <- function(x) { test_between <- x > range[1] & x < range[2] test_over <- x >= range[2] between <- ((x - range[1]) / factor) + range[1] over <- (x - range[2] + diff(range) / factor) + range[1] ifelse(test_over, over, ifelse(test_between, between, x)) } reverse <- function(x) { test_between <- x > range[1] & x < range[1] + diff(range) / factor test_over <- x >= range[1] + diff(range) / factor between <- ((x - range[1]) * factor) + range[1] over <- (x - range[1]) - diff(range) / factor + range[2] ifelse(test_over, over, ifelse(test_between, between, x)) } trans_new( "squish_trans", transform = forward, inverse = reverse ) } 参数运行,并使用您想要压缩的范围。您可以注意到 0.2-1 范围(数据范围的 80%)现在是轴范围的 0.08/0.28 ~= 0.28(~28%),因为我们用因子 10 进行挤压。

trans

reprex package (v1.0.0) 于 2021 年 2 月 5 日创建

答案 1 :(得分:0)

dat <- data.frame(x = runif(100), y = runif(100))
ggplot(dat, aes(x,y)) + 
  geom_point() + 
  scale_x_continuous(breaks=c(0,0.1,0.2,0.4,0.8,1)) + 
  scale_y_continuous(breaks=c(0,0.1,0.2,0.4,0.8,1))

enter image description here

dat$condx <- ifelse(dat$x > 0.2, "x2", "x1")
dat$condy <- ifelse(dat$y > 0.2, "y1", "y2")
dat$condxy <- paste(dat$condx, dat$condy)
ggplot(dat, aes(x, y, group=condxy)) + 
  geom_point() + 
  scale_x_continuous(breaks=c(0,0.05,0.1,0.15,0.2,0.4,0.6,0.8,1)) + 
  scale_y_continuous(breaks=c(0,0.05,0.1,0.15,0.2,0.4,0.6,0.8,1)) +
  facet_grid(condy~condx, scales="free")

enter image description here

(与Two scales in the same axis有关)

问候,