2017-06-29 11 views
1

私はggplotでのプロット、または一般的なrに関する質問があります。r -ggplot:ggplotの離散y軸エントリ?

私は次のようになり、データフレームがある場合:

df <- data_frame(Patients = c(1:10), 
       test1 = sample(c(0,1), 10, replace = TRUE), 
       test2 = sample(c(0,1), 10, replace = TRUE), 
       test3 = sample(c(0,1), 10, replace = TRUE), 
       test4 = sample(c(0,1), 10, replace = TRUE)) 

を次に出力は次のようになります。

# A tibble: 10 x 5 
    Patients test1 test2 test3 test4 
     <int> <dbl> <dbl> <dbl> <dbl> 
1  1  0  1  1  0 
2  2  1  0  1  1 
3  3  1  0  0  1 
4  4  1  1  0  1 
5  5  1  0  1  1 
6  6  1  0  0  1 
7  7  1  1  1  0 
8  8  1  0  0  1 
9  9  1  0  1  1 
10  10  0  1  0  1 

どのように患者が別個のエントリとして、x軸上にあるグラフを作ることができ、y軸は、4つの別個のエントリ、各テストのいずれかを持って?例えば

enter image description here

Iはまた、このように、一致するタイルの数でx軸を注文したいです。あなたの時間のために非常に多くの

enter image description here

感謝。

答えて

0

データを長い形式に再構成し、患者別のテスト数を計算し、Patientsを希望の順序の係数に変換するために使用します。

library(tidyverse) 
library(forcats) 
library(stringr) 
theme_set(theme_minimal()) 

set.seed(2) 
df <- data_frame(Patients = c(1:10), 
       test1 = sample(c(0,1), 10, replace = TRUE), 
       test2 = sample(c(0,1), 10, replace = TRUE), 
       test3 = sample(c(0,1), 10, replace = TRUE), 
       test4 = sample(c(0,1), 10, replace = TRUE)) 

ggplot(gather(df, Test, value, -Patients) %>% 
     group_by(Patients) %>% 
     mutate(Test = str_to_title(gsub("(.*)([0-9])", "\\1 \\2", Test)), 
       numTests = sum(value)) %>% 
     ungroup %>% 
     arrange(numTests, Patients) %>% 
     mutate(Patients = fct_inorder(factor(Patients))), 
     aes(Patients, Test, fill=factor(value))) + 
    geom_tile(colour="grey60", lwd=0.8, show.legend=FALSE) + 
    scale_fill_manual(values=c("white","black")) + 
    labs(x="Patient", y="") 

enter image description here

+0

それは私が探していた絶対に何で、どうもありがとうございました、と私はとして-よくあなたのプロセスの明確化を感謝しています。 –

関連する問題