【问题标题】:What type of graph is this? And can it be created using ggplot2?这是什么类型的图表?它可以使用ggplot2创建吗?
【发布时间】:2018-05-11 05:42:06
【问题描述】:

我有这个图表,我正在尝试复制。它有两个用于 X 轴和 Y 轴的连续变量,并用一条线绘制这两个变量之间的关系图。

我的问题分为两部分:

  • 首先,这种图表叫什么?这是不寻常的,因为点之间的线是由第三个变量(年份)而不是它们在 X 轴上的位置决定的。

  • 其次,有谁知道这是否可以用 ggplot 实现?到目前为止,我已经创建了一个类似于上面的图表,但没有连接点的线。这段代码 ggplot(data, aes(x = Weekly_Hours_Per_Person, y = GDP_Per_Hour)) + geom_point() 得到以下输出: 但是多年来如何获得这条线?

对任何一点的任何帮助都将不胜感激。谢谢!

【问题讨论】:

    标签: r plot ggplot2 graph


    【解决方案1】:

    只是扩展了原始问题。这是一个路径图,正如here 解释的那样:

    “geom_path() 按照它们在数据中出现的顺序连接观察结果。geom_line() 按照变量在 x 轴上的顺序连接它们。”

    作为原始问题的扩展,您可以标记线条弯曲的选定点。这是一个具有可重现数据的示例:

    set.seed(123)
    df <- data.frame(year = 1960:2006,
               Weekly_Hours_Per_Person = c(2:10, 9:0, 1:10, 9:1, 2:10),
               GDP_Per_Hour = 1:47 + rnorm(n = 47, mean = 0))
    
    # Only label selected years
    df_label <- filter(df, year %in% c(1960, 1968, 1978, 1988, 1997, 2006))
    

    并使用ggrepel 包从顶点偏移标签。

    library(ggrepel)
    
    ggplot(df, aes(Weekly_Hours_Per_Person, GDP_Per_Hour)) +
      geom_path() +
      geom_point(data = df_label) +
      geom_text_repel(data = df_label, aes(label = year)) +
      scale_x_continuous(limits = c(-2, 12))
    ))
    

    【讨论】:

      【解决方案2】:

      使用geom_path,即

      libraray(ggplot2)
      ggplot(data, aes(x = Weekly_Hours_Per_Person, y = GDP_Per_Hour)) +
      geom_point() + 
      geom_path()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-27
        • 1970-01-01
        • 2020-12-12
        • 1970-01-01
        • 2023-01-09
        相关资源
        最近更新 更多