【问题标题】:Multi x-axis using ggplot to present z-scores, iq scores and raw data使用 ggplot 呈现 z 分数、iq 分数和原始数据的多 x 轴
【发布时间】:2021-01-04 15:50:21
【问题描述】:

只是情境化,我从事心理测量学/心理测试。 我有一个由“points”、“percentile”、“z_real”、“z_normal”、“iq”组成的数据集。我想要一个单独的 ggplot,我可以在其中呈现 Z_score(来自我的原始数据)、z_score(具有潜在的正态分布),然后有两个带有“原始分数”和“iq 分数”的补充 x 轴.这在统计数据中很常见,您可以在下面查看

这是当前的情节

这是我最好的解决方案

这就是想要的情节

我正在与 tidyverse 合作,我想留在其中。以前的一些帖子对我有帮助,例如这个 [one][5] 和这个 [one][6]。

谢谢。 (部分)数据和代码在这里:

ask_ds <- structure(list(points = c(17, 17, 2, 16, 11, 17, 20, 16, 19, 
                                    15, 9, 14, 14, 16, 13, 13, 22, 21, 25, 17, 17, 17, 20, 6, 11, 
                                    5, 10, 23, 21, 19, 11, 15, 13, 17, 17, 17, 9, 18, 12, 22, 21, 
                                    23, 8, 12, 6, 7, 22, 12, 21, 16, 12, 5, 19, 19, 21, 13, 12, 18, 
                                    22, 13, 21, 24, 23, 4, 12, 19, 2, 22, 14, 16, 24, 19, 7, 12, 
                                    18, 14, 9, 18, 7, 22, 3, 18, 14, 22, 15, 11, 15, 19, 25, 9, 19, 
                                    16, 13, 19, 14, 15, 20, 3, 23, 9, 7, 22, 9, 22, 17, 12, 14, 11, 
                                    7, 9, 19, 16, 19, 10, 24, 14, 12, 18, 19, 24, 22, 11, 11, 15, 
                                    17, 15, 11, 11, 14, 24, 14, 21, 16, 10, 19, 21, 15, 20, 15, 20, 
                                    22, 10, 22, 16, 14, 16, 8, 16, 9, 15), percentile = c(63, 61, 
                                                                                          1, 56, 25, 59, 80, 55, 74, 49, 17, 45, 44, 57, 36, 32, 90, 85, 
                                                                                          99, 63, 63, 60, 82, 7, 24, 5, 20, 93, 83, 75, 24, 50, 35, 64, 
                                                                                          62, 61, 16, 65, 28, 90, 85, 93, 12, 28, 7, 8, 90, 26, 87, 55, 
                                                                                          30, 4, 74, 73, 87, 33, 30, 67, 91, 35, 86, 95, 93, 3, 29, 75, 
                                                                                          1, 89, 44, 55, 96, 75, 8, 27, 66, 41, 13, 68, 9, 91, 2, 65, 44, 
                                                                                          89, 51, 22, 46, 74, 98, 13, 76, 58, 37, 73, 43, 50, 79, 2, 93, 
                                                                                          17, 8, 91, 14, 92, 60, 26, 43, 25, 10, 14, 73, 57, 76, 18, 96, 
                                                                                          40, 31, 70, 73, 96, 91, 24, 23, 47, 59, 51, 24, 22, 40, 96, 45, 
                                                                                          85, 57, 17, 73, 86, 50, 79, 48, 81, 91, 21, 89, 53, 43, 58, 11, 
                                                                                          53, 14, 47), z_real = structure(c(0.36, 0.36, -2.38, 0.18, -0.73, 
                                                                                                                            0.36, 0.91, 0.18, 0.73, 0, -1.1, -0.19, -0.19, 0.18, -0.37, -0.37, 
                                                                                                                            1.28, 1.09, 1.83, 0.36, 0.36, 0.36, 0.91, -1.65, -0.73, -1.83, 
                                                                                                                            -0.92, 1.46, 1.09, 0.73, -0.73, 0, -0.37, 0.36, 0.36, 0.36, -1.1, 
                                                                                                                            0.55, -0.55, 1.28, 1.09, 1.46, -1.28, -0.55, -1.65, -1.47, 1.28, 
                                                                                                                            -0.55, 1.09, 0.18, -0.55, -1.83, 0.73, 0.73, 1.09, -0.37, -0.55, 
                                                                                                                            0.55, 1.28, -0.37, 1.09, 1.64, 1.46, -2.01, -0.55, 0.73, -2.38, 
                                                                                                                            1.28, -0.19, 0.18, 1.64, 0.73, -1.47, -0.55, 0.55, -0.19, -1.1, 
                                                                                                                            0.55, -1.47, 1.28, -2.2, 0.55, -0.19, 1.28, 0, -0.73, 0, 0.73, 
                                                                                                                            1.83, -1.1, 0.73, 0.18, -0.37, 0.73, -0.19, 0, 0.91, -2.2, 1.46, 
                                                                                                                            -1.1, -1.47, 1.28, -1.1, 1.28, 0.36, -0.55, -0.19, -0.73, -1.47, 
                                                                                                                            -1.1, 0.73, 0.18, 0.73, -0.92, 1.64, -0.19, -0.55, 0.55, 0.73, 
                                                                                                                            1.64, 1.28, -0.73, -0.73, 0, 0.36, 0, -0.73, -0.73, -0.19, 1.64, 
                                                                                                                            -0.19, 1.09, 0.18, -0.92, 0.73, 1.09, 0, 0.91, 0, 0.91, 1.28, 
                                                                                                                            -0.92, 1.28, 0.18, -0.19, 0.18, -1.28, 0.18, -1.1, 0), .Dim = c(150L, 
                                                                                                                                                                                            1L), "`\`\`\`scaled:center\`\`\``" = 15.0143288084465, "`\`\`\`scaled:scale\`\`\``" = 5.47051980922509), 
                         z_normal = c(0.33, 0.28, -2.33, 0.15, -0.67, 0.23, 0.84, 
                                      0.13, 0.64, -0.03, -0.95, -0.13, -0.15, 0.18, -0.36, -0.47, 
                                      1.28, 1.04, 2.33, 0.33, 0.33, 0.25, 0.92, -1.48, -0.71, -1.64, 
                                      -0.84, 1.48, 0.95, 0.67, -0.71, 0, -0.39, 0.36, 0.31, 0.28, 
                                      -0.99, 0.39, -0.58, 1.28, 1.04, 1.48, -1.17, -0.58, -1.48, 
                                      -1.41, 1.28, -0.64, 1.13, 0.13, -0.52, -1.75, 0.64, 0.61, 
                                      1.13, -0.44, -0.52, 0.44, 1.34, -0.39, 1.08, 1.64, 1.48, 
                                      -1.88, -0.55, 0.67, -2.33, 1.23, -0.15, 0.13, 1.75, 0.67, 
                                      -1.41, -0.61, 0.41, -0.23, -1.13, 0.47, -1.34, 1.34, -2.05, 
                                      0.39, -0.15, 1.23, 0.03, -0.77, -0.1, 0.64, 2.05, -1.13, 
                                      0.71, 0.2, -0.33, 0.61, -0.18, 0, 0.81, -2.05, 1.48, -0.95, 
                                      -1.41, 1.34, -1.08, 1.41, 0.25, -0.64, -0.18, -0.67, -1.28, 
                                      -1.08, 0.61, 0.18, 0.71, -0.92, 1.75, -0.25, -0.5, 0.52, 
                                      0.61, 1.75, 1.34, -0.71, -0.74, -0.08, 0.23, 0.03, -0.71, 
                                      -0.77, -0.25, 1.75, -0.13, 1.04, 0.18, -0.95, 0.61, 1.08, 
                                      0, 0.81, -0.05, 0.88, 1.34, -0.81, 1.23, 0.08, -0.18, 0.2, 
                                      -1.23, 0.08, -1.08, -0.08), iq = c(104.98, 104.19, 65.1, 
                                                                         102.26, 89.88, 103.41, 112.62, 101.88, 109.65, 99.62, 85.69, 
                                                                         98.12, 97.74, 102.65, 94.62, 92.98, 119.22, 115.55, 134.9, 
                                                                         104.98, 104.98, 103.8, 113.73, 77.86, 89.41, 75.33, 87.38, 
                                                                         122.14, 114.31, 110.12, 89.41, 100, 94.22, 105.38, 104.58, 
                                                                         104.19, 85.08, 105.78, 91.26, 119.22, 115.55, 122.14, 82.38, 
                                                                         91.26, 77.86, 78.92, 119.22, 90.35, 116.9, 101.88, 92.13, 
                                                                         73.74, 109.65, 109.19, 116.9, 93.4, 92.13, 106.6, 120.11, 
                                                                         94.22, 116.2, 124.67, 122.14, 71.79, 91.7, 110.12, 65.1, 
                                                                         118.4, 97.74, 101.88, 126.26, 110.12, 78.92, 90.81, 106.19, 
                                                                         96.59, 83.1, 107.02, 79.89, 120.11, 69.19, 105.78, 97.74, 
                                                                         118.4, 100.38, 88.42, 98.49, 109.65, 130.81, 83.1, 110.59, 
                                                                         103.03, 95.02, 109.19, 97.35, 100, 112.1, 69.19, 122.14, 
                                                                         85.69, 78.92, 120.11, 83.8, 121.08, 103.8, 90.35, 97.35, 
                                                                         89.88, 80.78, 83.8, 109.19, 102.65, 110.59, 86.27, 126.26, 
                                                                         96.2, 92.56, 107.87, 109.19, 126.26, 120.11, 89.41, 88.92, 
                                                                         98.87, 103.41, 100.38, 89.41, 88.42, 96.2, 126.26, 98.12, 
                                                                         115.55, 102.65, 85.69, 109.19, 116.2, 100, 112.1, 99.25, 
                                                                         113.17, 120.11, 87.9, 118.4, 101.13, 97.35, 103.03, 81.6, 
                                                                         101.13, 83.8, 98.87)), row.names = c(NA, -150L), class = c("tbl_df", 
                                                                                                                                    "tbl", "data.frame"))
