ggplot2 legend in 2 rows - r

I was unable to find a solution for putting ggplot2 legend in 2 rows.
Example
library(ggplot2)
theme_set(theme_bw())
data("midwest", package = "ggplot2")
ggplot(midwest, aes(x=area, y=poptotal)) +
geom_point(aes(col=state, size=popdensity)) +
geom_smooth(method="loess", se=F) +
xlim(c(0, 0.1)) +
ylim(c(0, 500000)) +
labs(y="Population",
x="Area",
title="") +
theme(legend.position = "top")
In the above image, I would like to have popdensity annotation on top (first row) and state annotation in the second row.

I think you're looking for theme(legend.box = "vertical") and guide_legend(order = ...)
library(ggplot2)
data("midwest", package = "ggplot2")
ggplot(midwest, aes(x=area, y=poptotal)) +
geom_point(aes(col=state, size=popdensity)) +
geom_smooth(method="loess", se=F) +
xlim(c(0, 0.1)) +
ylim(c(0, 500000)) +
labs(y="Population",
x="Area",
title="") +
theme_bw() +
theme(legend.position = "top",
legend.box = "vertical") +
guides(size = guide_legend(order = 1),
colour = guide_legend(order = 2))

Related

use if else within ggplot chunk to change colour palette

I'd like to be able to change the colour palette in ggplot2 boxplots, according to another variable data_origin.
This makes my boxplots, complete with legend:
library(hrbrthemes)
library(ggplot2)
library(reshape2)
library(tidyverse)
data_origin <- "airborne"
mytitle <- "something more than this"
legend_title <- "some words"
melted <- reshape2::melt(iris)
bp1 <- ggplot(melted, aes(x = variable, y = value, fill = Species)) +
geom_boxplot() +
theme_ipsum() +
scale_fill_brewer(palette = "Greens") +
theme(
legend.position = "bottom",
plot.title = element_text(size = 10)) +
theme(axis.text.x = element_blank()) +
ggtitle(mytitle) +
xlab("") +
ylab("") +
facet_wrap(~variable, scale = "free")
bp1
This however drops the legend completely and ignores the if else:
bp1 <- ggplot(melted, aes(x = variable, y = value, fill = Species)) +
geom_boxplot() +
theme_ipsum() +
scale_fill_brewer(legend_title, if (data_origin == "airborne" ) {palette = "Blues"} else {palette = "Greens"}) +
theme(
legend.position = "bottom",
# legend.title = legend_title,
plot.title = element_text(size = 10)) +
theme(axis.text.x = element_blank()) +
ggtitle(mytitle) +
xlab("") +
ylab("") +
facet_wrap(~variable, scale = "free")
bp1
Besides what #stefan suggested, there are two ways in which you can do this (that I know of). The first is using ifelse() (I moved the relevant part to the end):
data_origin <- "airborne"
bp1 <- ggplot(melted, aes(x = variable, y = value, fill = Species)) +
geom_boxplot() +
theme_ipsum() +
theme(
legend.position = "bottom",
# legend.title = legend_title,
plot.title = element_text(size = 10)) +
theme(axis.text.x = element_blank()) +
ggtitle(mytitle) +
xlab("") +
ylab("") +
facet_wrap(~variable, scale = "free") +
scale_fill_brewer(legend_title, palette = ifelse(
data_origin == "airborne",
"Blues",
"Greens"
))
bp1
The other one is to build the plot up in two steps:
data_origin <- "not airborne"
bp1 <- ggplot(melted, aes(x = variable, y = value, fill = Species)) +
geom_boxplot() +
theme_ipsum() +
theme(
legend.position = "bottom",
# legend.title = legend_title,
plot.title = element_text(size = 10)) +
theme(axis.text.x = element_blank()) +
ggtitle(mytitle) +
xlab("") +
ylab("") +
facet_wrap(~variable, scale = "free")
if (data_origin == "airborne") {
bp2 <- bp1 +
scale_fill_brewer(legend_title, palette = "Blues")
} else {
bp2 <- bp1 +
scale_fill_brewer(legend_title, palette = "Greens")
}
bp2
Created on 2021-08-01 by the reprex package (v2.0.0)

