ggplot legend.box.background produces strange relics - r

This is a bit harder to explain than my last question because the problem is not exactly reproducible.
I am producing legends for a couple of maps and am drawing a box around both legends since one has only 1 item (a line feature) and the others are discrete fills (a polygon feature). Using geom_sf to plot both.
I end up with a weird artefact that looks like part of the lines are drawn twice with just a slightly shifted position.
I managed to produce a similar error with the iris dataset where legend.box.background is only partially drawn.
data(iris)
ggplot(iris)+theme_classic()+
geom_point(aes(x=Petal.Length, y=Sepal.Length, color=Species, size=Sepal.Width))+
scale_color_manual(name=NULL, values=c("red","green","blue") ,labels=c("supersupersupersuperlong", "test2", "test3"))+
theme(legend.position=c(0.1,0.75),legend.box.background=element_rect(fill="white", color="black"), legend.spacing.y=unit(0,"cm"))
UPDATE
I noticed in my original example it had to do with text length, so I tried adding a space after some of the labels which changes the "arrangement" of the twice-drawn lines a little bit. But I can't find an arrangement of whitespace that makes it go away completely.
Anyone know how to manually change the size of the legend.box.background. If not I will draw a geometric rectangle and call it quits.

I think the problem here is that the legend.background (which is a white rectangle behind each component of your legend), is partially drawing over the line surrounding the legend.box, which is the rectangle surrounding the whole legend. You can simply remove the legend.background
For example, your plot goes from this:
ggplot(iris) +
theme_classic() +
geom_point(aes(x = Petal.Length, y = Sepal.Length, color = Species,
size = Sepal.Width)) +
scale_color_manual(name = NULL, values = c("red", "green", "blue"),
labels = c("supersupersupersuperlong", "test2", "test3")) +
theme(legend.position = c(0.1, 0.75),
legend.box.background = element_rect(fill = "white", color = "black"),
legend.spacing.y = unit(0, "cm"))
To this:
ggplot(iris) +
theme_classic() +
geom_point(aes(x = Petal.Length, y = Sepal.Length, color = Species,
size = Sepal.Width)) +
scale_color_manual(name = NULL, values = c("red", "green", "blue"),
labels = c("supersupersupersuperlong", "test2", "test3")) +
theme(legend.position = c(0.1, 0.75),
legend.background = element_blank(),
legend.box.background = element_rect(fill = "white", color = "black"),
legend.spacing.y = unit(0, "cm"))

Related

Adjust grid lines in ggplot+geom_tile (heatmap) or geom_raster

