[R] GGplot 2 – cannot get histogram and box plot axis to match.

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Fri Jul 26 10:03:32 CEST 2013


Dear John,

Use xlim() and ylim() instead of expand_limits()

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 <- diff(range(dataSet$MPG.city))

graphRange <- c(min(dataSet$MPG.city) - dataRange/5,
                max(dataSet$MPG.city) + dataRange/5)

#making the box plot
theBoxPlot <- ggplot(dataSet,aes_string(x = "MPG.city", y = "MPG.city"))

theBoxPlot <-
  theBoxPlot  + geom_boxplot() + coord_flip() + ylim(limits = graphRange)
print(theBoxPlot)


#making the histogram
thePlot <- ggplot(dataSet,aes_string(x = "MPG.city"))
thePlot <- thePlot + geom_histogram()  + xlim(graphRange)
print(thePlot)



ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest
team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
Kliniekstraat 25
1070 Anderlecht
Belgium
+ 32 2 525 02 51
+ 32 54 43 61 85
Thierry.Onkelinx op inbo.be
www.inbo.be

To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey

-----Oorspronkelijk bericht-----
Van: r-help-bounces op r-project.org [mailto:r-help-bounces op r-project.org] Namens John W. Clow
Verzonden: donderdag 25 juli 2013 20:03
Aan: r-help op r-project.org
Onderwerp: [R] GGplot 2 – cannot get histogram and box plot axis to match.

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

______________________________________________
R-help op r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
* * * * * * * * * * * * * D I S C L A I M E R * * * * * * * * * * * * *
Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is door een geldig ondertekend document.
The views expressed in this message and any annex are purely those of the writer and may not be regarded as stating an official position of INBO, as long as the message is not confirmed by a duly signed document.



More information about the R-help mailing list