首页 文章

geom_hline有多个点和facet_wrap

提问于
浏览
1

我试图在我的数据的特定点绘制水平线 . 我的想法是,我希望在每个轴的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的情节

enter image description here

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截距处的第一个点(在0等效迭代处)的水平线(由前4个中的第5列中的ratio1给出)值) . 任何帮助将不胜感激 . 先感谢您

1 回答

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

    enter image description here

相关问题