This heatmap has a grid builtin, which I am failing to find the way to customize.
I want to preserve horizontal lines in the grid, if possible increase thickness, and disable vertical lines. Each row should look as a continuous time-serie where data is present and blank where it is not.
Either adding vertical/horizontal lines on-top would possibly cover some data, because of that grid lines, or controlled gaps between tiny rectangles, is preferable.
Alternativelly, geom_raster doesn't shows any grid at all. With which I would need to add the horizontal lines of the grid.
I tried changing linetype, the geom_tile argument, which does seem to change the type or allow to fully disable it with linetype=0, fully disabling the grid, but it wouldn't allow to preserve horizontal grid-lines. I didn't saw any changes by modifying the size argument.
This is the code generating the plot as above:
ggplot( DF, aes( x=rows, y=name, fill = value) ) +
#geom_raster( ) +
geom_tile( colour = 'white' ) +
scale_fill_gradient(low="steelblue", high="black",
na.value = "white")+
theme_minimal() +
theme(
legend.position = "none",
plot.margin=margin(grid::unit(0, "cm")),
#line = element_blank(),
#panel.grid = element_blank(),
panel.border = element_blank(),
panel.grid = element_blank(),
panel.spacing = element_blank(),
#panel.grid = element_line(color="black"),
#panel.grid.minor = element_blank(),
plot.caption = element_text(hjust=0, size=8, face = "italic"),
plot.subtitle = element_text(hjust=0, size=8),
plot.title = element_text(hjust=0, size=12, face="bold")) +
labs( x = "", y = "",
#caption= "FUENTE: propia",
fill = "Legend Title",
#subtitle = "Spaces without any data (missing, filtered, etc)",
title = "Time GAPs"
)
I tried to attach DF %>% dput but I get Body is limited to 30000 characters; you entered 203304. If anyone is familiar with a similar Dataset, please advise.
Additionally,
There are 2 gaps at left&right of the plot area, one is seen inbetween the y-axis, and at the right you can see the X-axis outbounding, and are not controlled by a plot.margin argument.
I would want to set the grid to a thicker line when month changes.
The following data set has the same names and essential structure as your own, and will suffice for an example:
set.seed(1)
DF <- data.frame(
name = rep(replicate(35, paste0(sample(0:9, 10, T), collapse = "")), 100),
value = runif(3500),
rows = rep(1:100, each = 35)
)
Let us recreate your plot with your own code, using the geom_raster version:
library(ggplot2)
p <- ggplot( DF, aes( x=rows, y=name, fill = value) ) +
geom_raster( ) +
scale_fill_gradient(low="steelblue", high="black",
na.value = "white") +
theme_minimal() +
theme(
legend.position = "none",
plot.margin=margin(grid::unit(0, "cm")),
panel.border = element_blank(),
panel.grid = element_blank(),
panel.spacing = element_blank(),
plot.caption = element_text(hjust=0, size=8, face = "italic"),
plot.subtitle = element_text(hjust=0, size=8),
plot.title = element_text(hjust=0, size=12, face="bold")) +
labs( x = "", y = "", fill = "Legend Title", title = "Time GAPs")
p
The key here is to realize that discrete axes are "actually" numeric axes "under the hood", with the discrete ticks being placed at integer values, and factor level names being substituted for those integers on the axis. That means we can draw separating white lines using geom_hline, with values at 0.5, 1.5, 2.5, etc:
p + geom_hline(yintercept = 0.5 + 0:35, colour = "white", size = 1.5)
To change the thickness of the lines, simply change the size parameter.
Created on 2022-08-01 by the reprex package (v2.0.1)

Draw a Legend to an empty ggplot [duplicate]

This question already has answers here:
How to plot just the legends in ggplot2?
(4 answers)
Closed 4 years ago.
Is it possible to create a empty ggplot with no data, but draw a legend based on defined values in R?
So the image created is simply the legend?
Originally I thought I could plot the data and print a white rectangle over it, but given that not every background is white, it is unrealistic. I have tried the below, but it looks like ggplot wants some dataframe, therefore I had to add one. Is it possiable not to add one or create blank graph a different way?
library(ggplot2)
Outcome <- c("A", "B", "C", "D",'E')
shots <- rep('Label',5)
xc <-c(1:5)
yc <-c(1:5)
df <-data.frame(shots,Outcome,xc,yc)
p <- ggplot() +
geom_point(data = df, aes(x = xc, y = yc, fill = Outcome), shape=22, size=4,color = 'black', stroke=1) +
#Color and Legend
scale_fill_manual(values=c('chartreuse3','gainsboro','dodgerblue3','firebrick2','cornsilk4'),
labels = c("A", "B", "C", "D",'E'),
drop = FALSE) +
#Theme
theme(
panel.background = element_rect(fill = "transparent",colour = NA),
plot.margin = unit(c(0.1,0.1,0.1,0.1), "cm"),
plot.title = element_text(size = 14, hjust = 0.5, vjust = 1),
plot.background = element_rect(fill = "transparent", colour = NA),
axis.title=element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
legend.position = 'left',
legend.title=element_text(size=15),
legend.text=element_text(size=15),
legend.background = element_rect(fill = "transparent")
)
p
Do you want the full plot size, but with the space empty? If so, the override.aes parameter can help. Make the plotted points fully transparent (alpha=0), but the legend points fully opaque (alpha=1).
palette_color <- c("A"='chartreuse3', "B"='gainsboro', "C"='dodgerblue3', "D"='firebrick2', "E"='cornsilk4')
ggplot(df, aes(x = xc, y = yc, fill = Outcome)) +
geom_point(shape=22, alpha=0) +
# geom_blank() +
scale_fill_manual(values=palette_color, drop=FALSE) +
guides(fill = guide_legend(override.aes = list(alpha=1)))
I like your theme modifications, but I left them off here so it's more clear which elements are drawn where.
If this isn't what you're asking for, is it something addressed by geom_blank()?

