【问题标题】:Creating a scatter plot in R with multiple y-axis-values for each x-axis-value? [closed]在R中创建一个散点图,每个x轴值都有多个y轴值? [关闭]
【发布时间】:2020-06-26 08:25:07
【问题描述】:

我有以下示例数据框:

jan feb mar apr may jun jul aug sep oct nov dec someValue 0 0 0 0 0 0 0 1 0 0 0 0 109.24673 0 0 0 0 0 1 1 1 1 0 0 0 108.24444 0 0 0 0 0 0 1 1 1 1 0 0 247.25433 0 0 0 0 0 0 1 1 1 1 0 0 192.22873

我现在想创建一个散点图,每个月在 x 轴上占据一个部分。 “someValue”列应为 y 轴。 对于月份列中的每个“1”,它应在散点图的适当部分创建一个点。每个“0”都应该被忽略并且在图中不可见。

我如何在 R 中实现这一点?谢谢!

【问题讨论】:

    标签: r dataframe plot charts scatter-plot


    【解决方案1】:

    假设您的数据称为 df:

    df = structure(list(jan = c(0L, 0L, 0L, 0L), feb = c(0L, 0L, 0L, 0L
    ), mar = c(0L, 0L, 0L, 0L), apr = c(0L, 0L, 0L, 0L), may = c(0L, 
    0L, 0L, 0L), jun = c(0L, 1L, 0L, 0L), jul = c(0L, 1L, 1L, 1L), 
        aug = c(1L, 1L, 1L, 1L), sep = c(0L, 1L, 1L, 1L), oct = c(0L, 
        0L, 1L, 1L), nov = c(0L, 0L, 0L, 0L), dec = c(0L, 0L, 0L, 
        0L), someValue = c(109.24673, 108.24444, 247.25433, 192.22873
        )), class = "data.frame", row.names = c(NA, -4L))
    

    你可以在基础 R 中做到这一点(过去的美好时光):

    ind = which(df[,1:12]==1,arr.ind=TRUE)
    
          row col
     [1,]   2   6
     [2,]   2   7
     [3,]   3   7
     [4,]   4   7
     [5,]   1   8
     [6,]   2   8
     [7,]   3   8
     [8,]   4   8
     [9,]   2   9
    [10,]   3   9
    [11,]   4   9
    [12,]   3  10
    [13,]   4  10
    

    所以你要绘制的是 x 上的列号和 someValue 对应的行值,让我们把它放到 data.frame 中

    plotdf = data.frame(x=ind[,"col"],
    y=df$someValue[ind[,"row"]])
    
        x        y
    1   6 108.2444
    2   7 108.2444
    3   7 247.2543
    4   7 192.2287
    5   8 109.2467
    6   8 108.2444
    7   8 247.2543
    8   8 192.2287
    9   9 108.2444
    10  9 247.2543
    11  9 192.2287
    12 10 247.2543
    13 10 192.2287
    

    你可以只使用基础 R(过去的美好时光):

    plot(plotdf,xlim=c(1,12),xaxt="n",ylab="somevalue")
    months = colnames(df)[1:12]
    axis(1,at=1:12,labels=months)
    

    或者,如果您喜欢一些花哨的图形,我们可以使用 plotly,使用我们之前定义的 plotdf 和月份:

    library(plotly)
    
    plot_ly(x=plotdf$x,y=plotdf$y,type="scatter") %>% 
    layout(xaxis=list(range=c(0,13),tickvals=1:12,
    dtick=1,ticktext = months))
    

    【讨论】:

    • 谢谢你为我工作。我只想提一下,我对您对数据框的第一个表示感到困惑。我已经有一个非常大的数据框,每个月有 12 列,额外值有 1 列,所以我可以忽略创建数据框的第一步。只是为了将来阅读本文的人提一下。
    【解决方案2】:

    诀窍是将数据转换为正确的形状,即通过例如转换为长格式。 gather 来自 tidyr。试试这个:

    library(dplyr)
    library(tidyr)
    library(ggplot2)
    
    df <- tribble(
       ~jan, ~feb, ~mar, ~apr, ~may, ~jun, ~jul, ~aug, ~sep, ~oct, ~nov, ~dec, ~someValue,
      0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 109.24673,
      0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 108.24444,
      0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 247.25433,
      0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 192.22873
      )
    
    months <- names(df)[grepl("^\\w{3}$", names(df))]
    
    df_gather <- df %>% 
      gather(month, value, -someValue) %>% 
      mutate(
        # Convert to factor and set order of months
        month = factor(month, levels = months),
        # Set "0" to missing
        someValue = ifelse(value == 0, NA, someValue))
    
    ggplot(df_gather, aes(month, someValue)) +
      geom_point()
    #> Warning: Removed 35 rows containing missing values (geom_point).
    

    reprex package (v0.3.0) 于 2020-03-14 创建

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-13
      • 2020-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-01
      • 2019-06-03
      相关资源
      最近更新 更多