Force Ggplot Legend to Show All Categories When No Values Are Present

ggplot2: Legend does not show all categories even with drop=FALSE

You have already broken pval into discrete categories so you do not need to do so again in scale_fill_manual.

ggplot(pvalmat.m, aes(Principal_Component, Clinical_Variables)) + 
geom_tile(aes(fill = colorcut), colour = "white") +
scale_fill_manual(values = c("darkred", "red", "orange", "yellow", "gray"),
drop = FALSE,
name="P-value",
labels=c("< 0.001", "< 0.01", "< 0.05", "< 0.1", "> 0.1"))

Sample Image

Possible to force non-occurring elements to show in ggplot legend?

I have belatedly discovered that my question is a near-duplicate of this. The accepted answer there (from @joran) doesn't work for this but the second answer (from @Axeman) does. So the way for me to go here is that the last line should be

labels=c('red','blue','green'), limits=c('#FF0000','#0000FF','#00FF00'))

calling limits() instead of breaks(), and now my example and my real version work as desired.

I have to say I spent a lot of time digging around in the ggplot2 reference without ever gaining a suspicion that limits() was the correct alternative to breaks() -- which is explicitly mentioned in that ref page while limits() does not appear. The ?limits() page is quite uninformative, and I can't find anything that lays out the distinctions between the two: when this rather than that.

How can I force ggplot to show more levels on the legend?

If you set the limits to encompass the breaks you'll be able to alter the legend. Current most of the breaks are outside the default limits of the scale.

ggplot() + 
geom_point(data = nodos,
aes(x = ord, y = event, size = NP), color="black", shape = 16) +
scale_size_continuous(name = "Prop.",
breaks = bb,
limits = c(.05, .4),
labels = ll,
range = c(0, 6) )

Sample Image

ggplot2: force legend to have one row when guides does not appear to have an effect

You've specified colour instead of fill in guides. Try this instead:

library(ggplot2)
mpg$drv <- sample(as.character((-4:4)), size = length(mpg$drv), replace = T)
g <- ggplot(mpg, aes(class))
g + geom_bar(aes(fill = drv), position = "fill") + theme(legend.position = "bottom") + guides(fill = guide_legend(nrow = 1))

ggplot will not plot missing category

Here's a workaround you could use:

# generate dummy data 
set.seed(123)
df1 <- data.frame(lets = sample(letters[1:4], 20, replace = T),
y = rnorm(20), stringsAsFactors = FALSE)
# define factor, including the missing category as a level
df1$lets <- factor(df1$lets, levels = letters[1:5])
# make plot
ggplot(df1, aes(x = lets, y = y))+
geom_boxplot(aes(fill = lets))+
geom_point(data = NULL, aes(x = 'e', y = 0), pch = NA)+
scale_fill_brewer(drop = F, palette = 'Set1')+
theme_bw()

Sample Image

Basically, we plot an "empty" point (i.e. pch = NA) so that the category shows up on the x-axis, but has no visible geom associated with it. We also define our discrete variable, lets as a factor with five levels when only four are present in the data.frame. The missing category is the letter e.

NB: You'll have to adjust the positioning of this "empty" point so that it doesn't skew your y axis.

Otherwise, you could use the result from this answer to avoid having to plot an "empty" point.

# generate dummy data 
set.seed(123)
df1 <- data.frame(lets = sample(letters[1:4], 20, replace = T),
y = rnorm(20), stringsAsFactors = FALSE)
# define factor, including the missing category as a level
df1$lets <- factor(df1$lets, levels = letters[1:5])
# make plot
ggplot(df1, aes(x = lets, y = y)) +
geom_boxplot(aes(fill = lets)) +
scale_x_discrete(drop = F) +
scale_fill_brewer(drop = F, palette = 'Set1') +
theme_bw()

Sample Image

Default colors in ggplot2 for extended legend labels

scale_fill_manual, which is used in the question you liked to, is used, when the colors are getting defined. If you want the default colors, you can use scale_fill_discrete if your variable is discrete or scale_fill_continuous if your variable is numeric.

The example from the other question with specifying the colos would be:

ggplot(data =  dat, aes(x = Row, y = Col)) +
geom_tile(aes(fill = Y1), color = "black") +
scale_fill_discrete(labels = c("cat1", "cat2", "cat3", "cat4", "cat5", "cat6", "cat7", "cat8"),
drop = FALSE)

Hide unused levels in ggplot legend

OK; the related issue 4511 gives the answer. Setting limits = force in scale_fill_manual did it.



Related Topics



Leave a reply



Submit