How to delete legend in ggplot with reression model?

I'd like to ask how to delete legend in ggplot with regression model.
I already added theme(legend.position = "None")
but the legend cannot be deleted. Could you tell me what I was doing wrong?
Extra question!!
In my current code, how to change the symbol size and shape between N0 and N1? I want more bigger size of 'open circle', and 'closed square' shape.
Many thanks!!!
ggplot(data=x, aes(x=agw, y=pgw)) +
geom_point (data=x, aes(x=agw, y=pgw, color=Nitrogen)) +
stat_smooth(method = 'lm', se=FALSE, color="Black") +
scale_color_manual(values = c("Dark gray","Black")) +
theme(legend.position = "None") +
geom_text(x=30, y=70, label="", size=3.5, col="Black") +
geom_text(x=30, y=60, label="", size=3.5, col="Black") +
scale_x_continuous(breaks = seq(0,80,10),limits = c(0,80)) +
scale_y_continuous(breaks = seq(0,80,10), limits = c(0,80)) +
theme_bw() +
theme(panel.grid = element_blank())
This should work in lack of reproducible data. Be careful that functions like theme_bw() use to remove previous theme() settings as mentioned by #Ronald. So it is better to add in the final part of the plot. For shapes, you can enable shape in aes() like this and format with scale_shape_manual() (the numbers inside belong to the shape you want):
library(ggplot2)
#Code
ggplot(data=x, aes(x=agw, y=pgw)) +
geom_point (data=x, aes(x=agw, y=pgw, color=Nitrogen,shape=Nitrogen,size=3)) +
stat_smooth(method = 'lm', se=FALSE, color="Black") +
scale_color_manual(values = c("Dark gray","Black")) +
scale_shape_manual(values = c(1,15))+
geom_text(x=30, y=70, label="", size=3.5, col="Black") +
geom_text(x=30, y=60, label="", size=3.5, col="Black") +
scale_x_continuous(breaks = seq(0,80,10),limits = c(0,80)) +
scale_y_continuous(breaks = seq(0,80,10), limits = c(0,80)) +
theme_bw() +
theme(panel.grid = element_blank(),
legend.position = 'none')
For the legend: add the argument show.legend = F inside geom_point. For the different point size: can you give us an example of your dataset? We may need to reshape it.
ggplot(data=x, aes(x=agw, y=pgw)) +
geom_point (data=x, aes(x=agw, y=pgw, color=Nitrogen), show.legend = F) +
stat_smooth(method = 'lm', se=FALSE, color="Black") +
scale_color_manual(values = c("Dark gray","Black")) +
theme(legend.position = "None") +
geom_text(x=30, y=70, label="", size=3.5, col="Black") +
geom_text(x=30, y=60, label="", size=3.5, col="Black") +
scale_x_continuous(breaks = seq(0,80,10),limits = c(0,80)) +
scale_y_continuous(breaks = seq(0,80,10), limits = c(0,80)) +
theme_bw() +
theme(panel.grid = element_blank())

How to plot geom_line over bar chart grouped by x variable not fill variable?

