[R] legend in ggplot2

Ivan Calandra calandra at rgzm.de
Thu Feb 2 14:59:05 CET 2017


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,fill=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 at rgzm.de
+49 (0) 2631 9772-243
https://www.researchgate.net/profile/Ivan_Calandra
https://rgzm.academia.edu/IvanCalandra
https://publons.com/author/705639/



More information about the R-help mailing list