2016-11-22 106 views
1

我有一個數據幀,包含一些簡單的信息。樣本代碼(A,C,T或G)以及每個樣本的一些計數。我還有一個數據框,其中包含一些我想爲每個代碼繪製垂直線的方法。不幸的是,當我製作情節時,所有情節都顯示出來。有什麼辦法可以在一個地塊上繪製一條垂直線?ggplot2方面中的單個垂直線

非常感謝及以下

虛擬數據

sample <- c(1:100) 
code <- c(rep("A", 25), rep("C", 25),rep("G", 25),rep("T", 25)) 
count <- sample(1:30, 100, replace=T) 
df <- data.frame(sample, code, count) 


vline.data <- data.frame(z = c(15, 20, 25, 30)) 

ggplot(df, aes(x=count))+ 
    geom_histogram(binwidth=.5)+ 
    facet_grid(. ~ code)+ 
    geom_vline(aes(xintercept = z), vline.data)+ 
    theme(axis.title.x=element_text(), 
     axis.title.y=element_text(), 
     legend.position="none") 
+0

使用'vline.data < - data.frame( z = c(15,20,25,30),code = c(「A」,「C」,「G」,「T」))。這就是你將每個價值與特定方面聯繫起來的方式。您可以使用'dplyr'包:'library(dplyr)',然後在ggplot代碼中運行:'geom_vline(aes(xintercept = z),df%>% group_by(code)%>%summarize(z = mean(count)))+' – eipi10

+0

@ eipi10感謝您的更新迴應。這工作完美。謝謝! –

+0

請用你正在運行的確切代碼更新你的問題。 – eipi10

回答

1

一個簡單的例子也許這?:

sample <- c(1:100) 
code <- c(rep("A", 25), rep("C", 25),rep("G", 25),rep("T", 25)) 
count <- sample(1:30, 100, replace=T) 
df <- data.frame(sample, code, count) 

library(dplyr) 

vline.data <- df %>% 
       group_by(code) %>% 
       summarize(z = mean(count)) 

ggplot(df, aes(x=count))+ 
    geom_histogram(binwidth=.5)+ 
    facet_grid(. ~ code)+ 
    geom_vline(aes(xintercept = z), vline.data, colour = "red")+ 
    theme(axis.title.x=element_text(), 
     axis.title.y=element_text(), 
     legend.position="none") 

enter image description here