Make legend invisible but keep figure dimensions and margins the same

I have made a plot with a legend.
Using an image editing program I made the legend invisible (but otherwise the figure has the same dimensions)
Is it possible to do this in ggplot2? I want to have a 2x2 panel of diagrams in a document but only one legend.
Using this as an example,
library(ggplot2)
p <- ggplot(mtcars, aes(x = disp, y = hp, color = factor(cyl))) +
geom_point() +
geom_line()
The following seems to work:
p + theme(
legend.text = element_text(color = "white"),
legend.title = element_text(color = "white"),
legend.key = element_rect(fill = "white")
) +
scale_color_discrete(
guide = guide_legend(override.aes = list(color = "white"))
)
Notice that the dimension of the gray plot area did not change.
Making the elements just white could cause problems, i.e. in cases of continuous scales or so. One may makes the scales and text elements just invisible.
p <- ggplot(mtcars, aes(x = disp, y = hp, lty = factor(gear))) +
geom_point(aes(color = cyl)) +
geom_line()
Gives a normal plot with legend:
Now make it really "invisible" by setting alpha = 0 in override.aes = list() within the guide = guide_legend() argument for each of the scales and color = "transparent" for the text elements of the legend:
p + scale_color_continuous(guide = guide_legend(override.aes = list(alpha = 0) ) )+
scale_linetype(guide = guide_legend(override.aes = list(alpha = 0) ) )+
theme(legend.title = element_text(color = "transparent"),
legend.text = element_text(color = "transparent"))

move legend title in ggplot2

