Reputation: 291
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
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
Upvotes: 6
Views: 2114
Reputation: 98419
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
Upvotes: 4
Reputation: 251
You need to use the expand argument in the scale. I think the extra space might be due to the jitter, but if you give it a -2 it goes all the way to the edge. Almost seems like a bug that it's padding so much.
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()
)
Upvotes: 0