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)
Related
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
I searched about my problem but apparently, there is no topic about this. Any help will be appreciated.
Suppose I have a plot generated by:
library(tidyverse)
df<- data.frame(PCP = c("BOB","FRED","ED","Other"),
closed = c(42,64,45,1812),
total= c(53,81,58,3188),
percentage = c(.7924,.7901,.7758,.5683),
row= c(1, 2, 3,4),
color =c("0099FF","#CCCCCC","#CCCCCC","#660033"),
color_fill = c("99","0","0","98"
))
col <- c(
"99" = "#0099FF",
"98" = "#660033",
"0" = "#CCCCCC"
)
df %>%
arrange(desc(percentage)) %>%
mutate(PCP = PCP,
closed = closed,
total = total,
percentage = percentage,
row = row,
color = color,
color_fill = color_fill) -> df1
ggplot(df1,aes(x=PCP, y = percentage,fill=color_fill, color = color)) +
geom_col() +
coord_flip() +
labs(x ="PCP Name",
y = "Percentage of Gap Closures",
title = "TOP 10 PCPs")+
scale_fill_manual(values = col)+
scale_color_manual(values = col) +
theme(legend.position = "none",
panel.grid = element_blank(),
panel.background = element_blank(),
text = element_text(size=15),
plot.caption = element_text(hjust = 0, face= "italic"),
axis.text.y = element_text(colour = col ))
How can I put 0.6 on the x-axis and FRED on the y-axis inside a colored box?
have a look here by using two annotate (note coord_filp() is updated)
ggplot(df1,aes(x=PCP, y = percentage,fill=color_fill, color = color)) +
geom_col() +
#coord_flip() +
labs(x ="PCP Name",
y = "Percentage of Gap Closures",
title = "TOP 10 PCPs")+
scale_fill_manual(values = col)+
scale_color_manual(values = col) +
theme(legend.position = "none",
panel.grid = element_blank(),
panel.background = element_blank(),
text = element_text(size=15),
plot.caption = element_text(hjust = 0, face= "italic"),
axis.text.y = element_text(colour = col )) +
annotate("rect", xmin = 2.8, xmax = 3.2, ymin = -0.091, ymax = -0.045, alpha=0.5, fill="red") +
annotate("rect", xmin = 0.2, xmax = 0.35, ymin = 0.58, ymax = 0.62, alpha=0.5, fill="red") +
coord_flip(xlim = c(1,4), ylim = c(0, 0.8), clip = "off")
If you insist on a "box", then perhaps use segment instead of rect for the annotate.
I am having some troubles with the following piece of code:
ggplot(data = sb11.20194, aes(y = PROMLECTURACRITICA, x = año)) +
geom_boxplot(fill = "#3AAA35", color = "#3AAA35",outlier.color = "#95C11F",
outlier.size = 5) +
ylab("Puntajes promedio de Lectura Crítica") +
stat_boxplot(geom = "errorbar", colour = "#006633",
width = 0.6) +
stat_summary(geom = "crossbar", width=1.5, fatten=0,
color="white",
fun.data = function(x){ return(c(y=median(x),
ymin=median(x),
ymax=median(x))) }) +
theme(
panel.background = element_rect(fill = "white", colour = rgb(198,
198,
198,
maxColorValue = 255),
size = 1, linetype = "solid"),
panel.grid.minor = element_line(size = 0.1, linetype = 'dashed',
colour = rgb(198,198,198,
maxColorValue = 255)),
axis.ticks.x = element_blank(),
axis.text.x = element_blank(),
axis.title.x = element_blank(),
axis.text.y = element_text(family = "Montserrat"),
axis.title.y = element_text(family = "Montserrat")
) + geom_text(data = num, aes(label = num, y = num),
color = "#575756", hjust = -8,
family = "Montserrat")
which gives the following plot:
I would like to align the labels. Does anyone know how I might do this?
You didn't provide a sample data set, so I made some on my own. You can use two arguments in geom_text: nudge_x and hjust. You can use nudge_x in the way you're currently using hjust in your code. Then, we can use hjust to align the labels.
library(tidyverse)
set.seed(123)
# generate sample data and calculate quantiles
dat <- tibble(x = rnorm(1000))
dat_summary <- tibble(quants = quantile(dat$x))
ggplot(dat, aes(x = 1, y = x))+
geom_boxplot() +
geom_text(data = dat_summary, aes(x = 1.5, y = quants,
label = round(quants, 2)),
hjust = 'outward', nudge_x = 0.1)
I want to add a text to my forest plot in R that has strip in it but the text is repeated on every strip . how can I add only the text to one strip or just on the plot? My code is as below:
My data is like:
Group Mean LowerLimit UpperLimit
M 1.172827 1.083498 1.268857
H 5.142589 4.333141 6.148088
h<-"XXXX"
p = ggplot(data=df4,
aes(x = Group,y = Mean, ymin = LowerLimit, ymax = UpperLimit),
+
ggtitle(PlotTitle)+
geom_point(aes(fill=Group, color=Group), shape=22, size=3)+
geom_pointrange(aes(col=Group), fatten = 3)+
geom_hline(aes(),yintercept =1, linetype="longdash")+
geom_text(aes(-1.5, 0.8, vjust =-0.5, hjust=-0.8, size=10),label=h,
check_overlap = T)+
geom_errorbar(aes(ymin=LowerLimit,
ymax=UpperLimit,col=Group),width=0.4,cex=1)+
facet_wrap(~Group,strip.position="left",nrow=2, scales= "free_y") +
theme(plot.title=element_text(aes(5, 5), hjust=0.5, size=14,face="bold"),
legend.position='none',
strip.text.y = element_text(size=10, hjust=0.5,vjust =1,lineheight=0.1, angle=270,face="bold"),
panel.background = element_blank(),
strip.background = element_rect(fill="green"),
plot.margin = margin(3.5,0.1,3.5, 0.5, "cm"))+
coord_flip()
p
In your parameters for geom_text try changing label=h to label = ''
library(ggplot2)
df4 <- data.frame(Group = c("M", "H"),
Mean = c(1.172827, 5.142589),
LowerLimit = c(1.083498, 4.333141),
UpperLimit = c(1.268857, 6.148088))
PlotTitle = "Insert plot title here"
p = ggplot(data=df4,
aes(x = Group,y = Mean, ymin = LowerLimit, ymax = UpperLimit)) +
ggtitle(PlotTitle) +
geom_point(aes(fill=Group, color=Group), shape=22, size=3) +
geom_pointrange(aes(col=Group), fatten = 3) +
geom_hline(aes(),yintercept =1, linetype="longdash") +
geom_text(aes(-1.5, 0.8, vjust =-0.5, hjust=-0.8, size=10),label='',
check_overlap = T) +
geom_errorbar(aes(ymin=LowerLimit,
ymax=UpperLimit,col=Group),width=0.4,cex=1) +
facet_wrap(~Group,strip.position="left",nrow=2, scales= "free_y") +
theme(plot.title=element_text(aes(5, 5), hjust=0.5, size=14,face="bold"),
legend.position='none',
strip.text.y = element_text(size=10, hjust=0.5,vjust =1,lineheight=0.1, angle=270,face="bold"),
panel.background = element_blank(),
strip.background = element_rect(fill="green"),
plot.margin = margin(3.5,0.1,3.5, 0.5, "cm")) +
coord_flip()
p
which yields this image:
I have the following code which produces the plot below:
ggplot(data = factor_output, aes(y=F.Difference, x=reorder(Action.Title, F.Difference), fill=Efficacy.Median)) +
coord_flip() +
scale_fill_gradient(low = "red", high = "green") +
geom_bar(stat = "identity") +
geom_point(aes(y=Factor1, colour="DarkRed"), size=3) +
geom_point(aes(y=Factor2, colour="DarkBlue"), size=3) +
scale_color_manual(labels = c("Factor 1", "Factor 2"), values = c("DarkRed", "DarkBlue")) +
labs(colour="Coefficient Value") +
annotate("rect", ymin = c(0.3, -0.3, -0.3), ymax = c(Inf, -Inf, 0.3),
xmin = -Inf, xmax = Inf,
alpha = 0.1, fill = c("green", "green", "orange")) +
theme(axis.title.x = element_text(colour = "DarkGreen", size = 18),
axis.title.y = element_text(colour = "DarkGreen", size = 18),
axis.text.x = element_text(size = 15),
legend.title= element_text(size = 15),
legend.text= element_text(size = 12),
legend.position = c(0, 1),
legend.justification = c(0, 1),
legend.background = element_rect(fill="transparent"),
plot.title = element_text(colour = "DarkRed", size = 22, hjust = 0.5))
I know from other threads on stackoverflow that I need to start with empty ggplot() and then add annotate. I tried to do that and state the data and aes for each plot, but I got a lot of error messages.
Could you please let me know how I can amend the code above to send the three background areas to the back.
I used to get an error message, which reads: 'Error: Discrete value supplied to continuous scale.', when I tried to put annotate before the other plots. However, I managed to solve the problem by using scale_x_discrete() to specify to ggplot2 that I am using a discrete scale for the x axis.
ggplot(data = factor_output) +
scale_x_discrete() +
coord_flip() +
annotate(geom = "rect", ymin = c(0.3, -0.3, -0.3), ymax = c(Inf, -Inf, 0.3),
xmin = -Inf, xmax = Inf,
alpha = 0.4, fill = c("LightGreen", "LightGreen", "Orange")) +
scale_fill_gradient(low = "red", high = "green") +
scale_y_continuous(breaks=c(-0.75, -0.5, -0.3, 0, 0.3, 0.5, 0.75)) +
geom_bar(aes(y = F.Difference, x = reorder(Action.Title, F.Difference), fill=Efficacy.Median), stat = "identity") +
geom_point(aes(x = reorder(Action.Title, F.Difference), y = Factor1, colour="DarkRed"), size = 3) +
geom_point(aes(x = reorder(Action.Title, F.Difference), y = Factor2, colour="DarkBlue"), size = 3) +
scale_color_manual(labels = c("Factor 1", "Factor 2"), values = c("DarkRed", "DarkBlue")) +
labs(colour="Coefficient Value") +
ggtitle("XXX") +
xlab("XXX") +
ylab("XXX") +
theme_bw() +
theme(axis.title.x = element_text(colour = "DarkGreen", size = 18),
axis.title.y = element_text(colour = "DarkGreen", size = 18),
axis.text.x = element_text(size = 15),
legend.title = element_text(size = 15),
legend.text = element_text(size = 12),
legend.position = c(0, 1),
legend.justification = c(0, 1),
legend.background = element_rect(fill = "transparent"),
plot.title = element_text(colour = "DarkRed", size = 22, hjust = 0.5))
Ggplot adds layers on top of each other, so if I understand your question correctly, you can solve this by putting +annotate() before +geom_point() and +geom_bar(), like this:
ggplot(data = factor_output, aes(y=F.Difference, x=reorder(Action.Title, F.Difference), fill=Efficacy.Median)) +
coord_flip() +
scale_fill_gradient(low = "red", high = "green") +
annotate("rect", ymin = c(0.3, -0.3, -0.3), ymax = c(Inf, -Inf, 0.3),
xmin = -Inf, xmax = Inf,
alpha = 0.1, fill = c("green", "green", "orange")) +
geom_bar(stat = "identity") +
geom_point(aes(y=Factor1, colour="DarkRed"), size=3) +
geom_point(aes(y=Factor2, colour="DarkBlue"), size=3) +
scale_color_manual(labels = c("Factor 1", "Factor 2"), values = c("DarkRed", "DarkBlue")) +
labs(colour="Coefficient Value") +
theme(axis.title.x = element_text(colour = "DarkGreen", size = 18),
axis.title.y = element_text(colour = "DarkGreen", size = 18),
axis.text.x = element_text(size = 15),
legend.title= element_text(size = 15),
legend.text= element_text(size = 12),
legend.position = c(0, 1),
legend.justification = c(0, 1),
legend.background = element_rect(fill="transparent"),
plot.title = element_text(colour = "DarkRed", size = 22, hjust = 0.5))