[R] Grouped boxplots using ggplot() from ggplot2.

Rolf Turner r@turner @end|ng |rom @uck|@nd@@c@nz
Sat Jul 28 11:34:47 CEST 2018


On 28/07/18 17:03, Jeff Newmiller wrote:

> When you understand the strong dependence on how the data controls 
> ggplot, using it gets much easier. I still have to google details 
> sometimes though. Note that it can be very difficult to make a weird 
> plot (e.g. multiple parallel axes) in ggplot because it is very 
> internally consistent... a blessing and a curse.
> 
> 1) Colour is assigned in the scale according to order of levels of the 
> factor. Note that while they are both discrete, the so-called "discrete" 
> scales auto-colour, but "manual" scales require you to specify the exact 
> colour sequence.
> 
> 2) Assigning constants to properties is done outside the mapping (aes). 
> Note that "colour" is for lines and shapes outlines, while "fill" is 
> colour meant to fill in shapes. When the names of these two scales are 
> the same and the values are the same, the legends will merge. If not, 
> they will be shown separately.
> 
> 3) Discrete scales are controlled by the levels in the data. To prevent 
> ggplot from removing missing levels, use the drop=FALSE argument.
> 
> 4) Breaks are a property of the scale.
> 
> My changes were:
> 
> 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) ) +
>      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 ) )
> 
> Good luck with your graphics grammar!

Dear Jeff,

Thanks very much for this cogent advice and for taking the trouble to 
steer me in the right direction.  However I am not quite out of the 
woods yet.

(1) I'm still getting two legends.  How do I stop this from happening?

(2) The boxes are "filled" (with pinkish and blueish colours --- which 
are referenced in the second of the two legends that I get).  How can I 
get "unfilled" boxes?

(3) The y-axis scale runs only from 800 to 1800, rather than from 700 to 
2100.  How can I force it to run from 700 to 2100?

(4) With the modified code we now get some "outliers" (points beyond the 
whisker tips) plotted --- which I didn't get before (and don't want, 
because "last year's" graphics did not include outliers).  How can I 
suppress the plotting of outliers?

I have attached a pdf containing the results of running the code that
you provided, so that you can readily see what is happening.

May I prevail upon your good graces to enlighten me about questions
(1) --- (4) above?

Ever so humbly grateful.

cheers,

Rolf

-- 
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

-------------- next part --------------
A non-text attachment was scrubbed...
Name: demoPlot.pdf
Type: application/pdf
Size: 5772 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20180728/ea156d7a/attachment-0002.pdf>


More information about the R-help mailing list