2017-05-05 39 views
0

我一直在尋找一段時間來嘗試自己解決我的問題,但沒有遇到任何似乎與我的數據一起工作的東西。我有這樣的數據幀:如何使用相同的時間變量繪製三個變量? geom_point

dput(mydata2) 
structure(list(Sample = structure(c(44L, 2L, 4L, 10L, 14L, 15L, 
26L, 32L, 40L, 7L, 5L, 11L, 18L, 16L, 27L, 33L, 41L, 8L, 6L, 
12L, 19L, 17L, 28L, 34L, 42L, 29L, 35L, 43L, 30L, 36L, 45L, 1L, 
3L, 9L, 13L, 20L, 21L, 22L, 23L, 24L, 25L, 31L, 37L, 38L, 39L, 
46L, 47L), .Label = c("10242016 Female 2D", "10242016 Female 2D 10 minutes", 
"10242016 Female 2D 100 Refs", "10242016 Female 2D 100 Refs 10 minutes", 
"10242016 Female 2D 100 Refs 20 minutes", "10242016 Female 2D 100 Refs 30 minutes", 
"10242016 Female 2D 20 minutes", "10242016 Female 2D 30 minutes", 
"10242016 Female 2D 500 Refs", "10242016 Female 2D 500 Refs 10 minutes", 
"10242016 Female 2D 500 Refs 20 minutes", "10242016 Female 2D 500 Refs 30 minutes", 
"11012016 Female 2D", "11012016 Female 2D 10 minutes", "11012016 Female 2D 100 Refs 10 minutes", 
"11012016 Female 2D 100 Refs 20 minutes", "11012016 Female 2D 100 Refs 30 minutes", 
"11012016 Female 2D 20 minutes", "11012016 Female 2D 30 minutes", 
"11072016 Female 2D", "11102016 Female 2D", "11142016 Male 2D", 
"11172016 Male 2D", "11212016 Male 2D", "12072016 Male 1D", "12072016 Male 1D 10 Minutes", 
"12072016 Male 1D 20 Minutes", "12072016 Male 1D 30 Minutes", 
"12072016 Male 1D 40 Minutes", "12072016 Male 1D 50 Minutes", 
"12092016 Male 1D", "12092016 Male 1D 10 minutes", "12092016 Male 1D 20 minutes", 
"12092016 Male 1D 30 minutes", "12092016 Male 1D 40 minutes", 
"12092016 Male 1D 50 minutes", "12122016 Male 1D", "12162016 Male 1D", 
"12212016 Female 1D", "12212016 Female 1D 10 Minutes", "12212016 Female 1D 20 Minutes", 
"12212016 Female 1D 30 Minutes", "12212016 Female 1D 40 Minutes", 
"12212016 Female 1D 5 Minutes", "12212016 Female 1D 50 Minutes", 
"12232016 Female 1D", "12282016 Female 1D"), class = "factor"), 
    Minutes_to_100 = c(NA, 9.11666666666667, 9.1, 9.1, 8.86666666666667, 
    8.86666666666667, NA, NA, NA, 9.11666666666667, 9.1, 9.1, 
    8.86666666666667, 8.86666666666667, NA, NA, 15.0333333333333, 
    9.03333333333333, 9.01666666666667, 9.01666666666667, 8.86666666666667, 
    8.86666666666667, NA, NA, 15.0333333333333, NA, NA, 15.0333333333333, 
    NA, NA, 15.0333333333333, 9.03333333333333, 9.1, 9.1, 7.63333333333333, 
    10.3, 9.18333333333333, 11.2166666666667, 15.4333333333333, 
    12.45, NA, NA, NA, NA, 12.25, 26.3, 16.4833333333333), runtime = c(4.6, 
    6.3, 3.5, 4.5, 11.3, 8.1, 0.9, 0.8, 4.9, 6.5, 3.5, 4.5, 11.1, 
    8.1, 0.9, 0.9, 4.3, 6.3, 3.2, 4.5, 11.3, 8.3, 1.1, 1, 4.3, 
    1, 1, 4.3, 1.1, 1.1, 4.6, 7.5, 4.2, 4.3, 11.5, 15, 6.8, 15.5, 
    30.7, 34, 6, 16.1, 7.2, 8.9, 6.5, 2.5, 5.2), Sample_type = structure(c(1L, 
    2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 
    1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L), .Label = c("1D", "2D"), class = "factor"), Analysis_min = c(5, 
    10, 10, 10, 10, 10, 10, 10, 10, 20, 20, 20, 20, 20, 20, 20, 
    20, 30, 30, 30, 30, 30, 30, 30, 30, 40, 40, 40, 50, 50, 50, 
    60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 
    60)), .Names = c("Sample", "Minutes_to_100", "runtime", "Sample_type", 
"Analysis_min"), class = "data.frame", row.names = c(NA, -47L 
)) 

