geom_point and geom_col fill color change and legend label change
21,069
Just remove the quotes from fill = "company"
. It should work as you expect.
Here's the full source code again, but with the mentioned fix and also respecting Google's R Style Guide for indentation, it does help on readability and finding missed pieces like that :).
df = data.frame(
measure = c("Measure A", "Measure B", "Measure C"),
overall = c(9, 5, 11),
company = c(4, 6, 3)
)
ggplot(df, aes(measure)) +
geom_col(aes(y = company,
fill = company)) +
geom_point(aes(y = overall,
color = "overall"),
size = 8,
shape = 124) +
scale_color_manual(
values = c("company" = "yellow",
"overall" = "blue"),
labels = c("company" = "Your Company",
"overall" = "Overall Benchmark")
) +
coord_flip() +
guides(size = FALSE) +
theme(
legend.box = "horizontal",
legend.key = element_blank(),
legend.title = element_blank(),
legend.position = "top"
)
Author by
PinkyL
Updated on July 16, 2022Comments
-
PinkyL almost 2 years
My dataframe looks something like this:
df = data.frame( measure = c("Measure A","Measure B","Measure C"), overall = c(9, 5, 11), company = c(4,6,3) )
I am trying to plot bars for the company, and using geom_point, "lines" for the overall. For some reason, the company color fill and label for the legend doesn't change even though my code specifies it:
ggplot(df, aes(measure)) + geom_col(aes(y=company, fill="company")) + geom_point(aes(y=overall, color="overall"), size=8, shape=124) + scale_color_manual(values=c("company" = "yellow", "overall"="blue"),labels=c("company" = "Your Company", "overall"= "Overall Benchmark")) + coord_flip()+ guides(size=FALSE) + theme(legend.box="horizontal",legend.key=element_blank(), legend.title=element_blank(),legend.position="top")
The bars stay red and the legend reads company regardless. Is there a way to fix this?
-
aosmith almost 7 yearsYou used
fill
for the bars.scale_color_manual
is forcolor
, notfill
. Usescale_fill_manual
to change thefill
color and labels. -
PinkyL almost 7 yearsThat makes total sense.
-