line type legend not showing and background in not

2019-09-14 19:39发布

问题:

I have this plot:

dates = as.numeric(as.Date(c("2017-03-13","2017-03-13","2017-03-14","2017-03-14","2017-03-14")))
value = c(5,6,7,8,9)
group = c("A","B","A","B","C")
size = c(10,20,30,40,50)
data =data.frame(dates= dates, value = value, group = group, size = size)
ggplot(data, aes(x = dates, y = value, group = group)) + geom_point(aes (color = as.factor(group), size= size ))+
  geom_hline(aes(yintercept = 6,linetype ="dashed"),color = "green") +
  geom_hline(aes(yintercept = 7,linetype ="dashed"),color = "blue") +
  geom_vline(aes(xintercept = as.numeric(as.Date("2017-03-13") ),linetype ="dashed"),color = "yellow") +
  geom_vline(aes(xintercept = as.numeric(as.Date("2017-03-14") ),linetype ="dashed"),color = "yellow") +
  #geom_vline(aes(xintercept = "2017-03-13",linetype ="dashed"),color = "red")+
  scale_color_manual(name="group",
                     labels = c(A="A",B= "B",C = "C"),
                     values = c(A="green",B="red" , C = "orange" )
  )  +scale_linetype_manual("linetype",labels=c("y1", "y2","datelines","datelines"),values=c("solid","solid","solid","solid") ) +
  theme(
    panel.background = element_rect(fill = "black", colour = "black"),
    legend.key = element_rect(colour = "black", fill = "black")
  )+ 
  guides(
    size = guide_legend(override.aes = list(color = "red")),
    linetype = guide_legend(override.aes = list(fill = "white"))
    )

Questions:

(1) I am using scale_linetype_manual so I am expecting a legend called "linetype" with 3 types of lines: y1 , y2 and datelines (both the horizontal lines have the same label and color) BUT I only see a y1 legend. How can I see 3 legends one for y1, y2 and datelines?

So i'm trying to get to a "linetype" legend with a green dashed line called y1, a blue dashed line called y2 and a yellow line called datelines all with a white background

(2) I'd like the backgound of the "linetype" legend to be white so I used

linetype = guide_legend(override.aes = list(fill = "white"))

but the background still shows white.

标签: r ggplot2