断轴和断裂轴之间的间距在ggplot中标记?

时间:2015-09-18 20:35:23

标签: r ggplot2

两部分问题:在ggplot中,我试图增加轴间距之间的间距,这样当我在平均值和置信区间旁边偏移“原始数据”点时,数据点属于左边的矩形(例如,如果1988年和1989年之间的差距在纸上是1英寸,我怎么能做到2英寸)?

其次,我想知道是否有一种方法可以在轴上创建断点,这样我就可以在缺少数据的年份中消除空白区域。代码如下,但我希望看起来像这个例子

http://gnuplot-surprising.blogspot.com/2011/10/broken-axes-graph-in-gnuplot-3.html

set.seed(1)
mean<-rnorm(100,0,2)
years<-c(1988,1989,1990,2001,2002,2003,2012,2013,2014)
winter<-sample(years, 100,replace=T)
data<-as.data.frame(cbind(mean, winter))

library("Rmisc")
sum<-summarySE(data,measurevar="mean",groupvars=c("winter"))

library(ggplot2)
p<-ggplot(sum, aes(x=winter, y=mean, group=winter))+
geom_rect(aes(ymin=mean-ci, ymax=mean+ci, xmin=winter-.2, xmax=winter+.2), fill="gray")+
geom_point(shape=18, size=3)+
scale_x_continuous(breaks=seq(1985,2014,5))+
scale_y_continuous(breaks=seq(-2,4,1))+
geom_abline(intercept=0,slope=0, lty=2)+
theme_bw()

p2<-p + geom_point(data=data, aes(x=winter+.3,y=mean),shape=1, size=2.5)+
  scale_shape()
p2

1 个答案:

答案 0 :(得分:0)

正如@joran所述,ggplot中的轴断裂是不行的。 Facets通常提供最佳解决方案。也许这会对你有所帮助:

p<-ggplot(sum, aes(x=winter, y=mean, group=winter))+
  geom_rect(aes(ymin=mean-ci, ymax=mean+ci, xmin=winter-.2, xmax=winter+.2), fill="gray")+
  geom_point(shape=18, size=3)+
  scale_x_continuous(breaks=seq(1985,2014,1))+
  scale_y_continuous(breaks=seq(-2,4,1))+
  geom_abline(intercept=0,slope=0, lty=2)+
  theme_bw()

p2<-p + geom_point(data=data, aes(x=winter+.3,y=mean),shape=1, size=2.5)+
  scale_shape()
p2 + facet_wrap(~winter,nrow=1,scales="free_x")

enter image description here

BTW:您是否考虑过使用boxplot进行此类数据可视化?

相关问题