Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove gap at end of ggplot2 graph

Tags:

r

ggplot2

My input file is here on PasteBin.

My current graph code is:

 #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

The resulting graph is: Graph
graph

My issue is that by creating the breaks and labels in "scale_x_discrete", a large gap exists on the righthand side of the plot, between the data in December and the facet labels. I tried eliminating this gap by adding "limits=c(0,13)" to the "scale_x_discrete: command, but the resulting graph destroys the x-labels.

How do I remove that gap? Is there something fundamentally flawed in my plot creation?

Thanks!

EDIT: Didzis answered the question below. I just need to change from scale_x_discrete to scale_x_continuous

like image 652
user1399311 Avatar asked Dec 04 '12 20:12

user1399311


1 Answers

As the Month in your data is numerical, try replace

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

with

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

leaving all other parameters the same

like image 187
Didzis Elferts Avatar answered Oct 09 '22 11:10

Didzis Elferts