我有一系列大約 300 個資料幀,每個資料幀的結構都相同,我想撰寫一個代碼,將每個資料幀變成自己的條形圖。我正在努力撰寫一個首先正確構建圖形的代碼。我的資料框如下所示:
precursorMz Mz_Round HW Intensity Reg Intensity diff1 diff2
1 256.6814 141.10 4216 3994 0.96 1.00
2 256.6814 142.10 7184 5988 1.00 1.02
3 256.6814 143.12 44510 30020 1.02 1.00
4 256.6814 144.12 1858 1312 1.00 0.00
5 256.6814 260.20 43010 23230 4.52 1.00
6 256.6814 261.20 9452 6388 1.00 0.99
我希望我的圖表將 Mz_Round 列作為 X 軸,然后我的 Y 值是 HW 強度和 Reg 強度。
我曾嘗試使用 barplot() 函式,但在讓我的軸正確時再次遇到問題。
intensities <- table(split1$`HW Intensity`, split1$`Reg Intensity`)
barplot(intensities,
main = "Intensity Compared",
xlab = "M/z", ylab = "Intensity",
col = c("darkgrey", "blue"),
rownames(split1$Mz_Round),
beside = TRUE)
uj5u.com熱心網友回復:
我已經嘗試了幾個情節。我希望這有幫助。
# Data
> dput(df)
structure(list(precursor_Mz = c(256.6814, 256.6814, 256.6814,
256.6814, 256.6814, 256.6814), Mz_Round = c(141.1, 142.1, 143.12,
144.12, 260.2, 261.2), HW_Intensity = c(4216, 7184, 44510, 1858,
43010, 9452), Reg_Intensity = c(3994, 5988, 30020, 1312, 23230,
6388), diff1 = c(0.96, 1, 1.02, 1, 4.52, 1), diff2 = c(1, 1.02,
1, 0, 1, 0.99)), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -6L), spec = structure(list(cols = list(
precursor_Mz = structure(list(), class = c("collector_double",
"collector")), Mz_Round = structure(list(), class = c("collector_double",
"collector")), HW_Intensity = structure(list(), class = c("collector_double",
"collector")), Reg_Intensity = structure(list(), class = c("collector_double",
"collector")), diff1 = structure(list(), class = c("collector_double",
"collector")), diff2 = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1L), class = "col_spec"))
library(tidyverse)
# pivoting data
df1 <- df|>
select("Mz_Round", "HW_Intensity", "Reg_Intensity")|>
pivot_longer(!Mz_Round)
# stacked bar plot
ggplot(df1)
geom_col(aes(x = as.factor(Mz_Round), y = value, fill = name))
# dodged bar plot
ggplot(df1)
geom_col(aes(x = as.factor(Mz_Round), y = value, fill = name), position = "dodge")

轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/530798.html
標籤:rggplot2
