how to move one legend at different position in r - r

ggplot(data =data,aes(x = Voting.Method, y = Accuracy, linetype =Type))+
geom_boxplot(fill="white", fatten = 0) +
stat_summary(fun = mean, geom = "errorbar", aes(ymax = ..y.., ymin = ..y.., color = "mean"),
width = .75, linetype = "solid") +
stat_summary(fun = median, geom = "errorbar", aes(ymax = ..y.., ymin = ..y.., color = "median"),
width = .75, linetype = "solid") +
theme(legend.title=element_blank(), legend.text = element_text(size=10, face="bold"),
legend.position =c(0.9,0.9),legend.direction = "horizontal") +
scale_y_continuous(limits = c(50, 75))
i want to move one legend to bottom such like that

Related

Changing legend symbol ggplot2 with multiple legends

I have a graph like attached and want to change the 'colour' legend to be a symbol of a square or circle not a line.
ggplot(sharkanovadata, aes(x=mean, y=sd), colour="Sharks")+
geom_point(alpha=0.2)+
geom_point(data=birdanovadata, colour= "#D22B2B", alpha=0.1)+
geom_abline(data=sharkanovadata, mapping=aes(colour="Sharks", slope=0.15, intercept=3.33, linetype= "Genetic"), size=1.2)+
geom_abline(data=sharkanovadata, mapping= aes( colour="Sharks", slope= 0.22, intercept=4.12, linetype= "Imputed"), size=1.2) +
geom_abline(data=birdanovadata, mapping=aes(colour= "Birds", slope=0.17, intercept=0.31, linetype= "Genetic"), size=1.2)+
geom_abline(data=birdanovadata, mapping=aes(colour= "Birds", slope=0.30, intercept=0.53, linetype="Imputed"), size=1.2)+
scale_colour_manual(values= c("Sharks" = "black", "Birds" = "#B03A2E"))+
xlab("Mean ED") +
ylab("Standard deviation of ED") +
ggtitle("Imputed species have a greater linear relationship of standard devation and mean", (size=30))+
theme(panel.background = element_rect(fill = "white"))+
theme(panel.grid.major = element_line(size=0.4, colour= "grey"))+
scale_linetype_manual(values= c("Genetic"= "longdash", "Imputed" = "solid"))+
theme(legend.key.size = unit(1, 'cm')) +
theme(plot.title = element_text(face = "bold"))
As the legend symbols aka key glyphs are determined by the geoms one option would be to switch the key_glyph for one of your geom_ablines. to point which will add a point to each legend. Afterwards we can tweak the the color and linetype legends via the override.aes argument of guide_legend where I opted for a square shape.
As you provided no data I use some fake random data to mimic your real data:
set.seed(123)
birdanovadata <- sharkanovadata <- data.frame(
mean = runif(1000, 0, 150),
sd = runif(1000, 0, 30)
)
library(ggplot2)
ggplot(sharkanovadata, aes(x = mean, y = sd), colour = "Sharks") +
geom_point(alpha = 0.2) +
geom_point(data = birdanovadata, colour = "#D22B2B", alpha = 0.1) +
geom_abline(data = sharkanovadata, mapping = aes(colour = "Sharks", slope = 0.15, intercept = 3.33, linetype = "Genetic"), size = 1.2, key_glyph = "point") +
geom_abline(data = sharkanovadata, mapping = aes(colour = "Sharks", slope = 0.22, intercept = 4.12, linetype = "Imputed"), size = 1.2) +
geom_abline(data = birdanovadata, mapping = aes(colour = "Birds", slope = 0.17, intercept = 0.31, linetype = "Genetic"), size = 1.2) +
geom_abline(data = birdanovadata, mapping = aes(colour = "Birds", slope = 0.30, intercept = 0.53, linetype = "Imputed"), size = 1.2) +
scale_colour_manual(values = c("Sharks" = "black", "Birds" = "#B03A2E")) +
xlab("Mean ED") +
ylab("Standard deviation of ED") +
ggtitle("Imputed species have a greater linear relationship of standard devation and mean", (size <- 30)) +
theme(panel.background = element_rect(fill = "white")) +
theme(panel.grid.major = element_line(size = 0.4, colour = "grey")) +
scale_linetype_manual(values = c("Genetic" = "longdash", "Imputed" = "solid")) +
theme(legend.key.size = unit(1, "cm")) +
theme(plot.title = element_text(face = "bold")) +
guides(color = guide_legend(override.aes = list(linetype = "blank", size = 8, shape = 15)),
linetype = guide_legend(override.aes = list(shape = NA)))

change part of whiskers of boxplot in r [duplicate]

