6

我的输入文件在 PasteBin 上

我当前的图形代码是:

 #Input and data formatting
 merg_agg_creek<-read.table("merged aggregated creek.txt",header=TRUE)
 library(ggplot2)
 library(grid)
 source("http://egret.psychol.cam.ac.uk/statistics/R/extensions/rnc_ggplot2_border_themes.r")

 CombinedCreek<-data.frame(merg_agg_creek)
 Combined<-CombinedCreek[order(CombinedCreek[,2]),]
 Combined$Creek <- factor(rep(c('Culvert Creek','North Silcox','South Silcox','Yucca Pen'),c(32,57,51,31)))  
 Combined$Creek<-factor(Combined$Creek,levels(Combined$Creek)[c(1,4,3,2)])

 #The Graph Code

 creek <-ggplot(Combined,aes(Month,Density,color=factor(Year),shape=factor(Year)))+scale_color_discrete("Year")+scale_shape_discrete("Year")
 creek<-creek + facet_grid(Creek~. ,scales = "free_y")
 creek <- creek + geom_jitter(position = position_jitter(width = .3))
 creek<-creek+scale_color_grey("Year",end=.6)+theme_bw()
 creek<-creek+scale_y_continuous(expression("Number of prey captured " (m^2) ^-1))
 creek<-creek+opts( panel.border = theme_L_border() )+ opts(axis.line = theme_segment())
 creek<-creek+opts(panel.grid.minor = theme_blank())+opts(panel.grid.major = theme_blank())
 creek<-creek+scale_x_discrete("Month",breaks=c(2,5,8,11),labels=c("February","May","August","November"))
 creek

结果图是: Graph
图形

我的问题是,通过在“scale_x_discrete”中创建中断和标签,在图的右侧,12 月的数据和构面标签之间存在很大的差距。我尝试通过在“scale_x_discrete: 命令”中添加“limits=c(0,13)”来消除这个差距,但生成的图形会破坏 x-labels。

我如何消除这个差距?我的情节创作是否存在根本性缺陷?

谢谢!

编辑:Didzis 回答了下面的问题。我只需要从 scale_x_discrete 更改为 scale_x_continuous

4

2 回答 2

4

由于数据中的月份是数字,请尝试替换

 scale_x_discrete("Month",breaks=c(2,5,8,11),labels=c("February","May","August","November")) 

 scale_x_continuous("Month",breaks=c(2,5,8,11),labels=c("February","May","August","November"))

保持所有其他参数相同

于 2012-12-05T05:52:13.730 回答
0

您需要在比例中使用 expand 参数。我认为额外的空间可能是由于抖动造成的,但如果你给它一个-2,它就会一直到边缘。几乎看起来像一个错误,它填充了这么多。

ggplot(Combined,aes(Month,Density,color=factor(Year),shape=factor(Year))) +
scale_shape_discrete("Year") +
facet_grid(Creek~. ,scales = "free_y") +
geom_jitter(position = position_jitter(width = .3)) +
scale_color_grey("Year",end=.6) +
theme_bw() +
scale_y_continuous(expression("Number of prey captured " (m^2) ^-1))+
scale_x_discrete("Month",breaks=c(2,5,8,11),labels=c("February","May","August","November"), expand= c(0,-2)) +
theme(
panel.grid.major=element_blank(),
panel.grid.minor=element_blank()
)
于 2012-12-04T21:03:45.000 回答