Reputation: 21
I am attempting to do the following code to get a legend that has a red line for "Capacity" and a black box for "Demand" instead of the legend in the image with a black box with red outline for "Capacity"
ggplot(df, aes(x,y2)) +
geom_line(aes(x,y2,colour = "Capacity")) +
geom_col(aes(x,y1,colour="Demand"), fill = "black") +
theme(axis.ticks.x = element_blank(), axis.text.x = element_blank(), plot.title = element_text(face = "bold", hjust = 0.5)) +
labs(title = paste("Optimal Schedule", check[[4]], "-", check[[5]], sep = " "), x = "Time (hours)", y = "Driver Hours") +
ylim(0,max(df$y3)) +
scale_colour_manual("", values = c("red", "black"), guide = guide_legend(override.aes = list(linetype = c("solid","blank")), shape = c(NA,NA)))
Graph with incorrect legend
Upvotes: 2
Views: 2113
Reputation: 23899
Map fill
for geom_col
(or geom_area
, looks the same):
library(ggplot2)
df <- data.frame(Capacity = cumsum(rnorm(1000)))
df$Time <- 1:nrow(df)
df$Demand <- df$Capacity * 0.8
ggplot(df, aes(x = Time)) +
geom_line(aes(y = Capacity,colour = "Capacity")) +
geom_col(aes(y = Demand, fill = "Demand")) +
scale_colour_manual("", values = c("red")) +
scale_fill_manual(values = c("black"))
Upvotes: 1