I'm trying to modify the legend to a map by removing the borders around particular cells within the legend.
A simplified version of my code looks like this:
ggplot() +
stat_density2d(data = carto#data,
aes(x=field_2,y=field_1),
geom="polygon",
alpha = .37,
fill = "#e29206") +
geom_point(data = Schools#data[Schools#data$sch_type == "Charter" | Schools#data$sch_type == "District", ],
aes(x = x, y = y, color = sch_type, shape = grade_cat),
size = 1) +
scale_colour_manual(values=c("#e0100d", "#4753ff")) +
guides(color=guide_legend(override.aes=list(fill = "white"))) +
theme(legend.key = element_blank()) +
coord_map()
which produces the following image:
I would like to remove the blue and red borders on the top two legend cells. If I add color to the override.aes() arguments within guides() it changes the borders, but also makes the charter and district colors the same. Is there a different argument I could use in the place of color?
I looked at these two questions (among various sources) before posting:
Different legend-keys inside same legend in ggplot2
ggplot2 avoid boxes around legend symbols
Related
This is my first post on Stack Overflow, my first reproducible example, and I'm new to R, so please be gentle!
I am trying to display two histograms on one plot. Each histogram is a different variable (column) in my dataframe. I can't figure out how to both colour in the bars and have the legend displayed. If I use scale_fill_manual the colours are ignored, but if I use scale_colour_manual the colours are just the outlines of the bars. If I map the colours to each histogram separately (and don't use scale_xxx_manual at all) the colours work great but I then don't get the legend.
Here is my code:
TwoHistos <- ggplot (cars) +
labs(color="Variable name",x="XX",y="Count")+
geom_histogram(aes(x=speed, color= "Speed"), alpha = 0.2 ) +
geom_histogram(aes(x=dist, color= "Dist"), alpha = 0.2) +
scale_colour_manual(values = c("yellow","green"))
TwoHistos
Here is my result in an image (I pasted it but I don't know why it isn't showing up. I'm sorry!):
Two histograms with outlines for colours
I think (if I understand you correctly), what you might want is to give a fill arguement within the geom_histogram() call.
(I've used the mtcars built-in R data here as you did not give any data to work with)
TwoHistos <- ggplot (mtcars) +
labs(fill="Variable name",x="XX",y="Count")+
geom_histogram(aes(x=hp, fill= "Speed", color = "yellow"), alpha = 0.2 ) +
geom_histogram(aes(x=disp, fill= "Dist", color = "green"), alpha = 0.2) +
scale_fill_manual(values = c("yellow","green"))+
scale_colour_manual(values = c("yellow","green"), guide=FALSE)
TwoHistos
Edit: just to make really clear that I've changed the x in the geom_histogram() so it works with mtcars
Use fill instead of color and use scale_fill_manual
TwoHistos <- ggplot (cars) +
labs(color="Variable name",x="XX",y="Count")+
geom_histogram(aes(x=speed, fill= "Speed"), alpha = 0.2 ) +
geom_histogram(aes(x=dist, fill= "Dist"), alpha = 0.2) +
scale_fill_manual(values = c("yellow","green"))
TwoHistos
I am making a personality survey that will generate score reports for participants. I want to make these as easy to read and understand as possible, so I am generating a normal curve for the surveyed trait and a line showing the person where they fall on the curve.
First, let's generate some data:
sample <- as.data.frame(rnorm(1000, 0, 1))
names(sample) <- "trait"
score <- mean(sample$trait)
My problem is with the legend—I cannot figure out how to customize the legend to display 1) a filled "population" color when I'm not graphing multiple factors, and 2) the line showing the participant's score.
I can get close:
ggplot(sample, aes(x=trait)) +
geom_density(fill="blue") +
geom_density(aes(fill="Population")) +
geom_vline(aes(xintercept=score, color="You")) +
geom_vline(xintercept=score, color='red',
linetype="solid",size=1.5) +
scale_colour_manual(values=c('Population'='blue',
'You'='red'))
Graph image 1
But this does not use the colors specified, and has extraneous "colour" and "fill" text in the legend.
If I change the geom_density aesthetic to color instead of fill and leave everything else the same...
geom_density(aes(color="Population")) +
...This correctly applies the colors, but then does not fill the "Population" box in the legend with blue.
Graph image 2
Optimally, I'd like to fill the "Population" box blue and remove the red box around the "You" line in the legend. How can I achieve this?
I hope this can be used.
ggplot(sample, aes(trait)) +
geom_density(aes(fill = "Population")) +
geom_vline(aes(xintercept = mean(trait), color = "You")) +
theme(legend.title = element_blank()) +
scale_color_manual(values = "red", breaks = "You") +
scale_fill_manual(values = "blue", breaks = "Population")
I am using the following code to plot my data but I cannot manage to set the colours to geom_ribbon properly.
My graph contains 4 lines, each of one with a different color. I want the 'geom_ribbon' of each line to have the same color as its line (with transparency - alpha).
In addition, when I change the value of alpha (e.g. from 0.1 to 0.9) I dont't see any change on the transparency. Finally, an extra class is added in the legend and I would like to remove this? Any help on this basic ggplot?
ggplot(dfmean_forplot, aes(x = image, y = value, group = ID)) +
geom_line(aes(colour=factor(ID)))+
scale_x_discrete(breaks=1:21,
labels=c("19/1","7/2","17/2","18/3","17/4","27/4","17/5","27/5","7/6","16/6","26/6","5/7","16/7","6/8","15/8","25/8","4/9","25/9","4/10","14/10","22/11"))+
xlab("# reference")+
ylab("value")+
scale_colour_discrete(name = "class")+
ylim(0,0.9)+
geom_ribbon(aes(ymin=dfmean_forplot$value-dfsd_forplot$value, ymax=dfmean_forplot$value+dfsd_forplot$value, alpha = 0.3))
EDIT
What about the legend? Ideally, I would like to combine them so that there is a square for each color crossed by a line of the same color
You need to add the fill aesthetic and take alpha outside aes, both for geom_ribbon. The following code should solve that.
ggplot(dfmean_forplot, aes(x = image, y = value, group = ID)) +
geom_line(aes(colour=factor(ID)))+
scale_x_discrete(breaks=1:21,
labels=c("19/1","7/2","17/2","18/3","17/4","27/4","17/5","27/5","7/6","16/6","26/6","5/7","16/7","6/8","15/8","25/8","4/9","25/9","4/10","14/10","22/11"))+
xlab("# reference")+
ylab("value")+
scale_colour_discrete(name = "class")+
ylim(0,0.9)+
geom_ribbon(aes(ymin=dfmean_forplot$value-dfsd_forplot$value,
ymax=dfmean_forplot$value+dfsd_forplot$value,
fill = factor(ID)), alpha = 0.3)
Using ggplot2 to create a boxplot, I ran into the issue of needing to specify legend colour.
Before adjustment each box had a filled centre and black border Before picture here. However, after adjusting legend colour the fill is white and the border is the specified colour. After picture here How would I go about getting my specified colour as the fill with a black border?
ggplot(Data.Benin, aes(x=countrycode, y=mortality, color=year)) +
geom_boxplot(notch=F)+ scale_color_manual(values=c("coral1",
"darkolivegreen4", "gold4", "deepskyblue2", "darkorchid1"))+labs(x=" ",
y="Deltamethrin mortality", fill="Year")+theme_gray()
Thank you in advance.
color changes the border color, what you want is fill:
ggplot(Data.Benin, aes(x = countrycode, y = mortality, fill = year)) +
geom_boxplot(notch = F) +
scale_fill_manual(values = c("coral1", "darkolivegreen4", "gold4", "deepskyblue2", "darkorchid1")) +
labs(x = " ", y = "Deltamethrin mortality", fill = "Year") +
theme_gray()
Note that you correctly had fill = inside labs but the legend title of your plot was the lowercase year (i.e. it had no effect since you didn't set a fill aesthetic).
Also in the future you might want to include data with your question so others can replicate your issue. See how to make a great R reproducible example.
My data set contains three columns, name, value and indicator. I want to plot a bar graph with these three variables. the following is my code,
ggplot(data , aes(x = factor(name), y = value, color = as.factor(indicator))) + geom_bar(stat = "identity" ) +
scale_color_manual(values = c("lightblue", "red"), guide = guide_legend(title = "text", labels = c('A', 'B'))) +
xlab("xlab") + ylab("ylab") + ggtitle("title")
and following is the graph I got,
But the legend here is not showing the lightblue and red. Only the outline is of that colour, but inside it shows only grey. Can anybody help me in filling the legend with the similar color as the graph color. Also I want to change the 0,1 to A and B text.
Can anybody help me in doing this? Any help would be appreciated,
Thanks
The color aesthetic only changes the outline color of bars in ggplot2. To change the color of the entire bar you'll want to use fill instead.
After replacing color with fill in your code, you can change the colors used along with the title and labels of the legend in scale_fill_manual.
scale_fill_manual(values = c("lightblue", "red"), name = "text", labels = c('A', 'B'))