[R] GGplot 2 – cannot get histogram and box plot axis to match.
John W. Clow
jclow at umail.ucsb.edu
Thu Jul 25 20:02:35 CEST 2013
Problem:
I am trying to get the histogram and box plot x axis to match. I’ve tried using the expand_limits function to make the axis match but that didn’t make the axis match. The histogram’s axis are still consistently larger than the ones for the box plot (though the function did help). Does anyone have a suggestion as to what I should do instead?
Background:
I am building a Shiny app that displays a histogram below a bar chart for a set of data that a user uploads to the app. If you want to see the app, go here http://spark.rstudio.com/jclow/Archive20130725HistogramApp/
To run the app, select “Use Sample Data” , then select “MPG.city” under choose a column, then finally select box plot.
Sample code:
Below is a snippet of my code to demonstrate the problems I have.
library(ggplot2)
#sample data from ggplot2
data(Cars93, package = "MASS")
dataSet <- Cars93
#variables to calculate the range to extend the axis
dataVector <- unlist(dataSet[,"MPG.city"])
dataRange <- max(dataVector) - min(dataVector)
graphRange <- c(min(dataVector) - dataRange/5,
max(dataVector) + dataRange/5)
#making the box plot
theBoxPlot <- ggplot(dataSet,aes_string(x = "MPG.city",y = "MPG.city"))
theBoxPlot = theBoxPlot + geom_boxplot() + expand_limits(y= graphRange) + coord_flip()
print(theBoxPlot)
#making the histogram
thePlot <- ggplot(dataSet,aes_string(x = "MPG.city"))
thePlot <-thePlot + geom_histogram() + expand_limits(x= graphRange)
print(thePlot)
Thank you for taking the time to read this.
John Clow
UCSB Student
More information about the R-help
mailing list