簡體   English   中英

使用 ggplot2 的 R 數據可視化 - 使用 RColorBrewer 將 colors 添加到 20 個變量

[英]R data visualization with ggplot2 - adding colors to 20 variables with RColorBrewer

我正在嘗試使用 ggplot2 可視化數據,並將 20 個不同的 colors 添加到我在“描述”中的 20 個類別中。 沒有 colors,腳本可以完美運行(只有黑色)。 但是,無論如何我想添加顏色,它要么保持黑色,要么使用下面的腳本出現以下錯誤: Error: Aesthetics must be either length 1 or the same as the data (2265723): colour這不應該是問題,因為“Pastel1”有 255 colors,我創建了一個長度變量。

數據集(描述有 20 個類別):

chr pos p description
1 445578 0.05 Metabolic
3 659990 0.34 Metabolic
5 789689 0.55 Immunological
6 678599 0.05 BodyStructures
7 97890 0.67 Cardiovascular
2 67899 0.01 Hematological
8 9867647 0.34 Nutritional
3 675890 0.55 Environment
6 799030 0.76 Psychiatric
4 8609000 0.88 Cognitive
6 789900 0.12 Musculoskeletal
3 90907878 0.22 Opthalmological
colourCount = length(unique(dataset$description))
getPalette = colorRampPalette(brewer.pal(9, "Pastel1"))
Nean_PheWAS <- ggplot(dataset, aes(x=description, y=-log(p), colour = getPalette(colourCount))) + 
geom_jitter(mapping=aes(x=as.factor(description), y=-log10(p))) +
theme_classic() + 
theme(axis.text.x = element_blank(), 
panel.grid.minor=element_line(colour = "grey", linetype="dashed"), axis.ticks=element_blank()) + 
labs(color="description", size="Effect size", x="Phenotype Classes", y="log(p-value)") +
geom_hline(yintercept=-log(0.01), color="red", size=1, alpha=0.5)

我也願意接受其他解決方案,將 20 個不同的 colors 添加到類別中。

假設您希望通過變量description着色,您可以通過將scale_colour_manual(values = getPalette(colourCount))添加到 ggplot object 來使用 ColorBrewer 調色板和colourCount個元素。 並在美學中更改colour = description

colourCount = length(unique(dataset$description))
getPalette = colorRampPalette(brewer.pal(9, "Pastel1"))
Nean_PheWAS <- ggplot(dataset, aes(x=description, y=-log(p), colour = description)) + 
geom_jitter(mapping=aes(x=as.factor(description), y=-log10(p))) +
theme_classic() + scale_colour_manual(values = getPalette(colourCount)) +
theme(axis.text.x = element_blank(), 
panel.grid.minor=element_line(colour = "grey", linetype="dashed"), axis.ticks=element_blank()) + 
labs(color="description", size="Effect size", x="Phenotype Classes", y="log(p-value)") +
geom_hline(yintercept=-log(0.01), color="red", size=1, alpha=0.5)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM