ggplot2:创建包含多个符号,线型和颜色的图例

时间:2019-04-30 16:26:06

标签: r ggplot2

我正在尝试创建一个函数,该函数将绘制测得的流值(作为未连接的点)以及建模值的平滑曲线。我希望能够为每种类型的流创建独特的形状(15分钟对24小时),并且对于两种模型流也具有独特的颜色和线条类型。

我已经能够为测量值创建不同的颜色,并且可以在图例中正确显示这些颜色,但是当我尝试更改形状时,就会遇到麻烦。尝试匹配曲线中的虚线和实线时也是如此。我试过将geom_point()+更改为geom_point(shape = variable)+ 但这会产生此错误: 图层错误(数据=数据,映射=映射,统计=统计,geom = GeomLine 、:找不到对象“变量”

ggplot(data = r1,aes(x=ReturnPeriod, y=value, color=variable)) +
  geom_point()+
  geom_line(data = r2,aes(x=ReturnPeriod, y=value))+
  ylab("Max Yearly Flow (cfs)") +
  xlab("Return Interval (years)") +
  scale_shape_manual(name = "Legend", 
                      labels = c("Obs 15-min", "Obs 24-hour", "15-min P3", "24-hr P3"),
                      values = c("Peak_cfs"=16, "Daily_cfs"=17, "PeakEst"=NA, 
                                 "DailyEst" = NA)) +
  scale_colour_manual(name = "Legend", 
                      labels = c("Obs 15-min", "Obs 24-hour", "15-min P3", "24-hr P3"),
                      values = c("Peak_cfs"="grey", "Daily_cfs"="black", "PeakEst"="dark grey", 
                                 "DailyEst" = "black")) +
  scale_linetype_manual(name = "Legend", 
                        labels = c("Obs 15-min", "Obs 24-hour", "15-min P3", "24-hr P3"),
                        values = c("Peak_cfs"="blank", "Daily_cfs"="blank", "PeakEst"="solid", 
                                   "DailyEst" = "dashed"))+
  guides(colour = guide_legend(override.aes = list(
    linetype = c("blank", "blank", "solid", "dashed"),
    shape = c(16,17,NA,NA),
    color = c("grey","black", "dark grey", "black")))) +
  theme(legend.position=c(0.92, 0.2),  
        legend.background = element_rect(fill="white"),
        legend.key = element_blank(),
        legend.box = "horizontal")

我的数据是:

r1 = read.table(
  header = TRUE,
  stringsAsFactors = FALSE,
  text = 
"ReturnPeriod  variable value
1      1.090909 Daily_cfs  14.3
2      1.200000 Daily_cfs  52.3
3      1.333333 Daily_cfs  66.6
4      1.500000 Daily_cfs  93.3
5      1.714286 Daily_cfs  94.3
6      2.000000 Daily_cfs 103.0
7      2.400000 Daily_cfs 107.0
8      3.000000 Daily_cfs 119.0
9      4.000000 Daily_cfs 123.0
10     6.000000 Daily_cfs 166.0
11    12.000000 Daily_cfs 207.0
12     1.090909  Peak_cfs  29.0
13     1.200000  Peak_cfs 159.0
14     1.333333  Peak_cfs 241.0
15     1.500000  Peak_cfs 255.0
16     1.714286  Peak_cfs 296.0
17     2.000000  Peak_cfs 320.0
18     2.400000  Peak_cfs 339.0
19     3.000000  Peak_cfs 351.0
20     4.000000  Peak_cfs 512.0
21     6.000000  Peak_cfs 737.0
22    12.000000  Peak_cfs 800.0"
)

r2 <- read.table(
  header = TRUE,
  stringsAsFactors = FALSE,
  text = 
    "ReturnPeriod variable      value
1      1.010101 DailyEst   8.185016
2      2.000000 DailyEst 104.434396
3      4.000000 DailyEst 148.084828
4     10.000000 DailyEst 180.992656
5     25.000000 DailyEst 199.429333
6     50.000000 DailyEst 207.967918
7      1.010101  PeakEst  14.846387
8      2.000000  PeakEst 349.352462
9      4.000000  PeakEst 547.379809
10    10.000000  PeakEst 713.827969
11    25.000000  PeakEst 815.243158
12    50.000000  PeakEst 865.063562"
)

我希望在图表中获得24小时的Obs三角形符号,而不是得到圆圈,并且在图例和图表中,我都试图将实心黑线变为虚线黑线。

当前代码生成的图形:

img

2 个答案:

答案 0 :(得分:0)

您需要通过包含geom_point(aes(shape = factor(variable)))来告诉ggplot如何对形状进行分组

然后,您需要在scale_shape_manual(values = c(19, 17))中进行值调用,以告诉ggplot使用哪些符号(在本例中为实心圆和实心三角形)。应该是values的当前列表。

类似地,对于geom_line调用和这两种线型,您需要包括一个组因子和线型值。

ggplot2 pages

上看到一些很好的提示

答案 1 :(得分:0)

如果将r1r2合并到r3中进行绘图并将shape + linetype添加到aes中,它将起作用

library(ggplot2)

r1$variable <- factor(r1$variable)
r2$variable <- factor(r2$variable)
r3 <- rbind(r1, r2)

ggplot(data = r3, aes(x=ReturnPeriod, y=value, color=variable, shape=variable)) +
  geom_point()+
  geom_line(aes(linetype=variable))+
  ylab("Max Yearly Flow (cfs)") +
  xlab("Return Interval (years)") +
  scale_shape_manual(name = "Legend", 
                     labels = c("Obs 15-min", "Obs 24-hour", "15-min P3", "24-hr P3"),
                     values = c("Peak_cfs"=16, "Daily_cfs"=17, "PeakEst"=NA, 
                                "DailyEst" = NA)) +
  scale_colour_manual(name = "Legend", 
                      labels = c("Obs 15-min", "Obs 24-hour", "15-min P3", "24-hr P3"),
                      values = c("Peak_cfs"="grey", "Daily_cfs"="black", "PeakEst"="dark grey", 
                                 "DailyEst" = "black")) +
  scale_linetype_manual(name = "Legend",
                        labels = c("Obs 15-min", "Obs 24-hour", "15-min P3", "24-hr P3"),
                        values = c("Peak_cfs"="blank", "Daily_cfs"="blank", "PeakEst"="solid",
                                   "DailyEst" = "dashed"))+
  guides(colour = guide_legend(override.aes = list(
    linetype = c("blank", "blank", "solid", "dashed"),
    shape = c(16,17,NA,NA),
    color = c("grey","black", "dark grey", "black")))) +
  theme(legend.position=c(0.8, 0.6),  
    legend.background = element_rect(fill="white"),
    legend.key = element_blank(),
    legend.box = "horizontal")
#> Warning: Removed 12 rows containing missing values (geom_point).

enter image description here