I have been trying to shift my legend title across to be centered over the legend contents using the guide function. I've been trying to use the following code:
guides(colour=guide_legend(title.hjust = 20))
I thought of trying to make a reproducable example, but I think the reason it's not working has something to do with the above line not matching the rest of my code specifically. So here is the rest of the code I'm using in my plot:
NH4.cum <- ggplot(data=NH4_by_Date, aes(x=date, y=avg.NH4, group = CO2, colour=CO2)) +
geom_line(aes(linetype=CO2), size=1) + #line options
geom_point(size=3) + #point symbol sizes
#scale_shape_manual(values = c(1, 16)) + #manually choose symbols
theme_bw()+
theme(axis.text.x=element_text(colour="white"), #change x axis labels to white.
axis.title=element_text(size=12),
axis.title.x = element_text(color="white"), #Change x axis label colour to white
panel.border = element_blank(), #remove box boarder
axis.line.x = element_line(color="black", size = 0.5), #add x axis line
axis.line.y = element_line(color="black", size = 0.5), #add y axis line
legend.key = element_blank(), #remove grey box from around legend
legend.position = c(0.9, 0.6))+ #change legend position
geom_vline(xintercept=c(1.4,7.5), linetype="dotted", color="black")+ #put in dotted lines for season boundaries
scale_color_manual(values = c("#FF6600", "green4", "#0099FF"),
name=expression(CO[2]~concentration~(ppm))) + #manually define line colour
scale_linetype_manual(guide="none", values=c("solid", "solid", "solid")) + #manually define line types
scale_shape_manual(values = c(16, 16, 16)) + #manually choose symbols
guides(colour=guide_legend(title.hjust = 20))+
scale_y_continuous(expand = c(0, 0), limits = c(0,2200), breaks=seq(0,2200,200))+ #change x axis to intercept y axis at 0
xlab("Date")+
ylab(expression(Membrane~available~NH[4]^{" +"}~-N~(~mu~g~resin^{-1}~14~day^{-1})))+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())+
geom_errorbar(aes(ymin = avg.NH4 - se.NH4, #set y error bars
ymax = avg.NH4 + se.NH4),
width=0.1)
I have tried doing the following instead with no luck:
guides(fill=guide_legend(title.hjust=20)
I have also adjusted the hjust value from values between -2 to 20 just to see if that made a difference but it didn't.
I'll try to attach a picture of the graph so far so you can see what I'm talking about.
I've looked through all the questions I can on stack overflow and to the best of my knowledge this is not a duplicate as it's specific to a coding error of my own somewhere.
Thank-you in advance!!
The obvious approach e.g.
theme(legend.title = element_text(hjust = .5))
didn't work for me. I wonder if it is related to this open issue in ggplot2. In any case, one manual approach would be to remove the legend title, and position a new one manually:
ggplot(mtcars, aes(x = wt, y = mpg, colour = factor(cyl))) +
geom_point() +
stat_smooth(se = FALSE) +
theme_bw() +
theme(legend.position = c(.85, .6),
legend.title = element_blank(),
legend.background = element_rect(fill = alpha("white", 0)),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()) +
annotate("text", x = 5, y = 27, size = 3,
label = "CO[2]~concentration~(ppm)", parse = TRUE)
Output:

Control color of legend elements that are not colour guides in ggplot

When using a ggplot2 theme with a black background, can one control the legend colour for guides other than the colour guide so that things are not plotted in black? If so, how?
library(ggplot2) # needs to be 0.9.3 for this theme
data(iris) # included with ggplot2
theme_black<- function (base_size = 16, base_family = ""){
theme_minimal() %+replace%
theme(
line = element_line(colour = "white", size = 0.5, linetype = 1,
lineend = "butt"),
rect = element_rect(fill = "white",
colour = "white", size = 0.5, linetype = 1),
text = element_text(family = base_family,
face = "plain", colour = "white", size = base_size,
angle = 0, lineheight = 0.9, hjust = 0, vjust = 0),
plot.background = element_rect(colour = 'black', fill = 'black'),
plot.title = element_text(size = rel(1.2)),
panel.border = element_rect(fill = NA, colour = "white"),
panel.grid.major = element_line(colour = "grey20", size = 0.2),
panel.grid.minor = element_line(colour = "grey5", size = 0.5),
strip.background = element_rect(fill = "grey30", colour = "grey30")
)
}
ggplot(data=iris, aes(x=Sepal.Length, y=Sepal.Width, shape=Species,
colour=Petal.Length))+geom_point()+theme_black()+
scale_colour_gradient(low = "purple", high = "white")
As you can see, the default colour for the shape part of the legend has not been changed, so it is invisible and one cannot tell which species is which:
The only solution I have right now is to change the legend.background colour, but this is a waste of ink and ugly.
The short answer seems to be that there is no perfect way to do this.
We can add hidden layers with shape guides as per #user1317221_G's answer, which I accepted. But it is extra computation and if we save as pdf, I expect these hidden layers will be present.
Alternatively, we can override the shape guide's colour, as #bdemarest suggests in comments:
+ guides(shape=guide_legend(override.aes=list(colour="white"))
But this still forces us to add theme-specific code beyond just the +theme_black()
I've implemented a slightly more elaborate version of this because I set a default theme for the session depending on whether I am making plots for paper (grey/white background) or screen (black background). So my approach is to run something along these lines early in the session:
set_theme(theme_black); defaultcol = "white" # for slides
# or
set_theme(theme_bw); defaultcol = "black" # for paper
followed by a ggplot() that includes this:
+ guides(shape=guide_legend(override.aes=list(colour=defaultcol))
This has the advantage of minimizing the need for theme-specific adjustments to plots, though it is not as good as being able to control the default ggplot2 colour with a theme.
One way would be to add two extra geom_points, the logic would be:
plot white points for the legend, cover them with black points with no legend, then plot your coloured points with no legend,e.g.
geom_point(colour="white",size=1) +
geom_point(colour="black",size=3,show_guide=FALSE) +
geom_point(show_guide=FALSE)

Resources