带ggplot的R型轴 [英] R-style axes with ggplot

查看:143
本文介绍了带ggplot的R型轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ggplot,是否有可能获得在原点不符合的R-stylex / y轴,而是由两个断开的范围组成,如下例所示:





这里的原因主要是让ggplot图看起来与pure-R一致。

试试这个,

  library(ggplot2)

d < - data.frame(x = 1:10,y = rnorm(10))

base_breaks_x< - function(x){
b < - pretty(x)
d< - data.frame(y = -Inf, yend = -Inf,x = min(b),xend = max(b))
list(geom_segment(data = d,aes(x = x,y = y,xend = xend,yend = yend),
scale_x_continuous(breaks = b))
}
base_breaks_y< - function(x){
b< - pretty(x)
d < - data.frame(x = -Inf,xend = -Inf,y = min(b),yend = max(b))
list(geom_segment(data = d,aes(x = x, y = y,xend = xend,y (b,b = b))
}

ggplot(d,aes(x,y))+
geom_point()+
theme_bw()+
theme(panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid。 minor = element_blank())+
base_breaks_x(d $ x)+
base_breaks_y(d $ y)



编辑:ggtheme包中已经讨论过相关问题,并有可能提供清洁剂解决方案(不需要将数据明确地提供给休息功能)。

Using ggplot, is it possible to get "R-style" x/y axes that don't meet at the origin and that instead consist of two disconnected ranges, as in the following example?

The reason here is mainly to get ggplot plots to look consistent next to pure-R ones.

解决方案

Try this,

library(ggplot2)

d <- data.frame(x=1:10, y=rnorm(10))

base_breaks_x <- function(x){
  b <- pretty(x)
  d <- data.frame(y=-Inf, yend=-Inf, x=min(b), xend=max(b))
  list(geom_segment(data=d, aes(x=x, y=y, xend=xend, yend=yend), inherit.aes=FALSE),
       scale_x_continuous(breaks=b))
}
base_breaks_y <- function(x){
  b <- pretty(x)
  d <- data.frame(x=-Inf, xend=-Inf, y=min(b), yend=max(b))
  list(geom_segment(data=d, aes(x=x, y=y, xend=xend, yend=yend), inherit.aes=FALSE),
       scale_y_continuous(breaks=b))
}

ggplot(d, aes(x,y)) +
  geom_point() +
  theme_bw() +
  theme(panel.border = element_blank(),
       panel.grid.major = element_blank(),
       panel.grid.minor = element_blank()) +
  base_breaks_x(d$x) +
  base_breaks_y(d$y)

Edit: a related issue has since been discussed in the ggtheme package, and potentially provides a cleaner solution (no need to provide the data explicitly to the breaks function).

这篇关于带ggplot的R型轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