【问题标题】:geom_hline with multiple points and facet_wrap具有多个点和 facet_wrap 的 geom_hline
【发布时间】:2018-12-27 19:36:19
【问题描述】:

我正在尝试在数据的特定点绘制水平线。这个想法是,我希望在每个轴的 y 截距处的等效迭代的第一个值(即 0)的水平线; SA、VLA、HLA。我的问题会随着数据变得更加清晰。

iterations  subsets equivalent_iterations   axis    ratio1        ratio2
0              0                       0     SA     0.023569024 0.019690577
0              0                       0     SA     0.023255814 0.019830028
0              0                       0     VLA    0.025362319 0.020348837
0              0                       0     HLA    0.022116904 0.021472393
2              2                       4     SA     0.029411765 0.024911032
2              2                       4     SA     0.024604569 0.022838499
2              2                       4     VLA    0.026070764 0.022727273
2              2                       4     HLA    0.027833002 0.027888446
4             15                      60     SA     0.019746121 0.014403292
4             15                      60     SA     0.018691589 0.015538291
4             15                      60     VLA    0.021538462 0.01686747
4             15                      60     HLA    0.017052375 0.017326733
16            5                       80     SA     0.019021739 0.015021459
16            5                       80     SA     0.020527859 0.015384615
16            5                       80     VLA    0.023217247 0.017283951
16            5                       80     HLA    0.017391304 0.016298021

这是我使用 ggplot 的情节

ggplot(df)+ 
  aes(x = equivalent_iterations, y = ratio1, color = equivalent_iterations)+ 
  geom_point() + 
  facet_wrap(~axis) + 
  expand_limits(x = 0, y = 0) 

我想要的是对于每个轴 SA、VLA、HLA(即每个 facet_wrap)在 y 截距(由第 5 列中的 ratio1 给出前 4 个值)。任何帮助将不胜感激。提前谢谢你

【问题讨论】:

    标签: r ggplot2 facet-wrap geom-hline


    【解决方案1】:

    您可以像对待任何其他geom_* 一样对待它。只需使用要绘制水平线的 ratio1 值创建一个新列。我通过将数据设置为那些迭代 = 0(注意 SA 有 2 个)并将 ratio1 列加入原始数据帧的数据来做到这一点。然后可以将此列传递给geom_hline() 中的美学调用。

    library(tidyverse)
    
    df %>% 
      left_join(df %>% 
                  filter(iterations == 0) %>% 
                  select(axis, intercept = ratio1)) %>% 
    
      ggplot(aes(x = equivalent_iterations, y = ratio1, 
                 color = equivalent_iterations)) +
      geom_point() + 
      geom_hline(aes(yintercept = intercept)) +
      facet_wrap(~axis) + 
      expand_limits(x = 0, y = 0) 
    

    【讨论】:

      猜你喜欢
      • 2018-12-22
      • 1970-01-01
      • 2013-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-24
      • 1970-01-01
      相关资源
      最近更新 更多