如何为两种不同的场景绘制两个变量(米和高度)的分布?

How to plot the distribution of two variable(meter & height) for two different scenario?

我有两个数据框,代表两种情况。每个数据框有 3 个变量:Area、Height 和 Scenario_Name(场景 1 和场景 2)。

我想要的:

  1. 我想显示我在每个高度有多少 Suitable_Area 这两种情况。
  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)向我展示了 Suitable_Area 但没有提供我想要的清晰和准确的信息(参见上面提到的第 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"))

我可以想象两个半透明的overplotting 直方图(宽度为1,对应高度为1米)可以完成这项工作,但我无法做到Suitable_Area 作为 Y 轴,通常是密度或计数。

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

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

提前致谢。

我不确定这是否是您想要的,但是分组条形图是否可行?

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()