ggarrange - change background of shared legend and remove borders between plots - r

I would like the background to all be navy blue without the white borders around the plots and the legend background to also be navy blue (no large white box). I have seen some solutions here but they seem needlessly complicated and would involve a lot of rewriting of the script or even switching packages. Surely there is an easier way? Thanks in advance.
Example script:
df <- data.frame(supp=rep(c("VC", "OJ"), each=3),
dose=rep(c("D0.5", "D1", "D2"),2),
len=c(6.8, 15, 33, 4.2, 10, 29.5))
cols <- c("VC" = "#0FC9F7", "OJ" = "#1010EB")
p1 <- ggplot(df, aes(x=dose, y=len, color=factor(supp))) +
geom_line(aes(group = supp)) +
theme(
plot.title = element_text(hjust = 0.5),
panel.background = element_rect(fill = "#140F4B",
colour = "#140F4B",
size = 0.5, linetype = "solid"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.background = element_rect(fill = "#140F4B"),
text = element_text(colour = "white"),
axis.line = element_line(colour = "white"),
axis.text = element_text(colour = "white")
) +
scale_color_manual(values = cols,
name = "supp",
breaks=c("VC", "OJ"),
labels=c("VC", "OJ"))
p2 <- ggplot(df, aes(x=dose, y=len, color=factor(supp))) +
geom_line(aes(group = supp)) +
theme(
plot.title = element_text(hjust = 0.5),
panel.background = element_rect(fill = "#140F4B",
colour = "#140F4B",
size = 0.5, linetype = "solid"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.background = element_rect(fill = "#140F4B"),
text = element_text(colour = "white"),
axis.line = element_line(colour = "white"),
axis.text = element_text(colour = "white")
) +
scale_color_manual(values = cols,
name = "supp",
breaks=c("VC", "OJ"),
labels=c("VC", "OJ"))
plots <- list(p1, p2)
combined_plots <- ggpubr::ggarrange(plotlist = plots,
ncol = 2, nrow = 1, align = "hv", common.legend = TRUE, legend = "bottom")
#Annotate plot
combined_plots <- annotate_figure(
combined_plots,
top = text_grob(paste0("Example"), size = 18, color = "white")
) + bgcolor("#140F4B")+ border("#140F4B")

It looks like the problem is that there is a missing legend.background in your theme which might explain why there is a white background around your legend. I managed to solve the problem using patchwork! Hopefully, this helps.
library(ggplot2)
library(patchwork)
df <- data.frame(supp=rep(c("VC", "OJ"), each=3),
dose=rep(c("D0.5", "D1", "D2"),2),
len=c(6.8, 15, 33, 4.2, 10, 29.5))
cols <- c("VC" = "#0FC9F7", "OJ" = "#1010EB")
p1 <- ggplot(df, aes(x=dose, y=len, color=factor(supp))) +
geom_line(aes(group = supp)) +
theme(
plot.title = element_text(hjust = 0.5),
panel.background = element_rect(fill = "#140F4B",
colour = "#140F4B",
size = 0.5, linetype = "solid"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.background = element_rect(fill = "#140F4B"),
text = element_text(colour = "white"),
axis.line = element_line(colour = "white"),
axis.text = element_text(colour = "white")
) +
scale_color_manual(values = cols,
name = "supp",
breaks=c("VC", "OJ"),
labels=c("VC", "OJ"))
p2 <- ggplot(df, aes(x=dose, y=len, color=factor(supp))) +
geom_line(aes(group = supp)) +
theme(
plot.title = element_text(hjust = 0.5),
panel.background = element_rect(fill = "#140F4B",
colour = "#140F4B",
size = 0.5, linetype = "solid"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.background = element_rect(fill = "#140F4B"),
text = element_text(colour = "white"),
axis.line = element_line(colour = "white"),
axis.text = element_text(colour = "white")
) +
scale_color_manual(values = cols,
name = "supp",
breaks=c("VC", "OJ"),
labels=c("VC", "OJ"))
combined <- p1 + p2 + plot_annotation(title = "Title Here") +
plot_layout(guides = "collect") &
theme(plot.title = element_text(colour = "white", size = 18), legend.position = "bottom",
legend.background = element_rect(fill = "#140F4B", colour = "#140F4B" ),
legend.key = element_rect(fill = "#140F4B", colour = "#140F4B" ),
plot.background = element_rect(fill = "#140F4B", colour = "#140F4B"))
combined

Related

Make the point in the middle of each grid in ggplot2?

Here is the data.
df <- data.frame(x = LETTERS[1:5], y = LETTERS[6:10], z = rnorm(5))
Below are the codes to generate a point plot.
ggplot(df, aes(x, y, fill = z, size = z)) +
geom_point(shape = 21, stroke = 1, colour = 'black')
I'm wondering how to make each point into the middle of each grid. Something like this.
I found it worked by adding a geom_tile function.
ggplot(df, aes(x, y)) +
geom_tile(fill = NA, colour = 'black') +
geom_point(aes(fill = z, size = z), shape = 21, stroke = 1, colour = 'black') +
theme(panel.background = element_blank(),
plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line.x = element_blank(),
axis.line.y = element_blank(),
axis.ticks.x = element_blank(),
axis.ticks.y = element_blank(),
axis.title = element_text(size = 12),
axis.text.y = element_text(size = 10, colour = 'black'),
axis.text.x = element_text(size = 10, colour = 'black'),
strip.background = element_blank(),
strip.text = element_blank())

Unable to change color of line and scatter plot R ggplot

I am relatively new to ggplot plot so I think some of the intricacies are lost on me. I have plotted multiple months of data where the data is binned by the hour. Each line is meant to be colored by month where the x-axis is the hour of the day. I am having trouble changing the color of the lines and moved things around in ggplot to try to get it to work but the color of all lines remain black"
Here is an example of some of the data I am plotting: Example data
Here is my code:
p <- ggplot(mtozoneavgk_month, aes(hour, Avgk, group = factor(Date) )) +
geom_point(size = 4) +
geom_line(size = 1)+
scale_color_manual(values = c("#DC143C", "#B22222", "#000080", "#00008B",
"#0000CD", "#0000FF", "#66B2FF", "#FF6347", "#FF0000", "#B22222"),
name = "Month", labels = c("Sept-2019", "Oct-2019", "Nov-2019", "Dec-2019",
"Jan-2020", "Feb-2020", "Mar-2020", "April-2020", "May-2020", "Jun-2020"),
expand = c(0, 0))+
ylab("rate constant (k)")+
scale_y_continuous(label=scientific_10)+
#scale_y_continuous(labels = fancy_scientific)+
theme(axis.ticks.length = unit(0.2, "cm"),
axis.ticks = element_line(size = 2),
axis.text=element_text(size=12, face = 'bold'),
axis.title=element_text(size=14,face="bold"),
axis.text.x = element_text(color = "black", face = "bold", size = 14),
axis.text.y = element_text(color = "black", size = 14),
legend.title=element_text(size=14, face = "bold"),
legend.text = element_text(size = 14, face = "bold"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
panel.border = element_rect(colour = "black", fill=NA, size=3))
p + scale_x_continuous(breaks = c(0,2,4,6,8,10,12,14,16,18,20,22,24),
label = c(0,2,4,6,8,10,12,14,16,18,20,22,24),
expand = c(0, 0))
Any help would be appreciated!
If we simplify your code as follows (letting ggplot2 to take care of colors and labels):
p <- ggplot(mtozoneavgk_month, aes(hour, Avgk, group = factor(Date), color = factor(Date))) +
geom_point(size = 4) +
geom_line(size = 1)+
ylab("rate constant (k)")+
theme(axis.ticks.length = unit(0.2, "cm"),
axis.ticks = element_line(size = 2),
axis.text=element_text(size=12, face = 'bold'),
axis.title=element_text(size=14,face="bold"),
axis.text.x = element_text(color = "black", face = "bold", size = 14),
axis.text.y = element_text(color = "black", size = 14),
legend.title=element_text(size=14, face = "bold"),
legend.text = element_text(size = 14, face = "bold"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
panel.border = element_rect(colour = "black", fill=NA, size=3))
We obtain the following plot:
If you also want to control colors I would suggest to use named vectors:
pcolor <- c("#DC143C", "#B22222", "#000080", "#00008B",
"#0000CD", "#0000FF", "#66B2FF", "#FF6347", "#FF0000")
names(pcolor) <- unique(mtozoneavgk_month$Date)
plabel <- c("Sept-2019", "Oct-2019", "Nov-2019",
"Jan-2020", "Feb-2020", "Mar-2020", "April-2020", "May-2020", "Jun-2020")
names(plabel) <- unique(mtozoneavgk_month$Date)
p <- ggplot(mtozoneavgk_month, aes(hour, Avgk, group = factor(Date), color = factor(Date))) +
geom_point(size = 4) +
geom_line(size = 1)+
scale_color_manual(values = pcolor,
name = "Month",
labels = plabel,
expand = c(0, 0))+
ylab("rate constant (k)")+
theme(axis.ticks.length = unit(0.2, "cm"),
axis.ticks = element_line(size = 2),
axis.text=element_text(size=12, face = 'bold'),
axis.title=element_text(size=14,face="bold"),
axis.text.x = element_text(color = "black", face = "bold", size = 14),
axis.text.y = element_text(color = "black", size = 14),
legend.title=element_text(size=14, face = "bold"),
legend.text = element_text(size = 14, face = "bold"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
panel.border = element_rect(colour = "black", fill=NA, size=3))
Which results in:
Remark: I do not know if it was because of the sample you gave us but December 2019 is missing so tweaked a little bit your code. Be aware of it when you make your own

How to apply legend.key backround transparent in ggplot?

I use mpg data in ggplot2 package to test plot parameters. I found that the lengend.key backgound (upper right, label vehicle model) colour could be changed any other colours but transparent or white. How to change the lengend key backgound default grey to transparent?
library(ggplot2)
p <- ggplot(data=mpg,mapping=aes(x=cty,y=hwy,colour=class))+
geom_point(aes(size=displ),alpha=0.5)+
stat_smooth(method='loess')+
scale_size_continuous(range=c(4,10))+
facet_wrap(~year,ncol=2)+
labs(x='distance per gallon in city',y='distance per gallon in highway',
title='fuel consumption and model',size='displacement',colour='vehicle model')
p+
theme(
plot.background=element_rect(fill='transparent', colour='transparent'),
panel.background=element_rect(fill='transparent', colour='gray'),
panel.border=element_rect(fill='transparent', colour='black'),
axis.text=element_text(color='black'),
panel.grid.major=element_line(colour='grey',linetype='dashed'),
strip.background=element_rect(fill='transparent', colour='transparent'),
panel.spacing.x=unit(4,'mm'),
legend.box.background=element_rect(fill='blue', colour='transparent'),
legend.key=element_rect(fill='transparent', colour='transparent')
)
You need to either add se = FALSE or remove stat_smooth(method = "loess") to remove the grey filled background in vehicle model legend keys.
library(ggplot2)
p <- ggplot(data = mpg, mapping = aes(x = cty, y = hwy, colour = class)) +
geom_point(aes(size = displ), alpha = 0.5) +
stat_smooth(method = "loess", se = FALSE) + # Add se = FALSE
scale_size_continuous(range = c(4, 10)) +
facet_wrap(~year, ncol = 2) +
labs(
x = "distance per gallon in city", y = "distance per gallon in highway",
title = "fuel consumption and model", size = "displacement", colour = "vehicle model"
)
p +
theme(
plot.background = element_blank(),
panel.background = element_rect(fill = "transparent", colour = "gray"),
panel.border = element_rect(fill = "transparent", colour = "black"),
axis.text = element_text(color = "black"),
panel.grid.major = element_line(colour = "grey", linetype = "dashed"),
strip.background = element_blank(),
panel.spacing.x = unit(4, "mm"),
legend.box.background = element_rect(fill = "blue", colour = "transparent"),
legend.key = element_rect(fill = "transparent", colour = "transparent")
)
p +
theme(
plot.background = element_rect(fill = "transparent", colour = "transparent"),
panel.background = element_rect(fill = "transparent", colour = "gray"),
panel.border = element_rect(fill = "transparent", colour = "black"),
axis.text = element_text(color = "black"),
panel.grid.major = element_line(colour = "grey", linetype = "dashed"),
strip.background = element_rect(fill = "transparent", colour = "transparent"),
panel.spacing.x = unit(4, "mm"),
legend.background = element_blank(),
legend.box.background = element_blank(),
legend.key = element_blank()
)
Created on 2018-11-03 by the reprex package (v0.2.1.9000)

Increase the radius of the Circle in ggplot2

I've had trouble increasing the radius of the circle on a map. I've tried the scale_size_continuous with maximum size up to 1000, but the circle size remained the same after all.
My code is in the following:
states <- map_data("state")
gg1 <- ggplot(data=states) +
geom_polygon(data=states, aes(x=long,y=lat,group=group),
fill = "orange", color = "white") +
coord_fixed(ratio = 1.3)+
geom_circle(data = origCity, aes(x0 = Long, y0 = Lat, r = Percent),
fill = "black", color = "black", alpha = 0.5, show.legend = FALSE) +
scale_size_continuous(range = c(20, 5))+
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank())+
theme(axis.ticks.x = element_blank(),
axis.title.x = element_blank(),
axis.text.x = element_blank())+
theme(axis.ticks.y = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank())
print(gg1)
Any suggestions are greatly appreciated!

How to add two (same) legends in ggplot and change legend title and labels?

I would like to add two (same) legends in ggplot and also want to change legend title and labels. I have tried this:
library(ggplot2)
ggplot(ToothGrowth, aes(x = len, color=factor(dose), fill= factor(dose))) +
geom_density(alpha=0.4) +
theme(panel.background = element_rect(fill = "khaki1", colour = "darkorchid3", size = 2, linetype = "solid"),
panel.grid.major = element_line(size = 0.5, linetype = 'solid', colour = "white"),
panel.grid.minor = element_line(size = 0.25, linetype = 'solid', colour = "white"),
plot.background = element_rect(fill = "bisque2"),
text = element_text(colour="blue4"), axis.title = element_text(size = rel(1.25)), axis.text = element_text(colour="blue4", size = 12),
legend.position=c(.90,.85), legend.background =
element_rect(fill="lightsalmon", colour = "tomato3", size = 1.25),
legend.title = element_text(colour="navy", face="bold"),
legend.text = element_text( colour="midnightblue", face="bold"), strip.background = element_rect(fill="olivedrab1", colour = "darkorchid3", size = 2, linetype = "solid"),
strip.text = element_text(colour="coral4", size=12, angle=0, face="bold")) +
scale_fill_discrete(name = "Dose", labels = c("A", "B", "C")) +
facet_wrap(~supp)
but I got this plot:
I want this plot:
Can somebody help me? Thank you.
As #erocoar and others have suggested, grid.arrange from gridExtra is useful here. Borrowing heavily from from the linked question:
library(gridExtra)
out <- by(data = ToothGrowth, INDICES = ToothGrowth$supp, FUN = function(m) {
m <- droplevels(m)
m <- ggplot(m, aes(x = len, fill= factor(dose)), color=factor(dose)) +
geom_density(alpha=0.4) +
theme(panel.background = element_rect(fill = "khaki1", colour = "darkorchid3",
size = 2, linetype = "solid"),
panel.grid.major = element_line(size = 0.5, linetype = 'solid',
colour = "white"),
panel.grid.minor = element_line(size = 0.25, linetype = 'solid',
colour = "white"),
plot.background = element_rect(fill = "bisque2"),
text = element_text(colour="blue4"),
axis.title = element_text(size = rel(1.25)),
axis.text = element_text(colour="blue4", size = 12),
legend.position=c(.90,.85),
legend.background = element_rect(fill="lightsalmon",
colour = "tomato3", size = 1.25),
legend.title = element_text(colour="navy", face="bold"),
legend.text = element_text( colour="midnightblue", face="bold"),
strip.background = element_rect(fill="olivedrab1",
colour = "darkorchid3", size = 2,
linetype = "solid"),
strip.text = element_text(colour="coral4", size=12, angle=0,
face="bold")) +
scale_fill_discrete(name = "Dose", labels = c("A", "B", "C")) +
xlim(0,35) +
ylim(0,0.2) +
ggtitle(m$supp)
})
do.call(grid.arrange, list(grobs = out, ncol = 2))
Some things to note.
I moved the color argument outside of the aes() call and this removed the extra legend.
I manually set the x and y limits for a consistent look.
I needed to add a title.
To get the plots side by side I had to add a second argument to do.call(). When supplying more than one argument it needs to be in a list.
I hope this helps.

Resources