I have a data frame df
Group Time_Period mean uci lci
1 A Before 4.712195 5.054539 4.369852
2 A After 5.881463 6.241784 5.521142
3 B Before 5.349754 5.872940 4.826567
4 B After 6.653595 7.246231 6.060959
I want to plot this to illustrate that there is no difference in the mean increase between groups. I tried the following :
ggplot(df, aes(x=Time_Period, y=mean, fill=Group)) +
geom_bar(stat="identity", position=position_dodge(width = 1), color="black") +
geom_point(position = position_dodge(width = 1))+
geom_line(aes(group=Group, color=Group), color=c("cyan4","firebrick","cyan4","firebrick"), size =1, position = position_dodge(width = 1)) +
geom_errorbar(aes(ymin=lci, ymax=uci), position=position_dodge(width = 1)) +
theme_bw() +
scale_y_continuous(limits=c(-0.2,8), breaks= seq(0,300,1), minor_breaks=seq(0,300,0.5)) +
theme(panel.grid.minor = element_line(colour="lightgrey", size=0.5)) +
theme(panel.grid.major = element_line(colour="grey", size=0.5)) +
labs(y="Sales", x="Time Period", fill="Category") +
theme(axis.text.x = element_text(face="bold", size=12)) +
theme(axis.text.y = element_text(face="bold", size=12)) +
theme(axis.title.x = element_text(face="bold", size=16)) +
theme(axis.title.y = element_text(face="bold", size=16)) +
theme(legend.text= element_text(face="bold", size=12)) +
theme(legend.title= element_text(face="bold", size=16))
which plots:
However my manager is concerned it is difficult to differentiate the two lines due to the overlap, so he told me to rearrange the columns so that x is Group and fill is Time Period.
I tried the following:
ggplot(df, aes(x=Group, y=mean, fill=Time_Period)) +
geom_bar(stat="identity", position=position_dodge(width = 1), color="black") +
geom_point(position = position_dodge(width = 1))+
geom_line(aes(group=Group), color="black", size =1, position = position_dodge(width = 1)) +
geom_errorbar(aes(ymin=lci, ymax=uci), position=position_dodge(width = 1)) +
theme_bw() +
scale_y_continuous(limits=c(-0.2,8), breaks= seq(0,300,1), minor_breaks=seq(0,300,0.5)) +
theme(panel.grid.minor = element_line(colour="lightgrey", size=0.5)) +
theme(panel.grid.major = element_line(colour="grey", size=0.5)) +
labs(y="Sales", x="Group", fill="Time Period") +
theme(axis.text.x = element_text(face="bold", size=12)) +
theme(axis.text.y = element_text(face="bold", size=12)) +
theme(axis.title.x = element_text(face="bold", size=16)) +
theme(axis.title.y = element_text(face="bold", size=16)) +
theme(legend.text= element_text(face="bold", size=12)) +
theme(legend.title= element_text(face="bold", size=16))
But I can't work out how to get the lines to plot correctly between the two bars instead of just vertically in the centre, even if I adjust the "width" argument for position_dodge:
Please could anyone advise me on how to fix the plot?
You're looking for position_dodge2(). There's a little about it on the ggplot2 dodge reference, and a little more in the actual code on Github. The relevant section below, with some emphasis added:
Dodging preserves the vertical position of an geom while adjusting the
horizontal position. position_dodge2 is a special case of position_dodge
for arranging box plots, which can have variable widths. position_dodge2
also works with bars and rectangles. But unlike position_dodge,
position_dodge2 works without a grouping variable in a layer.
So here's the code, with some of the theming removed.
library(tidyverse)
txt = "
Group Time_Period mean uci lci
1 A Before 4.712195 5.054539 4.369852
2 A After 5.881463 6.241784 5.521142
3 B Before 5.349754 5.872940 4.826567
4 B After 6.653595 7.246231 6.060959"
df <- read.table(text = txt, header = TRUE) %>%
mutate(Group = fct_relevel(Group, "A", "B")) %>%
mutate(Time_Period = fct_relevel(Time_Period, "Before", "After"))
ggplot(df, aes(x=Group, y=mean, fill=Time_Period)) +
geom_bar(stat="identity", position=position_dodge(width = 1), color="black") +
geom_point(position = position_dodge(width = 1))+
geom_line(aes(group=Group), color="black", size =1,
position = position_dodge2(width = 1)) +
geom_errorbar(aes(ymin=lci, ymax=uci), position=position_dodge(width = 1)) +
theme_bw() +
scale_y_continuous(limits=c(-0.2,8), breaks= seq(0,300,1), minor_breaks=seq(0,300,0.5)) +
labs(y="Sales", x="Group", fill="Time Period")
Created on 2019-11-21 by the reprex package (v0.3.0)

How to add label to geom_segment at the start of the segment?

