[英]Barplot to plot frequency of DNA in different sequence lengths
我將此數據df<- read.table("WT1.txt", header= TRUE)
df
讀取為df<- read.table("WT1.txt", header= TRUE)
。 我想繪制每個長度值的直方圖標簽ACGT頻率。 有沒有更好的方法來繪制此圖?
df
length A C G T
17 95668 73186 162726 730847
18 187013 88641 120631 334695
19 146061 373719 152215 303973
20 249897 73862 115441 343179
21 219899 82356 109536 636704
22 226368 101499 111974 1591106
23 188187 112155 98002 1437280
您可以通過可變length
將數據幀融為長格式,並使用ggplot2
繪制堆疊的條形圖:
df <- read.table(text=
"length A C G T
17 95668 73186 162726 730847
18 187013 88641 120631 334695
19 146061 373719 152215 303973
20 249897 73862 115441 343179
21 219899 82356 109536 636704
22 226368 101499 111974 1591106
23 188187 112155 98002 1437280", header=T)
library(reshape2)
df <- melt(df, id.vars = "length")
library(ggplot2)
ggplot(df)+
geom_bar(aes(x=length, y=value, fill=variable), stat="identity")
使用dplyr
計算每個基准的頻率,並使用ggplot2
繪制條形圖。 我更喜歡使用stat = "identity", position = "dodge"
而不是僅使用stat = "identity"
因為它可以更好地理解數據的外觀。
library(tidyverse)
gather(df, Base, value, -length) %>%
group_by(length) %>%
mutate(frequency = value / sum(value)) %>%
ggplot(aes(factor(length), y = frequency, fill = Base))+
geom_bar(stat = "identity", position = "dodge",
color = "black", width = 0.6) +
labs(x = "Base pairs",
y = "Frequency",
fill = "Base") +
scale_y_continuous(limits = c(0, 1)) +
scale_fill_brewer(palette = "Set1") +
theme_classic()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.