ggplot2 - 使用多个数据框排序图例

时间:2017-02-10 21:19:10

标签: r ggplot2

我有一张带有geom_line图和2个geom_point图的图表。 geom_line有一个组变量。把它们放在一个传奇中我无法得到合乎逻辑的顺序。

    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

enter image description here

可以看出,传说是&#34;联系人,全部,一半,视频&#34; - 逻辑顺序为:&#34; Full,Half,Contact,Video&#34;。我怎么能做到这一点?我已经看到在数据帧上使用组变量中的因子顺序的示例,但由于我从三个数据帧中提取,我在这里看不到如何使用它。

使用override.aes至少会在正确的组件上获得正确的符号,这就是进步。

1 个答案:

答案 0 :(得分:1)

通过组合数据,您可以使用自定义有序因子,并且代码更少:

(2,1)

您的数据现在看起来像:

line1 <- data.frame(date = date.full, cust = cust.full, group=cust.full, type = 'full', line = 1, point = NA)
line2 <- data.frame(date = date.half, cust = cust.half, group=cust.half, type = 'half', line = 1, point = NA)
lines <- rbind(line1,line2)

points1 <- data.frame(date = video.date, cust=video.cust, group=video.cust+10, type = 'video', line = NA, point = 2)
points2 <- data.frame(date = contact.date, cust=contact.cust, group=contact.cust+20, type='contact', line = NA, point = 1)
points <- rbind(points1, points2)
dat <- rbind(lines, points)
dat$type <- factor(dat$type, levels = c("full", "half", "contact", "video"))


p <- ggplot(dat, aes(x = date, y = cust, group = group, colour = type, linetype=factor(line), shape=factor(point))) +
  geom_line() +
  geom_point() +

  guides(shape = FALSE, linetype = FALSE,
         colour = guide_legend(override.aes =  list(shape = c(NA, NA, 16,  17),
                                                   linetype = c("solid","solid","blank", "blank"),
                                                   labels= c("a","b","c","d")
         )))
p