This question already has an answer here:
How to customize whisker lines on a geom_box plot differently than the lines of the box itself
(1 answer)
Closed 9 months ago.
below code which i used for making boxlplot but i can't change some parts of whiskeres
so please notice to me how to change. i attached png.
ggplot(data =data,aes(x = Voting.Method, y = Accuracy))+
stat_boxplot(geom ='errorbar',width=0.15) +
geom_boxplot(fill="white", fatten = 0, size=1, width=0.3)+
stat_summary(fun = mean, geom = "errorbar", aes(ymax = ..y.., ymin = ..y.., color = "mean"),
width = .3, linetype = 1,size=1) +
stat_summary(fun = median, geom = "errorbar", aes(ymax = ..y.., ymin = ..y.., color = "median"),
width = .3, linetype = 1,size=1) +
theme(legend.title=element_blank(), legend.text = element_text(size=15, face="bold"), legend.background = element_rect(color="black"),
legend.position =c(0.9,0.9),legend.direction = "vertical", axis.text.x = element_text(size=15, angle = 20,vjust=0.5),
axis.text.y = element_text(size =15),axis.title.y = element_text(size =20),axis.title.x=element_blank()) +
scale_y_continuous(limits = c(50, 75)) +
scale_color_manual(values=c("skyblue","lightgreen"))+
scale_x_discrete(limits=c("LSTM","Bi-LSTM","GRU","Bi-GRU"))
box+ylab("Accuracy(%)")
The 'simple' way would be to draw the boxplot and the errorbars with seperate linetypes e.g. (using the palmerpenguins dataset):
library(tidyverse)
library(palmerpenguins)
penguins %>%
ggplot(aes(x = species, y = body_mass_g)) +
stat_boxplot(geom = "errorbar", width = 0.3, lty = 2) +
geom_boxplot(aes(ymin=..lower.., ymax=..upper..)) +
stat_summary(fun = mean, geom = "errorbar", aes(ymax = after_stat(y), ymin = after_stat(y), color = "mean"),
width = .75, linetype = 1, size=1) +
stat_summary(fun = median, geom = "errorbar", aes(ymax = after_stat(y), ymin = after_stat(y), color = "median"),
width = .75, linetype = 1, size=1) +
theme_minimal() +
scale_y_continuous(limits = c(0, 8000)) +
theme(legend.title=element_blank(),
legend.text = element_text(size=15, face="bold"),
legend.background = element_rect(color="black"),
legend.position =c(0.9,0.9),
legend.direction = "vertical",
axis.text.x = element_text(size=15, angle = 20, vjust=0.5),
axis.text.y = element_text(size =15),
axis.title.y = element_text(size =20),
axis.title.x=element_blank())
Created on 2022-06-01 by the reprex package (v2.0.1)
The 'more complicated' way would be to draw in the caps yourself, e.g.
library(tidyverse)
library(palmerpenguins)
penguins %>%
na.omit() %>%
group_by(species) %>%
mutate(bottom_cap = boxplot.stats(body_mass_g)$stats[1],
top_cap = boxplot.stats(body_mass_g)$stats[5]) %>%
ggplot(aes(x = species, y = body_mass_g)) +
stat_boxplot(geom = "errorbar", width = 0.3, lty = 2) +
geom_linerange(aes(xmin = as.numeric(species) - 0.2,
xmax = as.numeric(species) + 0.2,
y = top_cap)) +
geom_linerange(aes(xmin = as.numeric(species) - 0.2,
xmax = as.numeric(species) + 0.2,
y = bottom_cap)) +
geom_boxplot(aes(ymin=..lower.., ymax=..upper..)) +
stat_summary(fun = mean, geom = "errorbar", aes(ymax = after_stat(y), ymin = after_stat(y), color = "mean"),
width = .75, linetype = 1, size=1) +
stat_summary(fun = median, geom = "errorbar", aes(ymax = after_stat(y), ymin = after_stat(y), color = "median"),
width = .75, linetype = 1, size=1) +
theme_minimal() +
scale_y_continuous(limits = c(0, 8000)) +
theme(legend.title=element_blank(),
legend.text = element_text(size=15, face="bold"),
legend.background = element_rect(color="black"),
legend.position =c(0.9,0.9),
legend.direction = "vertical",
axis.text.x = element_text(size=15, angle = 20, vjust=0.5),
axis.text.y = element_text(size =15),
axis.title.y = element_text(size =20),
axis.title.x=element_blank())
Created on 2022-06-01 by the reprex package (v2.0.1)

Automatically select a more distinctive color pallet for ggplot2

