【问题标题】:How to plot the distribution of two variable(meter & height) for two different scenario?如何为两个不同的场景绘制两个变量(米和高度)的分布?
【发布时间】:2020-01-02 15:29:57
【问题描述】:

我有两个代表两种场景的数据框。每个数据框有 3 个变量:面积、高度和场景名称(场景 1 和场景 2)。

我想要什么:

  1. 我想显示我在每个高度有多少合适的面积 两种情况。
  2. 我想在每个高度的绘图内都有一个标签(例如在 直方图条的顶部),它告诉我区域的确切尺寸。
  3. 这两个场景应该在同一个图中可见(例如 重叠)

场景1:

structure(list(height = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
Suitable_Area = c(20462L, 
        21952L, 23069L, 20184L, 18836L, 18141L, 17988L, 17732L, 17227L, 
        17184L), ScenarioTyp = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 1L, 1L), .Label = "scenario1", class = "factor")), class = "data.frame", row.names = c("1","2", "3", "4", "5", "6", "7", "8",
"9", "10"))

场景2:

structure(list(height = c(1, 2, 3, 4, 5, 6, 7, 8, 9), Suitable_Area =
c(20462L, 
     20462L, 20457L, 16826L, 14847L, 13505L, 12726L, 11821L, 10853L
     ), ScenarioTyp = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
     1L), .Label = "scenario2", class = "factor")), class = "data.frame", row.names = c("1", 
     "2", "3", "4", "5", "6", "7", "8", "9"))

我已经尝试了几个情节(使用 ggplot2),它们向我展示了合适的区域,但没有提供我想要的清晰准确的信息(请参阅上面提到的第 1-2 点)

ggplot(Scenario1,aes(x=height,y=Suitable_Area))+
 geom_area(alpha=0.8,fill="lightblue")+
 geom_area(data= Scenario2,aes(y=Suitable_Area),fill="lightgrey",alpha=0.9)

和,

ggplot(Scenario2 , aes(x= height,y= Suitable_Area,colour="Scenario2"))+ 
  geom_line()+ 
  geom_line(data = Scenario1, aes(x= height,y= Suitable_Area,colour="Scenario1"))

我可以想象两个半透明的重叠 直方图(宽度为 1,对应于 1 米的高度)可以完成这项工作,但我无法将合适的区域作为 Y 轴通常是密度或计数。

我也考虑合并两个数据框的想法,但它并没有真正帮助我。

这可能吗?还是 ggplot2 的图表中有很多东西?

提前谢谢你。

【问题讨论】:

    标签: r ggplot2 histogram distribution data-representation


    【解决方案1】:

    我不确定这是否是您所追求的,但分组条形图是否可行?

    library(tidyverse)
    s1 = structure(list(height = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
                        Suitable_Area = c(20462L, 
                                          21952L, 23069L, 20184L, 18836L, 18141L, 17988L, 17732L, 17227L, 
                                          17184L), ScenarioTyp = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                                             1L, 1L, 1L), .Label = "scenario1", class = "factor")), class = "data.frame", row.names = c("1","2", "3", "4", "5", "6", "7", "8",
                                                                                                                                                                        "9", "10"))
    s2 = structure(list(height = c(1, 2, 3, 4, 5, 6, 7, 8, 9), Suitable_Area =
                     c(20462L, 
                       20462L, 20457L, 16826L, 14847L, 13505L, 12726L, 11821L, 10853L
                     ), ScenarioTyp = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                  1L), .Label = "scenario2", class = "factor")), class = "data.frame", row.names = c("1", 
                                                                                                                                     "2", "3", "4", "5", "6", "7", "8", "9"))
    
    s3 =  bind_rows(list(s1,s2)) %>%
      as_tibble()
    
    s3 %>%
      ggplot(aes(x = height, y = Suitable_Area, fill = ScenarioTyp)) +
        geom_bar(position = position_dodge(0.8) , stat = "identity", width = 0.8) +
        geom_text(aes(y = Suitable_Area + 1200, label = Suitable_Area), position = position_dodge(0.8), size = 4, angle = 90) +
        scale_x_continuous(breaks = 1:10) +
        theme_classic()
    

    如果您想要带有类似标签的区域,可能会起作用:

    s3 %>%
      ggplot(aes(x = height, y =Suitable_Area, fill = ScenarioTyp)) +
        geom_area(alpha = 0.5, position = "identity") +
        geom_label(aes(y = Suitable_Area, label = Suitable_Area, color = ScenarioTyp), size = 3, fill = "white") +
        scale_x_continuous(breaks = 1:10) +
        theme_classic()
    

    【讨论】:

    • 第一个情节更让我信服。实际上,我想要更多的第二个情节,每个场景只有一个标签,其总适合面积。非常感谢,我会保存代码以备将来使用!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-07
    • 1970-01-01
    • 2023-02-23
    • 1970-01-01
    • 2013-04-14
    相关资源
    最近更新 更多