我想創建使用GGPLOT2,將同時顯示運行時和Minutes_to_100情節,大概抖動,使用相同的Analysis_min變量。

這裏是剛剛Analysis_min情節VS運行時:

ggplot(mydata2, aes(Analysis_min, runtime))+geom_point(aes(color = factor(Sample_type))) 

enter image description here

這裏是Analysis_min情節VS Minutes_to_100:

ggplot(mydata2, aes(Analysis_min, Minutes_to_100))+geom_point(aes(color = factor(Sample_type))) 

enter image description here

我沒有看到如何結合機器人h變量在一個陰謀。我想我想要的是一個plot,它包含x軸上的Analysis_min,Y軸上的運行時以及plot中包含的Minutes_to_100的點。

+0

我不知道哪個回答標記爲接受的答案。我最終結合了這兩個建議,因爲兩者都提供了我在情節中使用的不同部分。 – aminards

+0

[This meta post](https://meta.stackexchange.com/questions/13413/how-do-i-combine-two-answers-to-create-the-best-answer-on-stackoverflow)建議您創建來自兩個提供的答案的新答案並接受。 –

回答

1

您可以將Minutes_to_100的值映射到顏色,然後對sample_type使用形狀審美。

ggplot(mydata2, 
     aes(x = Analysis_min, 
      y = runtime, 
      colour = Minutes_to_100, 
      shape = factor(Sample_type))) + 
    geom_point() 

或者你可以通過Sample_type一面:

ggplot(mydata2, 
     aes(x = Analysis_min, 
      y = runtime, 
      colour = Minutes_to_100)) + 
    geom_point() + 
    facet_wrap(~Sample_type) 
2

另一種選擇是gather您的數據轉化爲包含可以採取兩種Minutes_to_100runtime列的新對象。然後,您可以使用此變量colourSample_type使用shape

#libraries 
    library(tidyr) 
    library(dplyr) 

#gather data 
df <- mydata2 %>% 
    gather(var, val, -Sample, -Sample_type, -Sample_type, -Analysis_min,) 

> head(df) 
            Sample Sample_type Analysis_min   var  val 
1   12212016 Female 1D 5 Minutes   1D   5 Minutes_to_100  NA 
2   10242016 Female 2D 10 minutes   2D   10 Minutes_to_100 9.116667 
3 10242016 Female 2D 100 Refs 10 minutes   2D   10 Minutes_to_100 9.100000 
4 10242016 Female 2D 500 Refs 10 minutes   2D   10 Minutes_to_100 9.100000 
5   11012016 Female 2D 10 minutes   2D   10 Minutes_to_100 8.866667 
6 11012016 Female 2D 100 Refs 10 minutes   2D   10 Minutes_to_100 8.866667 


ggplot(data = df, 
     aes(x = Analysis_min, y = val, 
      colour = var, shape = Sample_type)) + 
    geom_point() 

enter image description here

當你注意,一個jitter可能會有所幫助。

ggplot(data = df, 
     aes(x = Analysis_min, y = val, 
      colour = var, shape = Sample_type)) + 
    geom_point(position = position_jitter()) 

enter image description here

相關問題