Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot2-使用多个数据帧排序图例_R_Ggplot2 - Fatal编程技术网

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

R ggplot2-使用多个数据帧排序图例,r,ggplot2,R,Ggplot2,我有一个图形,有一个几何线图和两个几何点图。geom_线有一个组变量。把所有的东西放在一个图例中,我无法得到一个合乎逻辑的顺序 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 &l

我有一个图形,有一个几何线图和两个几何点图。geom_线有一个组变量。把所有的东西放在一个图例中,我无法得到一个合乎逻辑的顺序

    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
库(ggplot2)

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

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