I use the following code to generate a series of plots which include both geom_boxplot and geom_point. I want the points to have more distinctive colors without me defining what they are every time:
for (i in 1:length(Girder.Plot)) {
Plot.Girder <- ggplot(data = subset(Girder.Plot[[i]], Names == "Sample"),
aes(x = Type, y = Moment, fill = factor(Spacing,levels = c("9","12","15")))) +
geom_boxplot(outlier.shape = NA, position = position_dodge(width = 0.75)) +
stat_summary(fun = mean, geom="point", shape=23, size=2,
position = position_dodge(width = 0.75)) +
stat_boxplot(geom='errorbar', linetype=1, width=0.5,
position = position_dodge(width = 0.75)) +
geom_point(data = subset(Girder.Plot[[i]], Names == "No Factor"),
aes(colour = Names), position = position_dodge(width = 0.75)) +
geom_point(data = subset(Girder.Plot[[i]], Names == "Factor 1"),
aes(colour = Names), position = position_dodge(width = 0.75)) +
geom_point(data = subset(Girder.Plot[[i]], Names == "Factor 2"),
aes(colour = Names), position = position_dodge(width = 0.75)) +
geom_point(data = subset(Girder.Plot[[i]], Names == "Factor 3"),
aes(colour = Names), position = position_dodge(width = 0.75)) +
labs(x = element_blank(), y = element_blank(),
title = paste0("Moment Live Load Distribution Factors \n Along the Roadway Width for \n Ultra-Girder Section: UG-"
,str_extract(names(Girder.Plot)[i],"\\d+")),
fill = "Girder Spacing (ft):", colour = element_blank()) +
theme_classic() + ylim(0.4, 1.1) +
theme(plot.title = element_text(hjust = 0.5, margin = margin(45,0,20,0),
face = "bold", size = 18),
legend.title.align = 0.5, legend.position = "bottom",
legend.box.background = element_rect(colour = "black", size = 0.5),
legend.box.margin = margin(0,0,0,0))
print(Plot.Girder)
}
Use scale_fill_brewer/scale_color_brewer.
library(ggplot2)
ggplot(iris, aes(Species, Sepal.Length, fill = Species)) +
geom_boxplot() +
theme_minimal() +
scale_fill_brewer(palette = "Set1")
To see available palettes.
RColorBrewer::display.brewer.all()

Define and specify legend quantiles scatter plot R

I have eg data and syntax for a scatter (jitter) plot below
eg_data <- data.frame(
period = c(sample( c("1 + 2"), 1000, replace = TRUE)),
max_sales = c(sample( c(1,2,3,4,5,6,7,8,9,10), 1000, replace = TRUE, prob =
c(.20, .10, .15, .20, .15, .10, .05, .02, .02, .01))) )
jitter <- (
(ggplot(data = eg_data, aes(x=period, y=max_sales)) +
geom_jitter(stat = "identity", width = .15, color = "blue", alpha = .4)) +
scale_y_continuous(breaks= seq(0,12, by=1)) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.25)), geom = "hline", aes(yintercept = ..y..), colour = "red", size = 1) +
stat_summary(fun.y = "mean", geom = "hline", aes(yintercept = ..y..), colour = "gold", size = 1) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.50)), geom = "hline", aes(yintercept = ..y..), colour = "blue", size = 1) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.75)), geom = "hline", aes(yintercept = ..y..), colour = "black", size = 1) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.90)), geom = "hline", aes(yintercept = ..y..), colour = "green", size = 1) +
ggtitle("Max Sales x Period 1 and 2") + xlab("Period") + ylab("Sales") +
theme(plot.title = element_text(color = "black", size = 14, face = "bold", hjust = 0.5),
axis.title.x = element_text(color = "black", size = 12, face = "bold"),
axis.title.y = element_text(color = "black", size = 12, face = "bold")) +
labs(fill = "Period") )
jitter
I cannot find documentation on how to define a legend for the horiztonal quantile / mean lines I have in this graph.
How to add legend to ggplot manually? - R
I came across this SO question / answer but I wasn't able to implement it, when I include color inside the aes setting, it doesn't work.
EDIT - a member suggested I add color to the aes specification...here is the same graph with color and size included.
jitter2 <- (
(ggplot(data = eg_data, aes(x=period, y=max_sales)) +
geom_jitter(stat = "identity", width = .15, color = "blue", alpha = .4)) +
scale_y_continuous(breaks= seq(0,12, by=1)) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.25)), geom = "hline", aes(yintercept = ..y.., colour = "red"), size = 1) +
stat_summary(fun.y = "mean", geom = "hline", aes(yintercept = ..y.., colour = "gold"), size = 1) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.50)), geom = "hline", aes(yintercept = ..y.., colour = "blue"), size = 1) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.75)), geom = "hline", aes(yintercept = ..y.., colour = "black"), size = 1) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.90)), geom = "hline", aes(yintercept = ..y.., colour = "green"), size = 1) +
ggtitle("Max Sales x Period 1 and 2") + xlab("Period") + ylab("Sales") +
theme(plot.title = element_text(color = "black", size = 14, face = "bold", hjust = 0.5),
axis.title.x = element_text(color = "black", size = 12, face = "bold"),
axis.title.y = element_text(color = "black", size = 12, face = "bold")) +
labs(fill = "Period") )
jitter2
So...any help is appreciated. Thank you!
I found the answer to my own question.
jitter <- (
(ggplot(data = eg_data, aes(x=period, y=max_sales)) +
geom_jitter(stat = "identity", width = .15, color = "blue", alpha = .4)) +
scale_y_continuous(breaks= seq(0,12, by=1)) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.25)), geom = "hline", aes(yintercept = ..y.., colour = "25%"), size = 1) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.50)), geom = "hline", aes(yintercept = ..y.., colour = "50%"), size = 1) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.75)), geom = "hline", aes(yintercept = ..y.., colour = "75%"), size = 1) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.90)), geom = "hline", aes(yintercept = ..y.., colour = "90%"), size = 1) +
stat_summary(fun.y = "mean", geom = "hline", aes(yintercept = ..y.., colour = "mean"), size = 1.5) +
ggtitle("Max Sales x Period 1 and 2") + xlab("Period") + ylab("Sales") +
theme(plot.title = element_text(color = "black", size = 14, face = "bold", hjust = 0.5),
axis.title.x = element_text(color = "black", size = 12, face = "bold"),
axis.title.y = element_text(color = "black", size = 12, face = "bold")) +
scale_colour_manual(values = c("red", "blue", "gold", "green", "black"), name = "Percentiles"))
jitter
Also, quickly, the idea of "just use (something), everyone gets it" as a suggestion is not helpful, and assumes way too much about the final intended audience. First time I've ever posted a question and had that as a reply. I asked a specific question for a specific reason.

