ggplot2:自定义行类型但收到错误消息

时间:2017-09-21 02:07:56

标签: r ggplot2 line

我想根据我的变量GraphIndex更改线型,但是我收到错误。对于每个标签:“Farm 3 Shaded”,“Farm 3 Unhaded”,“Farm 4 East 3”,“Farm 4 Spring Inside 1”,“Farm 4 Gothic”,“Farm 4 Spring Outside 1”,“Farm 4 West 3” “,我想要一个不同的线型。

以下是我的一些数据:

test <- structure(list(sensorheight = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 
16L, 16L, 16L, 16L, 16L, 16L, 16L), date = structure(c(1484985600, 
1484985600, 1484985600, 1484985600, 1484985600, 1484985600, 1484985600, 
1484985600, 1484985600, 1485072000, 1485072000, 1485072000, 1485072000, 
1485072000, 1485072000, 1485072000, 1485072000, 1485072000, 1485158400, 
1485158400, 1485158400, 1485158400, 1485158400, 1485158400, 1485158400, 
1485158400, 1484985600, 1484985600, 1484985600, 1484985600, 1484985600, 
1484985600, 1485072000, 1485072000, 1485072000, 1485072000, 1485072000, 
1485072000, 1485158400, 1485158400, 1485158400, 1485158400, 1485158400, 
1485158400), class = c("POSIXct", "POSIXt"), tzone = ""), GraphIndex = structure(c(6L, 
11L, 14L, 9L, 24L, 27L, 30L, 35L, 34L, 5L, 8L, 13L, 16L, 19L, 
26L, 29L, 32L, 33L, 7L, 12L, 15L, 10L, 25L, 28L, 31L, 36L, 7L, 
12L, 15L, 10L, 19L, 26L, 5L, 8L, 13L, 16L, 3L, 24L, 6L, 11L, 
14L, 9L, 4L, 25L), .Label = c("Farm 1 HT", "Farm 1 HT", "Farm 1 Mid-T", 
"Farm 1 Mid-T", "Farm 2 Non-vented", "Farm 2 Non-vented", "Farm 3 Caterpillar", 
"Farm 3 Open", "Farm 3 Shaded", "Farm 3 Unshaded", "Farm 3 Open Shaded", 
"Farm 3 Open", "Farm 3 Shaded", "Farm 3 Unshaded", "Farm 3 Caterpillar", 
"Farm 3 Open Shaded", "Main Station Shaded", "Main Station Shaded", 
"Farm 4 East 3", "Farm 4 Spring Inside 1", "Farm 4 Gothic", "Farm 4 Spring Outside 1", 
"Farm 4 Gothic", "Farm 4 West 3", "Farm 4 Gothic", "Farm 4 Gothic", 
"School 1 Caterpillar", "School 1 Caterpillar", "School 1 Standard", 
"School 1 Standard", "School 2 Standard", "School 2 Standard", 
"School 4 Standard", "School 4 Standard", "School 3 Standard", 
"School 3 Standard"), class = "factor"), TempC = c(-3.37992831541219, 
-3.35282258064516, -3.8456541218638, -2.94623655913979, -4.12253584229391, 
-2.65837813620072, -3.18906810035842, -3.46415770609319, -3.18458781362007, 
-2.41151433691756, -2.85483870967742, -2.9758064516129, -3.27352150537634, 
-3.48543906810036, -3.19623655913979, -1.19847670250896, -1.65770609318996, 
-1.78360215053763, -0.35203853046595, -2.45094086021505, -2.77620967741936, 
-0.96975806451613, -1.4923835125448, -0.681003584229391, -3.56093189964158, 
-3.5905017921147, -0.738351254480288, -4.04525089605735, -3.99775985663082, 
-1.88709677419355, -3.90524193548387, -3.77060931899642, -2.11491935483871, 
-2.88922491039427, -3.16151433691756, -3.29726702508961, -2.62914426523298, 
-3.22087813620072, -2.42338709677419, -2.02576164874552, -2.65613799283154, 
-1.73454301075269, -3.11559139784946, -0.377240143369176), sd = c(NA, 
NA, NA, NA, NA, NA, NA, NA, NA, 0.240771126210473, 0.154283603242764, 
NA, NA, NA, 0.0826858736064916, NA, NA, NA, 0.295736639891417, 
NA, NA, NA, 0.124504016579889, NA, NA, NA, 0.227782157559645, 
NA, NA, NA, NA, 0.28512370209135, 0.481542252420946, 0.177568705580224, 
NA, NA, 0.319496948399029, NA, NA, NA, NA, NA, 0.24710720847917, 
0.650398844881712), se = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, 
0.170250896057348, 0.109094982078853, NA, NA, NA, 0.058467741935484, 
NA, NA, NA, 0.209117383512545, NA, NA, NA, 0.088037634408602, 
NA, NA, NA, 0.161066308243728, NA, NA, NA, NA, 0.201612903225806, 
0.340501792114695, 0.125560035842294, NA, NA, 0.225918458781362, 
NA, NA, NA, NA, NA, 0.174731182795699, 0.459901433691756), ci = c(NA, 
NA, NA, NA, NA, NA, NA, NA, NA, 2.16324274182186, 1.38618317798322, 
NA, NA, NA, 0.742903099494086, NA, NA, NA, 2.65708828880356, 
NA, NA, NA, 1.11862420728419, NA, NA, NA, 2.04654148864463, NA, 
NA, NA, NA, 2.56173482584167, 4.32648548364371, 1.59539152209362, 
NA, NA, 2.87056619095703, NA, NA, NA, NA, NA, 2.22017018239611, 
5.84360177494772)), .Names = c("sensorheight", "date", "GraphIndex", 
"TempC", "sd", "se", "ci"), row.names = c(NA, -44L), class = c("tbl_df", 
"tbl", "data.frame"))

