我正在為一個專案在 R 中處理這個資料集(頁面底部)。首先我加載數??據:
count_data <- read.table(file = "../data/GSE156388_read_counts.tsv", header = T, sep = "",
row.names = 1)
然后我使用 reshape2 融化資料:
melted_count_data <- melt(count_data)
然后我創建一個按組著色圖表的因素:
color_groups <- factor(melted_count_data$variable, labels = rep(c("siTFIP11", "siGl3"), each = 3))
現在我們到了我正在嘗試制作的條形圖:
ggplot(melted_count_data, aes(x = variable, y = value / 1e6, fill = color_groups))
geom_bar(stat = "identity") labs(title = "Read counts", y = "Sequencing depth (millions of reads)")
問題是這會創建一個帶有一堆條紋的條形圖,讓我相信它試圖將大量條形堆疊在一起,而不是僅僅創建一個實心塊。
我還想在圖中添加資料標簽:
geom_text(label = value / 1e6)
但這似乎只是將一堆價值觀放在一起。
對于堆疊條問題,我嘗試使用y = sum(values),但這只是使所有條的高度相同。我也嘗試過使用y = colSums(values),但這顯然不起作用,因為它需要“至少二維的陣列”。
我嘗試使用未融化的資料來解決這個問題,但無濟于事。
我只是有點放棄了標簽,因為我什至無法解決酒吧問題。
編輯:
我發現一個執行緒建議這個:
ggplot(melted_count_data, aes(x = variable, y = value / 1e6, color = color_groups))
geom_bar(stat = "identity") labs(title = "Read counts", y = "Sequencing depth (millions of reads)")
更改fill為color. 這會修復白線,但會導致一些(更少)黑線。看著這個新圖表讓我相信它實際上可能是在粘貼一堆圖表?
uj5u.com熱心網友回復:
你可以這樣做:
library(tidyverse)
url <- paste0( "https://www.ncbi.nlm.nih.gov/geo/download/",
"?acc=GSE156388&format=file&file=GSE156388%5",
"Fread_counts.tsv.gz")
tmpfile <- tempfile()
download.file(url, tmpfile)
count_data <- readr::read_tsv(gzfile(tmpfile),
show_col_types = FALSE)
count_data %>%
pivot_longer(-1) %>%
mutate(color_groups = factor(name,
labels = rep(c("siTFIP11", "siGl3"), each = 3))) %>%
group_by(name) %>%
summarise(value = sum(value)/1e6, color_groups = first(color_groups)) %>%
ggplot(aes(name, value, fill = color_groups))
geom_col()
geom_text(aes(label = round(value, 2)), nudge_y = 0.5)
labs(title = "Read counts", x = "", fill = "Type",
y = "Sequencing depth (millions of reads)")
scale_fill_manual(values = c("gold", "deepskyblue3"))
theme_minimal()

由reprex 包于 2022-03-21 創建(v2.0.1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/450638.html