I'm sure this is simple but I can't figure it out.
I have the following chart:
library(data.table)
library(magrittr)
library(ggplot2)
cambodia <-
data.table(Period = c("Funan", "Chenla/Zhenla","Khmer Empire","Dark Ages of Cambodia"),
StartDate = c(-500,550,802,1431),
EndDate = c(550,802,1431,1863),
Color = c("lightblue","lightgreen","lightyellow","pink")) %>%
extract(order(-StartDate)) %>%
extract(, Period := factor(Period,levels = Period))
ggplot() +
geom_segment(data=cambodia, aes(x=StartDate, xend=EndDate, y=Period, yend=Period, color=Color),
linetype=1, size=2) +
scale_colour_brewer(palette = "Pastel1")+
xlab("Date")+
ylab("Ruler")+
theme_bw() +
theme(panel.grid.minor = element_blank(), panel.grid.major = element_blank()) +
theme(aspect.ratio = .2) +
theme(legend.position="none")
But I would like the labels to be off the axis and on the page. Either to the left or on top of the middle of the line. E.g.
Most of the examples of geom_text give me gobbledeegook. I can't seem to apply them to the factor data I have here. Do you know how to do this?
Thank you
Having the labels on the end of the segments might distort the visual mapping of segment length and location to year-range. You could put the labels in the middle of the segments instead.
library(data.table)
library(magrittr)
library(ggplot2)
library(stringr)
cambodia <-
data.table(Period = c("Funan", "Chenla/Zhenla","Khmer Empire","Dark Ages of Cambodia"),
StartDate = c(-500,550,802,1431),
EndDate = c(550,802,1431,1863),
Color = c("lightblue","lightgreen","lightyellow","pink")) %>%
extract(order(-StartDate)) %>%
extract(, Period := factor(Period,levels = Period))
ggplot(cambodia, aes(x=StartDate, xend=EndDate, y=Period, colour=Period)) +
geom_segment(aes(xend=EndDate, yend=Period), linetype=1, size=2) +
geom_label(aes(label=str_wrap(Period,12), x=(StartDate + EndDate)/2), size=3) +
scale_colour_brewer(palette = "Set1") +
xlab("Date")+ ylab("Ruler")+
theme_bw() +
theme(panel.grid.minor = element_blank(), panel.grid.major = element_blank(),
aspect.ratio = .2,
legend.position="none",
axis.text.y=element_blank(),
axis.ticks.y=element_blank())
Or what about going minimal:
ggplot(cambodia, aes(x=StartDate, y=1)) +
geom_rect(aes(xmin=StartDate, xmax=EndDate, ymin=0.97, ymax=1.03, fill=Period),
show.legend=FALSE, colour="white", size=0.5) +
geom_label(aes(label=str_wrap(Period,12), x=(StartDate + EndDate)/2), size=3.5) +
geom_text(aes(label=StartDate, y=0.96), size=3.5) +
geom_text(aes(label=ifelse(EndDate==max(EndDate), EndDate,""), x=EndDate, y=0.96), size=3.5) +
scale_colour_brewer(palette = "Set1") +
scale_y_continuous(limits=c(0.95,1.05)) +
theme_void()
ggplot() +
geom_segment(data=cambodia, aes(x=StartDate, xend=EndDate, y=Period, yend=Period, color=Color),
linetype=1, size=2) +
geom_label(data=cambodia, aes(x=StartDate, y=Period, label = Period),
nudge_x = c(-300, -200, -200, -100)) +
scale_colour_brewer(palette = "Pastel1")+
xlab("Date")+
ylab("")+
theme_bw() +
theme(legend.position="none") +
theme(aspect.ratio = .2) +
theme(panel.grid.minor = element_blank(), panel.grid.major = element_blank(),
axis.line.y = element_blank(), axis.text.y = element_blank(),
axis.ticks.y = element_blank())
You need to use element_blank() to remove the y axis elements and then use nudge_x argument in geom_label to offset the labels appropriately.

How do I customize the margin and label settings with plot_grid?

