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.
Related
This question already has an answer here:
Cannot remove grey area behind legend symbol when using smooth
(1 answer)
Closed 3 years ago.
I am trying to make a fitted plot using ggplot2. With geom_smooth(), the fitted lines are blue in color. I tried by adding color = "black" into the geom_smooth. You can see that lines were changed to black, but i still see grey background in the legend area. In the first I would like to remove that grey background or increase the width for better representation of dotted lines in legend area and second I would like to add "npg" color or different color palette of my interest to it. Can anyone help to fix these issues. Here is the code
df<-data.frame(y=rnorm(200,30,20),x=rnorm(200,20,10),treat=factor(rbinom(100,1,0.5)))
p1 <- ggplot(df, aes(x=x, y=y, group=treat))+
geom_point(size = 2)+
geom_smooth(aes(linetype=treat), color = "black")+
labs(x ="tf", y = "xm") +
theme_bw() +
scale_linetype_manual(values=c( "solid", "dashed"),
labels = c("1", "2"))
p1
Here is another way to do it using two stat_smooth calls. The first one draws the line only, the second one includes the se but omits the legend:
geom_smooth(aes(linetype=treat), color='black', se=FALSE)+
geom_smooth(aes(linetype=treat), color='black', show.legend=FALSE)+
credit: https://groups.google.com/forum/#!topic/ggplot2/zEIKqD-QeoY
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)
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
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'))