I have a plot (below) that I am trying to control the legend for. The trouble is that I have both geom_line(aes(linetype = season)) and geom_bar(aes(linetype = season)). I'd like to display the legend for the geom_line (as in plot 1 below) rather than the legend for the geom_bar (as in plot 2 below).
If I try using scale_linetype(guide = F), it turns off the legend for both. Is there a way to display the legend for the geom_line linetype but not the geom_bar linetype? Below is the code to produce the plots.
Why? Ultimately I would like the outline of the bars to match the lines, with a nicely displayed legend. As you can see plot 2 is almost there but the legend is, well, illegible
# code for plot 1
ggplot() +
geom_line(data = temp, aes(pos, res, linetype = season)) +
geom_bar(data = temp2, aes(depth, res, fill = season),
stat = "identity", position = "dodge", color = "black") +
scale_fill_manual(values = c("white", "black"), guide = F)
# code for plot 2
ggplot() +
geom_line(data = temp, aes(pos, res, linetype = season)) +
geom_bar(data = temp2, aes(depth, res, linetype = season, fill = season),
stat = "identity", position = "dodge", color = "black") +
scale_fill_manual(values = c("white", "black"), guide = F)
Answered by joran below in a comment. Used show_guide in the geom_bar call, code shown below
# plot 3
ggplot() +
geom_line(data = temp, aes(pos, res, linetype = season)) +
geom_bar(data = temp2, aes(depth, res, linetype = season, fill = season),
stat = "identity", position = "dodge", color = "black",
show_guide = F) +
scale_fill_manual(values = c("white", "black"))
Related
I'm trying to create a more aesthetically pleasing graph. For some reason I am producing 2 legends at the top of my chart. I would like only the one on the right to remain, but I cannot figure out what I am doing wrong. Here is my code and graph.
plt <- ggplot(ac_total_melt, aes(Date, Value, fill = Action)) +
geom_line(
aes(color = Action),
size = .9
)+
geom_point(aes(colour = Action))+
ylab("")+
scale_color_manual(values=c('Dark Green','Dark red'), labels = c("Number Closed", "Number Opened"))+
geom_text_repel(
aes(color = Action, label = Value),
show.legend = FALSE,
family = "Lato",
fontface = "plain",
size = 4,
direction = "y"
) +
theme(legend.position="top")+
guides(color = guide_legend (override.aes = list(linetype = 0, size=5)))
plt
Data:
Looks like I needed to add show_guide = F to geom_point
The issue is that you re-labeled the fill scale, but not the color scale, so ggplot isn't able to merge them:
plt <- ggplot(ac_total_melt, aes(Date, Value, fill = Action, color = Action)) +
geom_line(size = .9) +
geom_point() +
ylab("") +
scale_color_manual(
values=c('blue', 'green'),
labels = c("Number Closed", "Number Opened"),
aesthetics = c("color", "fill") # apply this scale to both color and fill
) +
geom_text_repel(
aes(label = Value),
show.legend = FALSE,
family = "Lato",
fontface = "plain",
size = 4,
direction = "y"
) +
theme(legend.position = "top")+
guides(color = guide_legend(override.aes = list(linetype = 0, size = 5)))
plt
I have the following code. I'd like to change the color of the boxplots so they all have the same fill color (grey).
Also I'd like to have the stat_summary texts to stick to the bottom of each barplot but vjust only seem to provide relative position?
Thanks
boxp <- ggplot(mtcars, aes(as.factor(cyl), wt, fill=as.factor(am)) ) +
geom_bar(position = "dodge", stat = "summary", fun.y = "median") +
geom_boxplot(outlier.shape = NA, width=0.2, color = "black", position = position_dodge(0.9)) +
stat_summary(aes(label=round(..y..,2)), fun.y=median, geom="text", size=8, col = "white", vjust=8, position = position_dodge(0.9)) +
stat_summary(fun.y=mean, geom="point", shape=18, size=4, col="white", position = position_dodge(0.9)) +
labs(x = "Conditions", y = "Medians") +
scale_y_continuous(limits=c(0,7),oob = rescale_none) +
theme_bw()
boxp
Here is a possible solution, but it needs ggplot v3.3.0 for the stage() function.
To point out major changes:
Instead of using the fill as an implicit grouping, I've explicitly set the grouping so it isn't tied to the fill.
I added the fill as an aesthetic of the bar geom.
The boxplot now has the unmapped aesthetic fill = 'gray'
The text stat summary uses stage() to calculate the statistic but then uses 0 as actual placement.
library(ggplot2)
library(scales)
ggplot(mtcars, aes(as.factor(cyl), wt,
group = interaction(as.factor(cyl), as.factor(am)))) +
geom_bar(aes(fill=as.factor(am)), position = "dodge", stat = "summary", fun = "median") +
geom_boxplot(outlier.shape = NA, width=0.2,
color = "black", fill = 'gray',
position = position_dodge(0.9)) +
stat_summary(aes(label=round(after_stat(y), 2), y = stage(wt, after_stat = 0)),
fun=median, geom="text", size=8, col = "white", vjust=-0.5,
position = position_dodge(0.9)) +
stat_summary(fun=mean, geom="point", shape=18, size=4, col="white", position = position_dodge(0.9)) +
labs(x = "Conditions", y = "Medians") +
scale_y_continuous(limits=c(0,7),oob = rescale_none) +
theme_bw()
Created on 2020-05-06 by the reprex package (v0.3.0)
I have these stacked bar charts and the labels are touching, causing it to be difficult to differentiate. I would prefer if I was able to separate them a bit, as the information is important to present clearly!
Here is the plot:
Here is my code:
ggplot(data =GraphData, aes(x = factor(Year), y = value, fill = variable)) +
geom_bar(position = "fill", stat = 'identity') +
geom_col(position = position_stack(), color = "black")+
geom_text(data = GraphPercent, aes(y = GraphData$value, label = paste0(percent(value),"(", GraphData$value, ")")),
position = position_stack(vjust = 0.5), size = 3.5, col = c("black"))+
scale_fill_grey(start = 0.6, end = 1)+
theme(panel.background = element_blank(),axis.line = element_line(colour = "black"),)+
xlab("Year") +
ylab("Cases")
Thanks a lot I really appreciate it.
I have a plot (below) that I am trying to control the legend for. The trouble is that I have both geom_line(aes(linetype = season)) and geom_bar(aes(linetype = season)). I'd like to display the legend for the geom_line (as in plot 1 below) rather than the legend for the geom_bar (as in plot 2 below).
If I try using scale_linetype(guide = F), it turns off the legend for both. Is there a way to display the legend for the geom_line linetype but not the geom_bar linetype? Below is the code to produce the plots.
Why? Ultimately I would like the outline of the bars to match the lines, with a nicely displayed legend. As you can see plot 2 is almost there but the legend is, well, illegible
# code for plot 1
ggplot() +
geom_line(data = temp, aes(pos, res, linetype = season)) +
geom_bar(data = temp2, aes(depth, res, fill = season),
stat = "identity", position = "dodge", color = "black") +
scale_fill_manual(values = c("white", "black"), guide = F)
# code for plot 2
ggplot() +
geom_line(data = temp, aes(pos, res, linetype = season)) +
geom_bar(data = temp2, aes(depth, res, linetype = season, fill = season),
stat = "identity", position = "dodge", color = "black") +
scale_fill_manual(values = c("white", "black"), guide = F)
Answered by joran below in a comment. Used show_guide in the geom_bar call, code shown below
# plot 3
ggplot() +
geom_line(data = temp, aes(pos, res, linetype = season)) +
geom_bar(data = temp2, aes(depth, res, linetype = season, fill = season),
stat = "identity", position = "dodge", color = "black",
show_guide = F) +
scale_fill_manual(values = c("white", "black"))
I'm using the workaround to remove diagonal lines from a ggplot legend: https://groups.google.com/forum/?fromgroups=#!topic/ggplot2/vJnF9_HBqx4
With the following data, how do I change the colours of the groups?
# Create data #
a<-as.data.frame(c(1,1,1,2,2))
b<-as.data.frame(c("A","A","B","B","A"))
c<-as.data.frame(c(20,20,60,50,50))
a<-cbind(a,b,c)
colnames(a)<-c("X","Gp","Y")
# Plot #
ggplot(a, aes(x=X, y=Y,fill=Gp)) +
geom_bar(stat = "identity", aes(colour = "black")) +
scale_color_identity() +
theme(legend.key = element_rect(colour = "black", size = 1))
I have tried changing the following elements :
scale_color_identity(values=c("red","yellow"))
geom_bar(stat = "identity", aes(colour = c("red","yellow")))
geom_bar(stat = "identity", aes(colour = "black"), fill=c("red","yellow"))
but each produces an error.
Try this. The guides call let's you pick which scale to not have a legend. And, you can set the outline colour without aes().
EDIT after comment about diagonal line in legend
Based on this SO question remove diagonal line in legend, you can added the guides(fill etc. call to remove the diagonal.
ggplot(a, aes(x=X, y=Y,fill=Gp)) +
geom_bar(stat = "identity", colour = "black") +
scale_fill_manual(values = c("red","yellow")) +
guides(fill = guide_legend(override.aes = list(colour = NULL))) +
guides(colour = FALSE)
You can also call geom_bar twice. Once for the legend, and without the color argument, and once with the color argument but suppressing the legend
ggplot(a, aes(x=X, y=Y,fill=Gp)) +
geom_bar(stat = "identity", color = 'black', show_guide = F) +
geom_bar(stat = 'identity') +
scale_fill_manual(values = c('red', 'yellow') )