This must have annoyed someone in the past so excuse me if this is a duplicate and I will remove it. Slashes across legends when using geom_bar can be annoying. e.g.:
x <- c("a","b")
y <- c(1,2)
df <- as.data.frame(cbind(x,y))
a <- ggplot(df,aes(x=x,y=y,fill=x))
a + geom_bar(colour="black") + scale_fill_manual(values=c("white", "black"))
When I use coloured bars I use this work around, plotting bars without colours first e.g.
a + geom_bar() + geom_bar(colour="black",show_guide=FALSE) +
scale_fill_manual(values=c("white", "black"))
However when the fill is white this leaves an unsatisfying empty white box in the legend without a border. e.g.
I have fixed this in the past manually using graphics software but now I think this must be of use to enough people to ask a question here. Can we make ggplot plot the legend with the black outline only but without the slash?
this,
a + geom_bar() + geom_bar(colour="black",show_guide=FALSE) +
scale_fill_manual(values=c("white", "black")) +
opts(legend.key = theme_rect(fill = 'black'))
gave me this,
thanks to this site.
Alos, you get the same result using colour instead of fill (it might be argued that one is better than).
a + geom_bar() + geom_bar(colour="black",show_guide=FALSE) +
scale_fill_manual(values=c("white", "black")) +
opts(legend.key = theme_rect(colour = 'black'))
Important note: In modern versions of ggplot2 opts has been deprecated and replaced with theme, and theme_rect has been replaced by element_rect.
No that's what gives it it's outline. If you use a gray instead of white (with your trick) it's more distinguishable. You could also add background color to the legend to make it more distinguishable and keep it white. See the bottom of this page for more detail:
http://wiki.stdout.org/rcookbook/Graphs/Legends%20(ggplot2)/
i wish there were a less hackish way to do this.
Related
I've created a ggplot2 graph using the basic code below:
my_df %>%
ggplot(aes(conv_norm, vot_norm, color = language:poa)) +
geom_smooth(method = "glm", se=FALSE) +
theme(
...
)
[I've left out the formatting commands from the theme() layer]
And I got a graph that looks like this:
Now, my question is: how can I add extra space only in between two legend items? I've looked online and have found ways to increase the spacing between all items in the legend, but I only want extra spacing between the English items and the Spanish items. Is there a way to add a 1-in distance between these language groups?
Well, I don't know of an elegant, simple solution to do what you are asking to do... but by working with how legends are drawn and adjusting some of the elements, we can come up with a really "hacky" solution. ;)
Here's a sample dataset that kind of simulates what you shared, along with the plot:
set.seed(12345)
my_df <- data.frame(
lang = rep(c(paste('English',1:3), paste('Spanish',1:3)),2),
x = c(rep(0,6), rep(1,6)),
y = rnorm(12, 10,2))
library(ggplot2)
p <- ggplot(my_df, aes(x,y, color=lang)) + geom_line()
p
The approach here is going to be to combine all the following individual steps:
Add a "blank" legend entry. We do this by refactoring and specifying the levels of the column mydf$lang to include a blank entry in the correct position. This will be the final order of the items in the legend.
Use scale_color_manual() to set the colors of the legend items manually. I make sure to use "NA" for the blank entry.
Within scale_color_manual() I use the drop=FALSE setting. This includes all levels for a factor, even if there is no data on the plot to show. This makes our blank entry show on the legend.
Use the legend.background theme element to draw transparent boxes for the legend key items. This is so that you don't have a white or gray box for that blank entry.
Putting it all together you get this:
my_df$lang <- factor(my_df$lang, levels=c(paste('English',1:3), '', paste('Spanish',1:3)))
ggplot(my_df, aes(x,y, color=lang)) +
geom_line() +
scale_color_manual(
values=c(rainbow(6)[1:3], 'NA', rainbow(6)[4:6]),
drop=FALSE) +
theme( legend.key = element_rect(fill='NA') )
Alternatively, you could use guides(color=guide_legend(override.aes... to set the colors, but you need the drop=FALSE part within scale_color_manual() get the blank level to draw in the legend anyway.
Another option would be to create two separate legends. Either by using two different aesthetics, or you can use color twice, e.g with ggnewscale - thanks to user chemdork123 for the fake data +1.
library(tidyverse)
library(ggnewscale)
set.seed(12345)
my_df <- data.frame(
lang = rep(c(paste('English',1:3), paste('Spanish',1:3)),2),
x = c(rep(0,6), rep(1,6)),
y = rnorm(12, 10,2))
ggplot(mapping = aes(x,y)) +
geom_line(data = filter(my_df, grepl("English", lang)), aes(color=lang)) +
scale_color_brewer(NULL, palette = "Dark2") +
new_scale_colour() +
geom_line(data = filter(my_df, grepl("Spanish", lang)), aes(color=lang)) +
scale_color_brewer(palette = "Set1") +
guides(color = guide_legend(order = 1))
Created on 2021-04-11 by the reprex package (v1.0.0)
Is there a way to control the fill for the color legend when fill is also used in the aesthetic in ggplot2?
Ex.
dat<-data.frame(id=as.character(1:4), var1=rep(LETTERS[1:2], 2), var2=rep(LETTERS[3:4], 2))
dat
library(ggplot2)
ggplot(dat)+
geom_bar(aes(id, color=var1, fill=var2))+
scale_color_manual(values=c("A"="black", "B"="grey"))+
theme_light()
In this legend it is very hard to see the light grey border around the darker gray fill in the color legend. I will admit it may not be a good practice to specify color and fill in the same plot as it can become difficult to understand which color means what and the colored borders around the filled objects can be hard to differentiate, but how would I do it if I wanted to?
By manually specifying fill in the plot, the fill color in the legend can be controlled when not also specifying fill.
ggplot(dat)+
geom_bar(aes(id, color=var1), fill="white")+
theme_light()
I thought there would be a way to control this using an argument in theme, but theme(legend.background=element_rect()) controls the background of the whole legend (and rightly so)
ggplot(dat)+
geom_bar(aes(id, color=var1, fill=var2))+
scale_color_manual(values=c("A"="black", "B"="grey"))+
theme_light()+
theme(legend.background = element_rect(fill="blue"))
Also in theme, theme(legend.key = element_rect(fill="blue")) only adds a thin blue line around the var2 boxes in the legend, legend.box controls the arrangement of multiple legends ("horizontal" or "vertical"), and theme(legend.box.background = element_rect(fill="blue")) colors the background between the var1 and var2 legends.
I thought perhaps the legend was using the default value for NA for the fill, so I tried specifying this manually using scale_color_discrete()
ggplot(dat)+
geom_bar(aes(id, color=var1, fill=var2))+
scale_color_manual(values=c("A"="black", "B"="grey",
na.translate=TRUE, na.value = "white"))+
theme_light()
Looks the same as the first plot.
This seems like something that should be controlled by an argument in theme() or scale_color_manual, but I can't seem to figure it out.
Cheers
For data-related aspects of the legend, theme() won't help you. Instead you need to use guides and override the fill for the color legend:
ggplot(dat)+
geom_bar(aes(id, color=var1, fill=var2))+
scale_color_manual(values=c("A"="black", "B"="grey"))+
guides(color = guide_legend(override.aes = list(fill = "white"))) +
theme_light()
The general rule is that theme() only controls the overall look of the plot, not the data-dependent parts like scales (maybe instead of "data-dependent" it should be "the parts of the plot that vary based on data").
In the process of writing up this question and trying everything I could think of I did find a solution. It does seem a little hacky and requires over plotting.
dat<-data.frame(id=as.character(1:4), var1=rep(LETTERS[1:2], 2), var2=rep(LETTERS[3:4], 2))
library(ggplot2)
ggplot(dat)+
geom_bar(aes(id, fill=var2))+
geom_bar(aes(id, color=var1), alpha=0)+
scale_color_manual(values=c("A"="black", "B"="grey"))+
theme_light()
Here by layering color bar chart with alpha=0 on top of the fill bar chart I can get the desired plot, but it feels a little like a corruption of ggplot2. I hope a better answer exists.
I've only very recently started learning R. Now what I'm trying to do is to integrate two legends for the same plot. In other words, I want the default size legend to change color depending on it's size.
I have been Googling several solutions that apparently all don't seem to work, but again, I'm new to R so maybe I'm just doing something wrong.
My code:
ggplot(Caschool, aes(x=testscr, y=avginc), colour="green") +
geom_point(aes(size=enrltot, color=enrltot)) +
geom_smooth(colour="blue") +
labs(x="Test Score", y="Average Income", title="California Test Score Data", color="Number of Students\nPer District") +
theme(
panel.grid.minor = element_blank(),
panel.grid.major=element_line(colour="grey", size=0.4),
panel.background=element_rect(fill="beige"),
axis.line=element_line(size = 1.2, colour = "black"),
plot.title = element_text(size = rel(2))) +
scale_color_continuous(limits=c(0, 30000), breaks=seq(0,30000, by=2500)) +
guides(color= guide_legend(), size=guide_legend())
Apparently, I'm not allowed to post pictures, or I would have shown what this looks like so far.
ggplot2 can indeed combine size and colour legends into one, however, this only works, if they are compatible: they need to have exactly the same breaks, otherwise they can not be combined.
Let me make an example: Assume, you have values between 0 and 10 that you want to map on size and colour. You tell ggplo2 to use small points for values below 5 and large points for larger value. It will then plot a legend with a small and a large point, as expected. Now, you also want to add colour and you require points below 3 to be green and points above to be blue. ggplot2 will also draw a legend for this, but it is impossible to combine the two legends. The small point would have to be both, green and blue. The problem can be solved by using the same breaks for colour and size.
In your example, you manually change the breaks of the colour scale, but not those of the size scale. This results in incompatible legends that can not be combined.
I can not demonstrate this using your date, because I don't have it. So I will create an example with mtcars. The variant with incompatible legends is constructed as follows:
p <- ggplot(mtcars, aes(x=mpg, y=drat)) +
geom_point(aes(size=gear, color=gear)) +
scale_color_continuous(limits=c(2, 5), breaks=seq(2, 5, by=0.5)) +
guides(color= guide_legend(), size=guide_legend())
which gives the following plot:
If I now add the same breaks for size,
p + scale_size_continuous(limits=c(2, 5), breaks=seq(2, 5, by=0.5))
I get a plot with only one legend:
For your code, this means that you should add the following to your plot:
+ scale_size_continuous(limits=c(0, 30000), breaks=seq(0,30000, by=2500))
A little side remark: What do you intend by using colour = "green" in your call to ggplot? I don't see that this has any effect at all, because you set the colour again in both geoms that you use later. Maybe a relic from an older variant of the plot?
I'm having two different problems with specifying the colors in my legends in ggplot. I've tried to make a simplified examples that shows my problem:
df <- data.frame(x=rep(1:9, 10), y=as.vector(t(aaply(1:10, 1, .fun=function(x){x:(x+8)}))), method=factor(rep(1:9, each=10)), DE=factor(rep(1:9, each=10)))
ggplot(df, aes(x, y, color=method, group=DE, linetype=DE)) + geom_smooth(stat="identity")
For some reason, the line types shown in the legend under the title DE are all blue. I'd like them to be black, but I have no idea why they're blue in the first place, so I'm not sure how to change them.
For my other problem, I'm trying to use both point color and point shape to show two different distinctions in my data. I'd like to have legends for both of these. Here's what I have:
classifiers <- c("KNN", "RF", "NB", "LR", "Tree")
des <- c("Uniform", "Gaussian", "KDE")
withoutDE <- c(.735, .710, .706, .628, .614, .720, .713, .532, .523, .557, .677, .641, .398, .507, .538)
withDE <- c(.769, .762, .758, .702, .707, .752, .745, .655, .721, .733, .775, .772, .749, .756, .759)
df <- data.frame(WithoutDE=withoutDE, WithDE=withDE, DE=rep(des, each=5), Classifier=rep(classifiers, 3))
df <- cbind(df, Method=paste(df$DE, df$Classifier, sep=""))
ggplot() + geom_point(data=df, aes(x=WithoutDE, y=WithDE, shape=Classifier, fill=DE), size=3) + ylim(0,1) + xlim(0,1) + xlab("AUC without DE") + ylab("AUC with DE") + scale_shape_manual(values=21:25) + scale_fill_manual(values=c("pink", "blue", "white"), labels=c("Uniform", "KDE", "Gaussian")) + theme(legend.position=c(.85,.3))
If I change the color to change as well as the fill (by putting color=DE into the aes), then those are visible in the legend. I like having the black border around the points, though. I'd just like to have the inside of the points in the legend reflect the point fill in the plot. (I'd also like to position the two legends side-by-side, but I really just want to get the color to work right now)
I've spent way too long googling about both of these problems and trying various solutions without any success. Does anyone have any idea what I'm doing wrong?
For question 1:
Give the legend for line type and the legend for colour the same name.
ggplot(df, aes(x, y, color=method, group=DE, linetype=DE)) +
geom_smooth(stat="identity") +
scale_color_discrete("Line") +
scale_linetype_discrete("Line")
For question 2:
I do not think your fills are matching your data. You should assign the name of the value to each colour in the scale_x_manual calls.
I couldn't get the black border for the points. Here is what I was able to get, though:
ggplot() +
geom_point(data=df, aes(x=WithoutDE, y=WithDE, shape=Classifier,
fill=DE, colour=DE), size=3) +
ylim(0,1) + xlim(0,1) +
xlab("AUC without DE") +
ylab("AUC with DE") +
scale_shape_manual(values=21:25) +
scale_fill_manual(values=c("Uniform"="pink", "KDE"="blue", "Gaussian"="white"),
guide="none") +
scale_colour_manual(values=c("Uniform"="pink", "KDE"="blue", "Gaussian"="white"),
labels=c("Uniform", "KDE", "Gaussian")) +
theme(legend.position=c(.85,.3))
I don't know if you can control the point type inside the legends. Maybe someone else with more knowledge of ggplot2 can figure it out.
with ggplot2, I make the following density plot:
ggplot(iris) + geom_density(aes(x=Sepal.Width, colour=Species))
The colour legend (for each Species value) appears as a box with a line through it, but the density plotted is a line. Is there a way to make the legend appear as just a colored line for each entry of Species, rather than a box with a line through it?
One possibility is to use stat_density() with geom="line". Only in this case there will be only upper lines.
ggplot(iris)+
stat_density(aes(x=Sepal.Width, colour=Species),
geom="line",position="identity")
If you need also the whole area (all lines) then you can combine geom_density() with show_guide=FALSE (to remove legend) and stat_density() than will add legend just with horizontal lines.
ggplot(iris) +
geom_density(aes(x=Sepal.Width, colour=Species),show_guide=FALSE)+
stat_density(aes(x=Sepal.Width, colour=Species),
geom="line",position="identity")
The show_guide function used in the answer by #liesb is deprecated under ggplot 3.0.0; it has been changed to show.legend:
ggplot(iris) +
geom_density(aes(x=Sepal.Width, colour=Species),show.legend=FALSE) +
stat_density(aes(x=Sepal.Width, colour=Species),
geom="line",position="identity", size = 0) +
guides(colour = guide_legend(override.aes=list(size=1)))
ggplot(iris) +
stat_density(aes(x=Sepal.Width, colour=Species),
geom="line",position="identity")
Will do want you want.
You can get around plotting the lines twice by
ggplot(iris) +
geom_density(aes(x=Sepal.Width, colour=Species),show_guide=FALSE) +
stat_density(aes(x=Sepal.Width, colour=Species),
geom="line",position="identity", size = 0) +
guides(colour = guide_legend(override.aes=list(size=1)))
ps: sorry for not commenting on the obviously correct answer -- lack of rep issues :)
pps: I realise the thread is quite old but it helped me today, so it might help someone else sometime...