ask_ds %>% 
  select(z_real, z_normal) %>% 
  pivot_longer(everything()) %>% 
  ggplot(., aes(value, fill = name)) + 
  geom_density(alpha=.2) +
  scale_x_continuous(breaks=-3:3, labels=parse(text=paste(-3:3, '*sigma')) ,
                     "Standard deviation", 
                     sec.axis = sec_axis(trans = ~.* sd(ask_ds$points) +
                                           mean(ask_ds$points), 
                                         "Raw score")) 


gridExtra::grid.arrange(ask_ds %>% 
                          select(z_real, z_normal) %>% 
                          pivot_longer(everything()) %>% 
                          ggplot(., aes(value, fill = name)) + 
                          geom_density(alpha=.2) ,
                        
                        ggplot(ask_ds, aes(points)) +
                          geom_density()
                        
)

【问题讨论】:

    标签: r ggplot2 data-visualization tidyverse


    【解决方案1】:

    我认为这里问题的核心是如何获得多个 x 轴,所以我将回答这个问题,而不是试图了解您的数据是关于什么的。在 ggplot2 中没有“正确”的方法来引入多个 x 轴,但是,您可以通过在彼此下方放置一堆微小的高度图来解决这个问题。为此,我推荐 patchwork 包。

    我们首先制作我们想要说明的情节,我将使用一些虚拟数据来做这个:

    library(ggplot2)
    library(patchwork)
    
    set.seed(0)
    df <- data.frame(
      x = rnorm(100, 100, 15)
    )
    
    dens <- ggplot(df, aes(x)) +
      geom_density() +
      labs(x = "IQ") +
      theme(axis.line.x = element_line())
    

    接下来,我们将制作两个虚拟图,其中 x 轴将被训练,但没有绘制几何图形。此外,纵横比设置为一个较小的数字,使其看起来好像这只是轴线并且没有出现绘图。这通常仅适用于从一个轴到下一个轴的线性平移。我不是心理测量学家,但我认为 IQ 大约是~N(100, 15),因此我们需要将其逆变换为标准正态以获得 Z 分数。

    zscore_axis <- ggplot(df, aes((x - 100) / 15)) +
      labs(x = "Z-score") +
      theme(axis.line.x = element_line(),
            aspect.ratio = 1e-8)
    
    quantile_axis <- ggplot(df, aes((x - 100) / 15)) +
      labs(x = "Quantile") +
      scale_x_continuous(breaks = qnorm(seq(0.1, 0.9, by = 0.1)),
                         labels = seq(0.1, 0.9, by = 0.1)) +
      theme(axis.line.x = element_line(),
            aspect.ratio = 1e-8)
    

    最后,通过拼凑魔法,我们将所有三个地块放在一个下方。

    dens / zscore_axis / quantile_axis
    

    reprex package (v0.3.0) 于 2020-09-17 创建

    这就是我制作多个 x 轴的方式。这将如何处理您的数据,我不确定。您可以调整自己的指标,例如原始分数,只要它们是数据的线性变换。

    【讨论】:

    • 真的令人印象深刻。感谢您提供此代码!
    【解决方案2】:

    我不知道patchwork 的事情——这真的很酷,谢谢@teunbrand。我有一个不太优雅的解决方案,但我想我会发布它。在这里,您在绘图区域内手动制作轴。

    ## turn your data into a stand alone dataset
    plot.dat <- ask_ds %>% 
      select(z_real, z_normal) %>% 
      pivot_longer(everything()) 
    ## identify the values for the x-axis
    xv <- c(-2, -1, 0, 1, 2)
    ## identify the middle values for the y-tick marks
    yv1 <- c(-.025,-.025,-.025,-.025,-.025)
    yv2 <- c(-.065,-.065,-.065,-.065,-.065)
    ## identify the transformations for the tick mark labels on other axes
    rv <- xv* sd(ask_ds$points) + mean(ask_ds$points)
    iq <- xv*15 + 100
    
    ## make the plot
    ggplot() + 
      geom_density(data=plot.dat, aes(value, fill = name), alpha=.2) +
      ## add an abline that will serve as the raw data axis
      geom_abline(slope=0, intercept=-.025) + 
      ## add segments for the tick marks
      geom_segment(aes(x=xv, y=yv1 - .005, xend=xv, yend=yv1 + .005)) + 
      ## add the text labels
      geom_text(aes(x=xv, y = yv1 - .011, label=sprintf("%.2f", rv))) + 
      ## repeat a bit lower for the IQ axis
      geom_abline(slope=0, intercept=-.065) + 
      geom_segment(aes(x=xv, y=yv2 - .005, xend=xv, yend=yv2 + .005)) +  
      geom_text(aes(x=xv, y = yv2 - .011, label=sprintf("%.2f", iq))) + 
      ## format the actual x-axis
      scale_x_continuous(breaks=-3:3, labels=parse(text=paste(-3:3, '*sigma')) ,
                         "Standard deviation") + 
      ## add a y-axis to identify the two new scales.
      scale_y_continuous(sec.axis = sec_axis(~., 
                                            breaks = c(-.025, -.065), 
                                            labels = c("Raw Data", "IQ"))) + 
      ## put the legend on top - this keeps it from being pushed aside for the second
      ## y-axis labels. 
      theme(legend.position="top")
    

    【讨论】:

      猜你喜欢
      • 2017-08-31
      • 1970-01-01
      • 2020-12-09
      • 1970-01-01
      • 1970-01-01
      • 2011-09-26
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      相关资源
      最近更新 更多