简体   繁体   English

使用geom_col和geom_line的ggplot的图例格式

[英]Legend formatting for ggplot with geom_col and geom_line

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 图例不正确 图例不正确的图

Map fill for geom_col (or geom_area , looks the same): geom_col地图fill (或geom_area ,看起来相同):

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"))

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM