0
我有一個帶有geom_line圖和2個geom_point圖的圖。 geom_line有一個組變量。把所有東西都彙集在一起,我無法得到一個合理的順序。ggplot2 - 用多個數據幀排序圖例
library(ggplot2)
date.full <- as.Date(c("2016-10-1", "2016-12-13",
"2017-1-1", "2017-2-15",
"2016-10-1", "2017-2-15"))
cust.full <- c(1,1, 2,2, 3,3)
##Half Season
date.half <- as.Date(c("2016-10-1", "2016-11-13",
"2016-10-1", "2017-2-15",
"2016-10-1", "2017-2-1"))
cust.half <- c(4,4,5,5,6,6)
##Contacts
contact.date <- as.Date(c("2016-11-1", "2016-10-13", "2017-1-1", "2016-12-2",
"2016-11-4", "2016-11-3", "2016-11-5"))
contact.cust <- c(4,3,2,1, 6, 6, 6)
video.date <- as.Date(c("2016-12-1", "2016-11-13","2016-12-1", "2016-11-2",
"2016-11-2", "2016-11-3"))
video.cust <- c(1,3,3,4,6, 6)
life.span <- data.frame(date.full,cust.full, date.half, cust.half)
video.events <- data.frame(video.date, video.cust)
contact.events <- data.frame(contact.date, contact.cust)
##Create graph
p <- ggplot(life.span) +
geom_line(aes(x= date.full, y=cust.full, group=cust.full, colour = "Full"))+
geom_line(aes(x= date.half, y=cust.half, group=cust.half, colour = "Half")) +
geom_point(data=contact.events, aes(x=contact.date, y=contact.cust, colour = "Contact")) +
geom_point(data=video.events,aes(x=video.date, y=video.cust, colour="Video"), shape=2) +
xlab('Date') + ylab('Customer') +
ggtitle('Illustrative Hypothetical Customers') +
scale_colour_discrete("Previous")+
guides(shape = FALSE,
colour = guide_legend(override.aes = list(shape = c(16, NA, NA, 17),
linetype = c("blank","solid","solid", "blank"),
labels= c("a","b","c","d")
)))
p
可以看出傳說是「聯繫,全,半視頻」的順序 - 一個邏輯順序將是:「全,半,聯繫人,視頻」。我怎樣才能做到這一點?我已經在數據框中看到了使用組變量中因子順序的示例,但由於我從三個數據框中提取數據,因此我不知道在這裏如何使用它。
使用override.aes至少可以在正確的組件上獲得正確的符號,所以這是進步。
謝謝。 linetype = line是我不知道的想法。很多要學習。 –