繁体   English   中英

ggplot2-订购带有多个数据帧的图例

[英]ggplot2 - ordering legend with multiple data frames

我有一个带有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

在此处输入图片说明

可以看到,图例的顺序为“联系,全部,一半,视频” –逻辑顺序为:“全部,一半,联系,视频”。 我该怎么做? 我已经看到了在数据帧上的组变量中使用因子顺序的示例,但是由于我是从三个数据帧中提取的,因此在这里看不到如何使用它。

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

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

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

您的数据现在看起来像:

> dat
         date cust group    type line point
1  2016-10-01    1     1    full    1    NA
2  2016-12-13    1     1    full    1    NA
3  2017-01-01    2     2    full    1    NA
4  2017-02-15    2     2    full    1    NA
5  2016-10-01    3     3    full    1    NA
6  2017-02-15    3     3    full    1    NA
7  2016-10-01    4     4    half    1    NA
8  2016-11-13    4     4    half    1    NA
9  2016-10-01    5     5    half    1    NA
10 2017-02-15    5     5    half    1    NA
11 2016-10-01    6     6    half    1    NA
12 2017-02-01    6     6    half    1    NA
13 2016-12-01    1    11   video   NA     2
14 2016-11-13    3    13   video   NA     2
15 2016-12-01    3    13   video   NA     2
16 2016-11-02    4    14   video   NA     2
17 2016-11-02    6    16   video   NA     2
18 2016-11-03    6    16   video   NA     2
19 2016-11-01    4    24 contact   NA     1
20 2016-10-13    3    23 contact   NA     1
21 2017-01-01    2    22 contact   NA     1
22 2016-12-02    1    21 contact   NA     1
23 2016-11-04    6    26 contact   NA     1
24 2016-11-03    6    26 contact   NA     1
25 2016-11-05    6    26 contact   NA     1

暂无
暂无

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

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