我能够为颜色和形状编写类似的代码,这是我的图形代码:

ggplot(subset(test, sensorheight == "1" & GraphIndex %in% c("Farm 3 Shaded", "Farm 3 Unshaded",
                                                                     "Farm 4 East 3", "Farm 4 Spring Inside 1", 
                                                                     "Farm 4 Gothic", "Farm 4 Spring Outside 1", 
                                                                     "Farm 4 West 3")),
       aes(x=date,y=TempC,colour=GraphIndex, group=GraphIndex)) + 
  geom_ribbon(aes(ymin=TempC-sd, ymax=TempC+sd, linetype = NA, fill = GraphIndex), alpha = 0.2) +
  geom_line(aes(linetype=GraphIndex)) + 
  geom_point(aes(shape=GraphIndex),size=1) +
  scale_x_datetime(name = " ", date_breaks = "1 month", date_labels = "%B", limits = as.POSIXct(c("2016-05-01","2017-06-01"))) +
  scale_y_continuous(name = expression("Farm 3"), limits = c(-10,20)) +
  scale_linetype_manual(name = "Type",labels = c("Caterpillar", "Quonset Large","Quonset Small", "Gothic Large", "Gothic Small", "Mid-Tunnel","Outside"), 
                        breaks = c("Farm 3 Shaded", "Farm 3 Unshaded",
                                   "Farm 4 East 3", "Farm 4 Spring Inside 1", 
                                   "Farm 4 Gothic", "Farm 4 Spring Outside 1", 
                                   "Farm 4 West 3"),
                        values = c("twodash","dashed","dotted","dotdash","longdash","twodash","solid"))

以下是我遇到的错误:

Error in grid.Call.graphics(L_polygon, x$x, x$y, index) : 
  invalid line type

1 个答案:

答案 0 :(得分:1)

geom_ribbon和geom_line的线型aes与分配的scale_linetype_manual之间存在分歧。

也可以为geom_ribbon设置linetype = GraphIndex,或者从aes中删除它,并将其设置为linetype = 0。

解决方案(1)在aes

中设置geom_ribbon linetype = GraphIndex
ggplot(subset(test, sensorheight == "1" & GraphIndex %in% c("Farm 3 Shaded", "Farm 3 Unshaded",
                                                                 "Farm 4 East 3", "Farm 4 Spring Inside 1", 
                                                                 "Farm 4 Gothic", "Farm 4 Spring Outside 1", 
                                                                 "Farm 4 West 3")),
       aes(x=date,y=TempC,colour=GraphIndex, group=GraphIndex)) + 
  geom_ribbon(aes(ymin=TempC-sd, ymax=TempC+sd, linetype = GraphIndex, fill = GraphIndex), alpha = 0.2) +
  geom_line(aes(linetype=GraphIndex)) + 
  geom_point(aes(shape=GraphIndex),size=1) +
  scale_x_datetime(name = " ", date_breaks = "1 month", date_labels = "%B", 
limits = as.POSIXct(c("2016-05-01","2017-06-01"))) +
  scale_y_continuous(name = expression("Farm 3"), limits = c(-10,20)) +
  scale_linetype_manual(name = "Type",labels = c("Caterpillar", "Quonset Large","Quonset Small", "Gothic Large", "Gothic Small", "Mid-Tunnel","Outside"), 
                    breaks = c("Farm 3 Shaded", "Farm 3 Unshaded",
                               "Farm 4 East 3", "Farm 4 Spring Inside 1", 
                               "Farm 4 Gothic", "Farm 4 Spring Outside 1", 
                               "Farm 4 West 3"),
                    values = c("twodash","dashed","dotted","dotdash","longdash","twodash","solid"))

解决方案(2)在aes

之外设置geom_ribbon linetype = 0(空白)
ggplot(subset(test, sensorheight == "1" & GraphIndex %in% c("Farm 3 Shaded", "Farm 3 Unshaded",
                                                                 "Farm 4 East 3", "Farm 4 Spring Inside 1", 
                                                                 "Farm 4 Gothic", "Farm 4 Spring Outside 1", 
                                                                 "Farm 4 West 3")),
       aes(x=date,y=TempC,colour=GraphIndex, group=GraphIndex)) + 
  geom_ribbon(aes(ymin=TempC-sd, ymax=TempC+sd, fill = GraphIndex), alpha = 0.2, linetype = 0) +
  geom_line(aes(linetype=GraphIndex)) + 
  geom_point(aes(shape=GraphIndex),size=1) +
  scale_x_datetime(name = " ", date_breaks = "1 month", date_labels = "%B", 
limits = as.POSIXct(c("2016-05-01","2017-06-01"))) +
  scale_y_continuous(name = expression("Farm 3"), limits = c(-10,20)) +
  scale_linetype_manual(name = "Type",labels = c("Caterpillar", "Quonset Large","Quonset Small", "Gothic Large", "Gothic Small", "Mid-Tunnel","Outside"), 
                    breaks = c("Farm 3 Shaded", "Farm 3 Unshaded",
                               "Farm 4 East 3", "Farm 4 Spring Inside 1", 
                               "Farm 4 Gothic", "Farm 4 Spring Outside 1", 
                               "Farm 4 West 3"),
                    values = c("twodash","dashed","dotted","dotdash","longdash","twodash","solid"))