R ggplot2 stat_summary legend mean additional to groups

I've plotted the mean for the whole study population (black line) and for men and women separately.
plotYYIR1<- ggplot(data=YYIR1Long, aes(x=TimeValue, y=YYIR1Value)) +
labs(x="Week number", y="YYIR1 distance run (m)") +
theme(plot.title = element_text(hjust = 0, vjust=0))+
theme(legend.title=element_blank())+
theme(legend.key.width = unit(1, "cm"))+
stat_summary(fun.y = mean,geom = "point", size=2) +
stat_summary(fun.y = mean, geom = "line", size=0.7) +
stat_summary(fun.y = mean,geom = "point", size=2, aes(shape=Sex,colour=Sex)) +
scale_shape_manual(values = c("Male"=17, "Female"=15))+
stat_summary(fun.y = mean, geom = "line", size=0.7, aes(colour=Sex)) +
scale_colour_manual(values = c("#009CEF", "#CC0000"))+
stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width =2)+
stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width =2, aes(colour=Sex))
plotYYIR1
The legend only shows the genders, could someone help me with adding the black line and points in the legend for the whole group?
You need to add aes() to get a legend for the black line/points. If you want the legend to be for lines/shapes combined you can turn off the legend for shapes by adding guide = F to scale_shape_manual and then use override.aes in guides to specify the shapes in the legend:
ggplot(data=YYIR1Long, aes(x=TimeValue, y=YYIR1Value)) +
labs(x="Week number", y="YYIR1 distance run (m)") +
theme(plot.title = element_text(hjust = 0, vjust=0))+
theme(legend.title=element_blank())+
theme(legend.key.width = unit(1, "cm"))+
stat_summary(fun.y = mean,geom = "point", size=2, aes(colour = "mean")) +
stat_summary(fun.y = mean, geom = "line", size=0.7, aes(colour = "mean")) +
stat_summary(fun.y = mean,geom = "point", size=2, aes(shape=Sex,colour=Sex)) +
scale_shape_manual(values = c("Male"=17, "Female"=15, "mean"=16), guide = F)+
stat_summary(fun.y = mean, geom = "line", size=0.7, aes(colour=Sex)) +
scale_colour_manual(values = c("#009CEF", "#CC0000", "#000000"))+
stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width =2, aes(colour = "mean"))+
stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width =2, aes(colour=Sex)) +
guides(colour = guide_legend(override.aes = list(shape = c("Male"=17, "Female"=15, "mean"=16))))

Resources