[英]Dotplot with two categorical variables and two quantitative variables
我在制作點圖時遇到問題。 我有一個數據框“ distribution_tab”,其中包含4列6行。 前兩列是定量變量,另外兩列是分類值:
read.length percentage.GC strand organism
1 203 63.0 forward bacteria
2 250 33.0 forward plant
3 205 72.0 reverse bacteria
4 240 36.0 reverse plant
5 210 33.5 forward plant
6 230 63.5 reverse bacteria
我只想從此數據幀中繪制一個點圖,其中x軸為read.length,y軸為percent.GC。 股線“正向”必須用點表示,股線以三角形(或其他兩個不同的符號)反轉。 生物“細菌”必須用粉紅色表示,生物“植物”用綠色表示。
因此,例如,如果一個數據是“正向細菌”,則它必須在點圖中用粉紅色的圓點表示,而如果是“反向並細菌”,則它必須是綠色三角形。
我真的不知道該怎么做(或者根本不可能)。 目前,我已經用兩個定量變量繪制了一個散點圖:
plot(distribution_tab$read_length ~ distribution_tab$percentage.GC)
我不知道如何根據它們的生物和鏈值在圖中區分它們。
distribution_tab <- read.table(header = TRUE, text = "read.length percentage.GC strand organism
1 203 63.0 forward bacteria
2 250 33.0 forward plant
3 205 72.0 reverse bacteria
4 240 36.0 reverse plant
5 210 33.5 forward plant
6 230 63.5 reverse bacteria ")
plot(percentage.GC ~ read.length, data = distribution_tab,
pch = c(17,19)[(strand %in% 'forward') + 1L],
col = c('pink', 'green')[(organism %in% 'plant') + 1L])
或使用ifelse
但上述方法更靈活
plot(percentage.GC ~ read.length, data = distribution_tab,
pch = ifelse(strand %in% 'forward', 19, 17),
col = ifelse(organism %in% 'plant', 'green', 'pink'))
使用ggplot:
library(ggplot2)
df$col <- ifelse(df$organism == "bacteria", "pink", "green")
ggplot(df, aes(read.length, percentage.GC, shape = strand, col = col)) +
geom_point(size = 4) +
scale_color_identity()
#dummy data
df <- read.table(text=" read.length percentage.GC strand organism
1 203 63.0 forward bacteria
2 250 33.0 forward plant
3 205 72.0 reverse bacteria
4 240 36.0 reverse plant
5 210 33.5 forward plant
6 230 63.5 reverse bacteria ", header = TRUE)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.