我有一個名為 的資料cells框,其中包含行中的單元格和列中的樣本,這是一個 dput 樣本:
structure(c(8.10937548981953e-20, 0.095381661829093, 0.054868371418562,
0.0523687378840825, 0.0100173293159538, 0.0332395245437795, 3.37811149975583e-20,
0.048191378909587, 0.13314908462763, 0, 0.00612878313809124,
0, 0.00209460699328093, 0.205599458004829, 0.318048653115709,
4.21796249339787e-05, 0.00844407692255898, 0, 0.00613007026042523,
0.0300024082993193, 0.0405191646567986, 0.00654087887823056,
0.0111094954094255, 1.30617589099212e-19, 0.0398730537850546,
0.0390946117756341, 0.239413780024853, 2.07521807718399e-19,
0.00116980239850497, 0, 0, 0.00971921247886335, 0.0588607291731613,
3.8563512241696e-21, 0.00247621905821516, 0), .Dim = c(6L, 6L
), .Dimnames = list(c("Adipocytes", "B-cells", "Basophils", "CD4 memory T-cells",
"CD4 naive T-cells", "CD4 T-cells"), c("Pt1", "Pt10", "Pt103",
"Pt106", "Pt11", "Pt17")))
行數cells為 38,列數為 49。我要為其構建箱線圖的目標單元格型別是:CD4 memory T-cells
此外,還有另一個名為 的資料框Metadata,其中包含回應列,如果樣本對某種療法有反應 - Metadata$Benefit。所以對于每個樣本,cells我都可以知道他們是否回應。
目標:在 x 軸和 y 軸上制作一個有回應/無回應的箱線圖,我需要帶有單個資料點的箱線圖。
我的代碼:
cells %>%
ggplot( aes(x = as.factor(Metadata$Benefit), y = as.numeric(cells['CD4 memory T-cells',]), fill= c('red','lightblue')))
geom_boxplot()
scale_fill_viridis(discrete = TRUE, alpha=0.6)
geom_jitter(color="black", size=0.4, alpha=0.9)
theme_ipsum()
theme(
legend.position="none",
plot.title = element_text(size=11)
)
ggtitle("A boxplot with jitter")
xlab("")
錯誤:Aesthetics must be either length 1 or the same as the data (38): x, y and fill
我不明白有什么問題,因為length(as.factor(Metadata$Benefit))和length(as.numeric(cells['CD4 memory T-cells',]))都是 49,它是一樣的,所以有什么問題?
uj5u.com熱心網友回復:
如評論中所述,您在此處的嘗試存在一些問題。你不能從不同的來源獲取x和美學,所以你應該先合并它們。這也意味著在合并之前旋轉(我在它仍然是矩陣時使用)原始資料,因此每個樣本都是一行。為了證明這一點,我制作了一些回應元資料。ydatat()
library(tidyverse)
d <- structure(c(8.10937548981953e-20, 0.095381661829093, 0.054868371418562, 0.0523687378840825, 0.0100173293159538, 0.0332395245437795, 3.37811149975583e-20, 0.048191378909587, 0.13314908462763, 0, 0.00612878313809124, 0, 0.00209460699328093, 0.205599458004829, 0.318048653115709, 4.21796249339787e-05, 0.00844407692255898, 0, 0.00613007026042523, 0.0300024082993193, 0.0405191646567986, 0.00654087887823056, 0.0111094954094255, 1.30617589099212e-19, 0.0398730537850546, 0.0390946117756341, 0.239413780024853, 2.07521807718399e-19, 0.00116980239850497, 0, 0, 0.00971921247886335, 0.0588607291731613, 3.8563512241696e-21, 0.00247621905821516, 0), .Dim = c(6L, 6L), .Dimnames = list(c("Adipocytes", "B-cells", "Basophils", "CD4 memory T-cells", "CD4 naive T-cells", "CD4 T-cells"), c("Pt1", "Pt10", "Pt103", "Pt106", "Pt11", "Pt17")))
metadata <- data.frame(sample = c("Pt1", "Pt10", "Pt103", "Pt106", "Pt11", "Pt17"), response = c(T, T, T, F, F, F))
d %>%
t() %>%
as.data.frame() %>%
rownames_to_column("sample") %>%
right_join(metadata, .) %>%
ggplot(aes(x = response, y = `CD4 memory T-cells`))
geom_boxplot(aes(fill = response))
scale_fill_manual(values = c('red','lightblue'))
#> Joining, by = "sample"

由reprex 包于 2022-02-16 創建(v2.0.1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/427634.html
上一篇:修復ggplot2中的軸標簽長度
下一篇:使用ggplot2的螺旋條形圖