I would like to have the title not be chopped off and have the axis labels removed from this chart that I generated with plot_grid from cowplot.
Here is my code
data(mtcars)
library(ggplot2)
library(cowplot)
mpg = ggplot() +
geom_boxplot(aes(y = mpg,x = as.factor(cyl)),data=mtcars) +
coord_flip()
am=ggplot() +
geom_boxplot(aes(y = mpg,x = as.factor(am)),data=mtcars) +
coord_flip()
vs=ggplot() +
geom_boxplot(aes(y = mpg,x = as.factor(vs)),data=mtcars) +
coord_flip()
gear = ggplot() +
geom_boxplot(aes(y = mpg,x = as.factor(gear)),data=mtcars) +
coord_flip()
p=plot_grid(mpg,am,vs,gear, labels = "Variables effecting Mileage", label_size = 14, hjust = -0.5,
+ vjust = 0.5)+theme_grey()
p
Also, if it would be simpler to create this without cowplot, what do you suggest?
Here is a cowplot only answer. It might be more what you want.
library(ggplot2)
library(cowplot)
library(gtable)
data(mtcars)
mpg = ggplot() +
geom_boxplot(aes(y = mpg,x = as.factor(cyl)),data=mtcars) +
coord_flip() + labs(x="",y="")
am=ggplot() +
geom_boxplot(aes(y = mpg,x = as.factor(am)),data=mtcars) +
coord_flip()+ labs(x="",y="")
vs=ggplot() +
geom_boxplot(aes(y = mpg,x = as.factor(vs)),data=mtcars) +
coord_flip()+ labs(x="",y="")
gear = ggplot() +
geom_boxplot(aes(y = mpg,x = as.factor(gear)),data=mtcars) +
coord_flip()+ labs(x="",y="")
p=plot_grid(mpg,am,vs,gear) +
theme_grey() +
# Use annotation text as it is centered at the x,y point
annotate("text",x=0.5,y=1.04,size=7,label="Variables affecting Mileage") +
# Add some space around the edges
theme(plot.margin = unit(c(1,0.5,0.5,0.5), "cm"))
# Suppress tick marks
p=p+scale_y_continuous(breaks=NULL)+scale_x_continuous(breaks=NULL)
# Have to turn off clipping
gt <- ggplot_gtable(ggplot_build(p))
gt$layout$clip[gt$layout$name == "panel"] <- "off"
# need to draw it with the new clip settings
grid.draw(gt)
Yields:
would this workout for you?
library(ggplot2)
library(gridExtra)
a <- ggplot() + geom_boxplot(aes(y = mpg,x = as.factor(cyl)),data=mtcars) + coord_flip() + theme_grey() + theme(axis.title.x = element_blank())
b <- ggplot() + geom_boxplot(aes(y = mpg,x = as.factor(am)),data=mtcars) + coord_flip() + theme_grey() + theme(axis.title.x = element_blank())
c <- ggplot() + geom_boxplot(aes(y = mpg,x = as.factor(vs)),data=mtcars) +
coord_flip() + theme_grey() + theme(axis.title.x = element_blank())
d <- ggplot() + geom_boxplot(aes(y = mpg,x = as.factor(gear)),data=mtcars) +
coord_flip() + theme_grey() + theme(axis.title.x = element_blank())
grid.arrange(a, b, c, d, ncol=2, top = "Variables effecting Mileage")
Thanks Mike. This does the job.
data(mtcars)
library(ggplot2)
library(cowplot)
library(gtable)
mpg = ggplot() +
geom_boxplot(aes(y = mpg,x = as.factor(cyl)),data=mtcars) +
coord_flip() + labs(x="",y="")
am=ggplot() +
geom_boxplot(aes(y = mpg,x = as.factor(am)),data=mtcars) +
coord_flip()+ labs(x="",y="")
vs=ggplot() +
geom_boxplot(aes(y = mpg,x = as.factor(vs)),data=mtcars) +
coord_flip()+ labs(x="",y="")
gear = ggplot() +
geom_boxplot(aes(y = mpg,x = as.factor(gear)),data=mtcars) +
coord_flip()+ labs(x="",y="")
p=plot_grid(mpg,am,vs,gear,
labels = "Variables effecting Mileage",
label_size = 14, hjust = -1.3[![enter image description here][1]][1], vjust = -0.1)+
theme_grey() +
# Add some space around the edges
theme(plot.margin = unit(c(1,0.5,0.5,0.5), "cm"))
# Suppress tick marks
p=p+scale_y_continuous(breaks=NULL)+scale_x_continuous(breaks=NULL)
# Have to turn off clipping
gt <- ggplot_gtable(ggplot_build(p))
gt$layout$clip[gt$layout$name == "panel"] <- "off"
# need to draw it with the new clip settings
grid.draw(gt)
Here is my plot

Resources