[R] Grouped boxplots using ggplot() from ggplot2.
Rolf Turner
r@turner @end|ng |rom @uck|@nd@@c@nz
Sun Jul 29 01:04:02 CEST 2018
On 29/07/18 02:54, Jeff Newmiller wrote:
> 1) I don't know... it looks to me like you did not run my code.
Aaaarrrgghhh. I *thought* I had, but instead left "fill=Type" inside
the aes() call and neglected to add fill=NULL outside this call.
Duhhhh!!! It's tough being mentally challenged, let me assure you.
> I have
> included a complete reprex below... try it out in a fresh session. If
> you still get the problem, check your sessionInfo package versions
> against mine.
Yep. Works like a charm.
> 2) This still smells like your fill parameter is inside the aes function
> with Type as value. This causes a legend to be created, and since that
> legend has a different name ("Type") than the colour scale, they are
> separated. Confirm that you are using fill outside the aes function
> (because you don't want fill to depend on the data) and have the
> constant NULL as value (so it won't generate any fill graphical
> representation).
Yeah. Well. Duhhh. I'm a retread.
>
> 3) I missed that... the ylim()/scales_y_continuous(breaks=) limits
> constrain which data are included as input into the graph. The
> coord_cartesian function forces the limits as desired.
Bewdy, ta.
>
> 4) While showing outliers is a standard semantic feature of boxplots
> whether produced by ggplot or lattice or base or non-R solution,
Indeed. But the client is always right! :-)
> you can
> please the client by making the outliers transparent.
And your code shows me how! Which I need. Bewdy, ta.
> There is a link to the generated image below.
>
> ################
> # Simulate some data:
> Type <- rep( c( "National", "Local" ), each = 250 )
> M0 <- 1300+50*(0:4)
> set.seed( 42 )
> M1 <- M0 + runif( 5, -100, -50 )
> X0 <- rnorm( 250, rep( M0, each = 50 ), 150 )
> X1 <- rnorm( 250, rep( M1, each = 50 ), 100 )
>
> library(ggplot2)
> Year <- factor( rep( 4:8, each = 50, times = 2)
> , levels = 0:8 )
> DemoDat <- data.frame( Year = Year
> , Score = c( X0, X1 )
> , Type = Type
> )
>
> ggplot( data = DemoDat
> , aes( x = Year
> , y = Score
> , color = Type
> )
> , fill = NULL
> ) +
> geom_boxplot( position = position_dodge( 1 )
> , outlier.alpha = 0
> ) +
> theme_minimal() +
> scale_colour_manual( name = "National v. Local"
> , values = c( "red", "black" ) ) +
> scale_x_discrete( drop = FALSE ) +
> scale_y_continuous( breaks=seq( 700, 2100, 100 ) ) +
> coord_cartesian( ylim = c( 700, 2100 ) )
>
> # ![](https://i.imgur.com/wUVYU5H.png)
Looks perfect. Thanks *HUGELY* for your patience with my stupidity.
<SNIP>
cheers,
Rolf
--
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276
More information about the R-help
mailing list