【问题标题】:Is there a way in ggplot to add multiple legends?ggplot 中有没有办法添加多个图例?
【发布时间】:2021-05-11 22:16:05
【问题描述】:

我有数据框(d):

     d<- structure(list(Total.VolumeD = c(1118.4, 638.4, 1180.8, 1166.4, 
1296, 936, 1324.8, 936, 307.2, 1152, 691.2, 585.6, 648, 979.2, 
374.4, 1358.4, 676.8, 777.6, 1344, 1286.4, 825.6, 849.6, 1056, 
1785.6, 868.8, 1579.2, 283.2, 1132.8, 820.8, 1291.2, 1833.6, 
614.4), Speed_KM = c(27.353, 109.412, 109.412, 78.841, 109.412, 
75.623, 111.021, 114.239, 106.194, 107.803, 112.63, 109.412, 
114.239, 112.63, 90.104, 45.052, 107.803, 109.412, 111.021, 111.021, 
109.412, 94.931, 109.412, 64.36, 43.443, 104.585, 106.194, 106.194, 
104.585, 45.052, 82.059, 102.976), Precipitation = structure(c(1L, 
1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L), .Label = c("0", 
"0-50", "50-100", "100-200", ">200"), class = "factor")), row.names = c(NA, 
-32L), class = "data.frame")

我从以下两个数据集中添加了两个 geom_path:

no_d<- structure(list(q = c(1572.837685, 1310.733335, 1464.593355, 1806.375156, 
1852.930416, 1844.816783, 1723.916843, 1215.776704, 1292.854462
), u = c(55L, 36L, 46L, 107L, 102L, 104L, 109L, 31L, 35L)), row.names = c(NA, 
-9L), class = "data.frame")

with_d<- structure(list(q = c(1358.242525, 1334.898171, 1640.129166, 1648.496715, 
1558.353432, 565.2914834, 1472.853017, 1303.752995, 1326.376898
), u = c(51L, 107L, 95L, 99L, 106L, 11L, 64L, 46L, 48L)), row.names = c(NA, 
-9L), class = "data.frame")

我使用下面的代码来绘制我真正想要的东西。它工作得很好。但是,我需要有降水图例和新两条线(geom_path 线)的另一个图例。

ggplot(d) +
  geom_point(aes( x = Total.VolumeD,y = Speed_KM, width = 0.5,shape=Precipitation, color=Precipitation, 
                  size = Precipitation))+
  scale_colour_manual(values = c("lightgrey", "black", "black", "black", "black"))+
  geom_path(data = no_d, aes(q, u),  size = 1, linetype="solid")+
  geom_path(data = with_d, aes(q, u), size = 1, linetype="dashed")+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        panel.border = element_rect(colour = "black", fill=NA, size=1))+
  scale_x_continuous(limits = c(0,2300))+
  scale_y_continuous(limits = c(20,125))+
  labs(shape = "precipitation(mm/h)")

【问题讨论】:

    标签: r ggplot2 tidyverse


    【解决方案1】:

    这行得通,

    ggplot(d) +
        geom_point(aes( x = Total.VolumeD,y = Speed_KM, width = 0.5,shape=Precipitation, color=Precipitation, 
                        size = Precipitation))+
        scale_colour_manual(values = c("lightgrey", "black", "black", "black", "black"))+
        # add linetype in aes
        geom_path(data = no_d, aes(q, u, linetype="solid"),  size = 1)+
        geom_path(data = with_d, aes(q, u,linetype="dashed"), size = 1)+
        theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
              panel.background = element_blank(),
              panel.border = element_rect(colour = "black", fill=NA, size=1))+
        scale_x_continuous(limits = c(0,2300))+
        scale_y_continuous(limits = c(20,125))+
        labs(shape = "precipitation(mm/h)") +
        # add this
        scale_linetype_discrete(name = "Y series", labels = c("solid", "dashed"))
    

    更新

    ggplot(d) +
        geom_point(aes(Total.VolumeD, Speed_KM, width = 0.5, shape = Precipitation, 
                       color = Precipitation, size = Precipitation))+
        scale_colour_manual(values = c("lightgrey", "black", "black", "black", "black"), guide = "none")+
        geom_path(data = no_d, aes(q, u, linetype = "solid"),  size = 1)+
        geom_path(data = with_d, aes(q, u,linetype = "dashed"), size = 1)+
        theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
              panel.background = element_blank(),
              panel.border = element_rect(colour = "black", fill=NA, size=1))+
        scale_x_continuous(limits = c(0,2300))+
        scale_y_continuous(limits = c(20,125))+
        labs(shape = "precipitation(mm/h)") +
        scale_linetype_discrete(name = "Y series", labels = c("solid", "dashed")) +
        scale_size_discrete(guide = "none")
    

    • scale_colour_manual 中添加guide = 'none'
    • 添加scale_size_discrete(guide = "none")

    您正在获取图例,因为您在aes 中提供了coloursize
    ggplot 将返回aesthetic 中给出的每个图例参数。

    【讨论】:

    • 我想删除第一个图例。我只想要最后两个!我真的不明白为什么我得到第一个?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-04
    • 1970-01-01
    • 2018-08-14
    • 2023-02-15
    • 2020-05-15
    • 2018-04-21
    • 1970-01-01
    相关资源
    最近更新 更多