首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ggplot2 -对具有多个数据帧的图例进行排序

ggplot2 -对具有多个数据帧的图例进行排序
EN

Stack Overflow用户
提问于 2017-02-11 05:19:10
回答 1查看 716关注 0票数 0

我有一个带有一个geom_line图和两个geom_point图的图。geom_line有一个组变量。把所有这些都放在一个图例中,我得不到一个合理的顺序。

代码语言:javascript
复制
    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

可以看出,图例的顺序是“联系人、完整、半个、视频”--逻辑顺序是:“完整、半个、联系人、视频”。我如何才能做到这一点呢?我见过在数据帧上使用组变量中的因子顺序的示例,但由于我是从三个数据帧中提取的,所以我不知道如何在这里使用它。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-11 05:57:03

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

代码语言:javascript
复制
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

您的数据现在如下所示:

代码语言:javascript
复制
> 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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42168824

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档