我有这个情节:
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"))
)
问题:
(1)我正在使用scale_linetype_manual所以我期待一个名为“linetype”的图例有3种类型的线:y1,y2和日期线(两条水平线都有相同的标签和颜色)但是我只看到一个y1图例。我怎样才能看到y1,y2和datelines的3个图例?
所以我试图通过一条名为y1的绿色虚线和一条名为y2的蓝色虚线以及一条名为datelines的黄色线(全部为白色背景)进入“线型”传奇
(2)我希望“linetype”传奇的背景为白色所以我用
linetype = guide_legend(override.aes = list(fill = "white"))
但背景仍显示白色。