0

フィルター処理されたデータと 1 つの列からの値の選択に基づいて geom_hline を割り当てようとしていますが、これはすべて同じコード チャンク内で行っています。最善の方法が何であるかはわかりません-どんな助けでも大歓迎です。

サンプルデータ:

structure(list(sample_name = c("control1", "control2", "S01", 
"S02", "S03", "S04", "S05", "S06", "S07", "S08"), estimate = c(1.703, 
5.553, 4.851, 5.257, 4.573, 3.278, 1.687, 3.628, 1.877, 5.826
), std.error = c(1.767, 2.382, 1.641, 1.062, 1.133, 1.477, 0.978, 
0.611, 1.893, 0.78), upper_limit_value = c(5.166, 10.223, 8.067, 
7.339, 6.795, 6.173, 3.605, 4.825, 5.586, 7.355), lower_limit_value = c(-1.761, 
0.884, 1.635, 3.175, 2.352, 0.384, -0.231, 2.431, -1.833, 4.298
)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))
df%>%
  ggplot(., aes(x=sample_name, y=estimate, group=sample_name, color=sample_name))+ 
  geom_point() +
  geom_hline(???)

geom_hline() 部分内で、フィルタリングされたデータに基づいて y 切片を定義する方法はありますか?

の線に沿った何か

geom_hline(aes(yintercept = df %>% filter(sample_name="control1") %>% select("upper_limit_value"))) +
geom_hline(aes(yintercept = df %>% filter(sample_name="control1") %>% select("lower_limit_value")))

この場合、sample_name="control1" のフィルター処理されたデータは 1 行しかなく、"upper_limit_value" 列 (および "lower_limit_value" 値を別の geom_hline として) の下の値を使用しようとしています。

ありがとう!

4

1 に答える 1

1

引数dataのデータをサブセット化してみてください:

geom_hline(data = df %>% filter(sample_name == "control") %>% select(upper_limit_value),
           mapping = aes(yintercept = upper_limit_value))

完全なコードは次のようになります。

df %>%
  ggplot(aes(x=sample_name, y=estimate, group=sample_name, color=sample_name))+ 
  geom_point() +
  geom_hline(data = df %>% filter(sample_name == "control") %>% select(upper_limit_value),
             mapping = aes(yintercept = upper_limit_value))

(必要があるとは思いませんselect(upper_limit_value)が、テストデータがなければ簡単には言えません。)

別のオプションはpull、 ではなく、質問のコードで使用することselectです。違いは、selectそれらの列を含むデータ セットをpull返し、列の値を返すことです。

geom_hline(aes(yintercept = df %>% filter(sample_name == "control") %>% pull(upper_limit_value)))

編集

上記で言及しているデータ変換は

df$sample_name[df$sample_name == "control2"] <- "control"

次に、filteringeom_hlineは 1 行のデータ セットを返します。

このコメントの後、投稿されたデータを使用してコードをテストしましたが (ただし、に変更"control2"した後"control")、すべてが期待どおりに機能します。

dfまた、データ セットに変換を適用しないため、最初のintoパイプはggplot不要であることをお勧めします。

ggplot(df, aes(x=sample_name, y=estimate, group=sample_name, color=sample_name))+ 
  geom_point() +
  geom_hline(aes(yintercept = df %>% filter(sample_name == "control") %>% pull(upper_limit_value)))
于 2020-11-01T18:02:00.483 に答える