【问题标题】:How can I make multiplot in R如何在 R 中制作多图
【发布时间】:2020-12-09 06:19:08
【问题描述】:

我有这个数据集,我想制作三个 y 轴,其中“国家”将是 X 轴。在 y 轴上,我想做两个聚集列,一个折线图重叠。 我是新用户,学习16天

dput(data)

structure(list(Country = c("China", "Indonesia", "Vietnam", "Thailand", 
"Egypt", "India", "Turkey", "Brazil", "United States", "Russia"
), Plastic.Consumption = c(44.14201935, 12.87658986, 23.6336878, 
52.92058216, 13.93164324, 6.994354455, 64.77526757, 31.53076177, 
87.30070657, 32.01972449), Plastic.Production.Kt. = c(42421L, 
2258L, 387L, 5881L, 411L, 7211L, 939L, 5860L, 33985L, 3487L), 
    Plastic.Waste.Mismanagemet..kt. = c(8819.716784, 3216.855605, 
    1833.819141, 1027.739178, 967.0123574, 599.8191155, 485.937142, 
    471.4039969, 275.4244968, 80.75948103)), class = "data.frame", 
row.names = c(NA, -10L))

【问题讨论】:

    标签: r ggplot2 lattice


    【解决方案1】:

    欢迎来到 SO。这是一个资源,它演示了为什么两个 Y 轴是一个坏主意link。以下是其他选项的一些示例。

    
    library(tidyverse)
    
    df <- structure(list(Country = c("China", "Indonesia", "Vietnam", "Thailand", 
                               "Egypt", "India", "Turkey", "Brazil", "United States", "Russia"
    ), Plastic.Consumption = c(44.14201935, 12.87658986, 23.6336878, 
                               52.92058216, 13.93164324, 6.994354455, 64.77526757, 31.53076177, 
                               87.30070657, 32.01972449), Plastic.Production.Kt. = c(42421L, 
                                                                                     2258L, 387L, 5881L, 411L, 7211L, 939L, 5860L, 33985L, 3487L), 
    Plastic.Waste.Mismanagemet..kt. = c(8819.716784, 3216.855605, 
                                        1833.819141, 1027.739178, 967.0123574, 599.8191155, 485.937142, 
                                        471.4039969, 275.4244968, 80.75948103)), class = "data.frame", 
    row.names = c(NA, -10L))
    
    ggplot(df,aes(Country,Plastic.Consumption,size = Plastic.Waste.Mismanagemet..kt.,color = Plastic.Waste.Mismanagemet..kt.)) + 
      geom_point()
    

    
    
    df %>% 
      pivot_longer(2:4) %>% 
      ggplot(aes(Country,value)) + 
      geom_point() + 
      coord_flip() + 
      facet_wrap(~name,scales = 'free')
    

    reprex package (v0.3.0) 于 2020 年 12 月 9 日创建

    祝你好运!

    【讨论】:

      【解决方案2】:

      尽管有关于使用辅助轴的警告,但您可以通过以下方式从您共享的数据中创建您所描述的图:

      library(ggplot2)
      library(tidyr)
      library(dplyr)
      
      data %>% 
        mutate(Country = forcats::fct_reorder(Country, -Plastic.Production.Kt.)) %>%
        pivot_longer(3:4) %>% 
        ggplot(aes(Country, value, fill = name)) +
        geom_col(position = position_dodge()) +
        geom_line(aes(y = Plastic.Consumption * 400, group = 1, 
                      color = "Consumption"), size = 1) +
        scale_color_manual(values = "black", name = "") +
        scale_fill_manual(values = c("orange", "deepskyblue4"), name = "",
                          labels = c("Plastic production", 
                                     "Plastic waste mismanagement")) +
        scale_y_continuous(labels = scales::comma, name = "kilotonnes per year",
                           sec.axis = sec_axis(trans = ~ .x/400, 
                                               name = "Consumption (kilotonnes)")) +
        theme_bw() +
        theme(legend.position = "top")
      

      【讨论】:

      • 先生,非常感谢您的帮助,我很好奇您为什么将塑料消耗量乘以 400? (geom_line(aes(y = Plastic.Consumption * 400, group = 1) 如果我想在列中进行生产和消费并在线图中进行管理不善,我可以通过替换变量来使用该代码?
      • @Kazi 如果您想在单个图中显示变量,其中数字处于不同的比例,您需要将其中一个变量乘以某个常数,使其与其他人,但随后创建第二个轴,其数字乘以同一常数的倒数。在您的示例中,这允许您从左轴读取条,但从右轴读取线。你不能只是交换变量,因为尺度是如此不同。您的生产数量大约是消耗量的 500 倍,因此无法将条形显示在一起
      猜你喜欢
      • 1970-01-01
      • 2020-05-07
      • 2021-11-09
      • 2019-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多