For which I would like the legend to look like this other plot:
Because p
is a combination of two different plots p1
and p2
, I used:
p<- grid.arrange(p1, p2, ncol = 3, widths = c(3,5,0))
And only the legend for p2 was kept (which doesn't include "Collar ID"=41365´, which is what I would like to ad)
Hence, I thought the easiest was to create a legend manually. In case that is needed, I used the script below to create p
:
df1 <- tibble::tribble(~Proportion, ~Lower,~Upper, ~Time,~Collar,
0.242, 0.173, 0.329, "Day","41361´",
0.216, 0.152, 0.296, "Night","41361´")
df2 <- tibble::tribble(~Proportion, ~Lower,~Upper, ~Time,~Collar,
0.290, 0.214, 0.381, "Day","41366´",
0.256, 0.186, 0.342, "Night","41366´")
df<-rbind(df1,df2)
dfnew <- df %>%
mutate(ymin = Proportion - Lower,
ymax = Proportion + Upper,
linegroup = paste(Time, Collar))
set.seed(2)
myjit <- ggproto("fixJitter", PositionDodge,
width = 0.6,
dodge.width = 0,
jit = NULL,
compute_panel = function (self, data, params, scales)
{
#Generate Jitter if not yet
if(is.null(self$jit) ) {
self$jit <-jitter(rep(0, nrow(data)), amount=self$dodge.width)
}
data <- ggproto_parent(PositionDodge, self)$compute_panel(data, params, scales)
data$x <- data$x + self$jit
#For proper error extensions
if("xmin" %in% colnames(data)) data$xmin <- data$xmin + self$jit
if("xmax" %in% colnames(data)) data$xmax <- data$xmax + self$jit
data
} )
p1<-ggplot(data = dfnew, aes(x = Time, y = Proportion, group=linegroup)) +
geom_point(aes(shape = as.character(Collar)), size = 4, stroke = 0,
position = myjit)+
geom_line(aes(group = linegroup),linetype = "dotted",size=1, position = myjit) +
theme(axis.text=element_text(size=15),
axis.title=element_text(size=20)) +
geom_errorbar(aes(ymin = Lower, ymax = Upper), width=0.3, size=1,
position = myjit) + scale_shape_manual(values=c("41361´"=19,"41366´"=15)) +
scale_color_manual(values = c("Day" = "black",
"Night" = "black")) + labs(shape="Collar ID") + ylim(0.05, 0.4) + theme(legend.position = "none")
p1
df1 <- tibble::tribble(~Proportion, ~Lower,~Upper, ~Area,~Collar,
0.181, 0.148, 0.219, "LGCA","41361´",
0.289, 0.242 ,0.341 , "SNP","41361´")
df2 <- tibble::tribble(~Proportion, ~Lower,~Upper, ~Area,~Collar,
0.099, 0.096, 0.104, "LGCA","41365´",
0.224, 0.217 ,0.232 , "SNP","41365´")
df<-rbind(df1,df2)
dfnew <- df %>%
mutate(ymin = Proportion - Lower,
ymax = Proportion + Upper,
linegroup = paste(Area, Collar))
set.seed(2)
myjit <- ggproto("fixJitter", PositionDodge,
width = 0.6,
dodge.width = 0,
jit = NULL,
compute_panel = function (self, data, params, scales)
{
#Generate Jitter if not yet
if(is.null(self$jit) ) {
self$jit <-jitter(rep(0, nrow(data)), amount=self$dodge.width)
}
data <- ggproto_parent(PositionDodge, self)$compute_panel(data, params, scales)
data$x <- data$x + self$jit
#For proper error extensions
if("xmin" %in% colnames(data)) data$xmin <- data$xmin + self$jit
if("xmax" %in% colnames(data)) data$xmax <- data$xmax + self$jit
data
} )
p2<-ggplot(data = dfnew, aes(x = Area, y = Proportion, group=linegroup)) +
geom_point(aes(shape = as.character(Collar)), size = 4, stroke = 0,
position = myjit)+
geom_line(aes(group = linegroup),linetype = "dotted",size=1, position = myjit) +
theme(axis.text=element_text(size=15),
axis.title=element_text(size=20)) +
geom_errorbar(aes(ymin = Lower, ymax = Upper), width=0.3, size=1,
position = myjit) + scale_shape_manual(values=c("41361´"=19,"41365´"=17)) + scale_size_manual(values=c(2,2)) +
scale_color_manual(values = c("SNP" = "black",
"LGCA" = "black")) + labs(shape="Collar ID") + ylim(0.05, 0.4) +
theme(legend.text=element_text(size=18))+
theme(legend.title = element_text(size=18))
#+ theme(legend.position = "none")
p2
p<- grid.arrange(p1, p2, ncol = 3, widths = c(3,5,0))
Please let me know if there's a better solution for this. Any help is appreciated!