[R] legend in ggplot2

Thierry Onkelinx thierry.onkelinx at inbo.be
Thu Feb 2 15:12:26 CET 2017


Dear Ivan,

You're making things too complicated.

ggplot(mydata, aes(x = Spot, y = Value)) +
  geom_boxplot(aes(colour = Equipment), outlier.colour = "red") +
  geom_jitter() +
  facet_wrap(~Equipment, scales = "free_y") +
  scale_colour_manual(values = c(Leeb = "blue", Shore = "red"))

Best regards,

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

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

2017-02-02 14:59 GMT+01:00 Ivan Calandra <calandra op rgzm.de>:

> Dear useRs,
>
> I have been using base graphics since a long time and I'm currently trying
> to switch to ggplot2. I'm struggling with the legend (which probably means
> that my graphic commands are not optimal).
> I have copied the output from dput(mydata) at the end of the email.
>
> Here is what I have tried:
> library(ggplot2)
> library(cowplot)
> colo <- c("#F8766D", "#00BFC4")
> p1 <- ggplot(data=mydata[mydata$Equipment=="Shore",],
> aes(x=Spot,y=Value)) + geom_boxplot(outlier.size=2,fi
> ll=colo[1],outlier.color=colo[1]) + geom_jitter()
> p2 <- ggplot(data=mydata[mydata$Equipment=="Leeb",], aes(x=Spot,y=Value))
> + geom_boxplot(outlier.size=2,fill=colo[2],outlier.color=colo[2]) +
> geom_jitter()
> plot_grid(p1, p2, nrow=2, ncol=1)
>
> The plot is fine, but there is no legend at all. I know this has to do
> with 'color' and 'fill' arguments to aes(), but I got lost with all of
> this...
> Ideally, I am looking for this kind of structure in the legend:
> Equipment
>     [red box] Shore
>     [blue box] Leeb
> Points
>     [black dot] Measurements
>     [red/blue dot] Outliers
>
> Thank you in advance for your help!
> Bests,
> Ivan
>
>
> mydata <- structure(list(Equipment = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
> 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
> 2L), .Label = c("Leeb", "Shore"), class = "factor"), Spot = structure(c(1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
> 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
> 3L, 3L), .Label = c("1", "2", "3"), class = "factor"), Measurement =
> structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L,
> 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L,
> 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L,
> 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), .Label = c("1","2", "3", "4", "5",
> "6", "7", "8", "9", "10"), class = "factor"), Value = c(540, 640, 587, 593,
> 677, 675, 626, 625, 635, 645, 492, 538, 560, 565, 560, 543, 560, 555, 580,
> 577, 645, 645, 667, 658!
> , 652, 667, 652, 659, 669, 671, 89, 90.5, 93, 91, 91.5, 94, 94, 91.5,
> 92.5, 91.5, 82, 82, 78.5, 84.5, 83.5, 87, 88, 88.5, 83.5, 88, 89, 87, 87,
> 88, 92, 90.5, 88, 90.5, 93, 92.5)), .Names = c("Equipment", "Spot",
> "Measurement", "Value"), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
> 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L,
> 25L, 26L, 27L, 28L, 29L, 30L, 101L, 102L, 103L, 104L, 105L, 106L, 107L,
> 108L, 109L, 110L, 111L, 112L, 113L, 114L, 115L, 116L, 117L, 118L, 119L,
> 120L, 121L, 122L, 123L, 124L, 125L, 126L, 127L, 128L, 129L, 130L), class =
> "data.frame")
>
>
> --
>
>
> --
> Ivan Calandra, PhD
> MONREPOS Archaeological Research Centre and
> Museum for Human Behavioural Evolution
> Schloss Monrepos
> 56567 Neuwied, Germany
> calandra op rgzm.de
> +49 (0) 2631 9772-243
> https://www.researchgate.net/profile/Ivan_Calandra
> https://rgzm.academia.edu/IvanCalandra
> https://publons.com/author/705639/
>
> ______________________________________________
> R-help op r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posti
> ng-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list