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)
Related
I have created 2 plots and combined them as seen below but i now wish to plot this with one legend and the same colour scheme for both plots, does anyone know what changes i need to make this?
code used to create plots
#plotting actual from 1998-2020
mapplot_temp = ggplot(map_preds20, aes(x = long, y = lat, group = group)) +
geom_polygon(aes(fill = GDP_pdiff), colour = "black")
mapplot1 = mapplot_temp + scale_fill_gradient(name = 'GDP per capita change %', low =
'red', high = 'yellow', na.value = 'grey') +
theme_bw() +
theme(panel.border = element_blank(),
panel.grid = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.ticks = element_blank(),
plot.title = element_text(hjust = 0.5, size = 20, face = "bold"))
#plotting predicted from 2021-2040
mapplot_temp2 = ggplot(map_preds40, aes(x = long, y = lat, group = group)) +
geom_polygon(aes(fill = GDP_pdiff), colour = "black")
mapplot2 = mapplot_temp2 + scale_fill_gradient(name = 'GDP per capita change % ($)', low = 'red', high = 'yellow', na.value = 'grey') +
ggtitle('Predicted GDP per capita change') +
theme_bw() +
theme(panel.border = element_blank(),
panel.grid = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.ticks = element_blank(),
plot.title = element_text(hjust = 0.5, size = 20, face = "bold"))
#combine plots with one legend and same colour scheme
mapplot1 + mapplot2
plot
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
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!
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.
If I have a scale_fill_coninuous(name='Servings') and I want it to be in white. How can I set a theme parameter for this? adding color='white' to the above code doesn't work. Here is my ggplot.
ggplot() +
geom_map(data = map.world,map = map.world,colour='white',size=.25, aes(
map_id = region,
x=long,
y=lat,
fill=beer_servings
)) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
plot.background = element_rect(fill = "black"),
panel.background = element_rect(fill = "black"),
legend.background = element_rect(fill='black',
size=0.5, linetype="solid"),
legend.text = element_text(colour = 'white'),
legend.position = 'bottom'
)+
scale_fill_continuous(name = "Servings Of Beer \nPer Person, 2010")+
#guides(fill = guide_legend(
#title.theme = element_text(size=10, colour = "white",angle = 0)))+
coord_quickmap()
Sorry, Found this.
theme(legend.title =
element_text(face = "italic", size = 12, angle = 45, color = "grey70") )