R 具有不同颜色和pch的绘图

R 具有不同颜色和pch的绘图,r,scatter-plot,R,Scatter Plot,我有这张桌子: structure(list(Samples = structure(1:7, .Label = c("Sample1", "Sample2", "sample3", "sample4", "sample5", "sample6", "sample7" ), class = "factor"), number_cycle = c(22L, 22L, 26L, 26L, 26L, 22L, 22L), Quality = structure(c(2L, 2L, 3L, 1L,

我有这张桌子:

structure(list(Samples = structure(1:7, .Label = c("Sample1", 
"Sample2", "sample3", "sample4", "sample5", "sample6", "sample7"
), class = "factor"), number_cycle = c(22L, 22L, 26L, 26L, 26L, 
22L, 22L), Quality = structure(c(2L, 2L, 3L, 1L, 1L, 1L, 3L), .Label = c("Bad", 
"Good", "Middle"), class = "factor"), Concentration = c(14L, 
24L, 22L, 40L, 10L, 27L, 12L), Raw_reads = c(100000L, 5000L, 
70000L, 340000L, 4789L, 50000L, 25000L)), class = "data.frame", row.names = c(NA, 
-7L))
我想做一个散点图(raw_reads~concentration),我可以观察到两个因素的分散:质量(颜色)和循环数(pch)。当我尝试这段代码时,我有一个没有任何点的绘图

palette(rainbow(3))
plot(test$Raw_reads ~ test$Concentration, 
     col = test$Quality, 
     pch = test$number_cycle)
legend(x = "topleft", 
       legend = levels(test$Quality),
       col = rainbow(3), 
       pch = test$number_cycle)
那么我该怎么做才能得到散点图呢


谢谢您的帮助。

当您使用base R plot时,对于颜色和pch, 您需要指定一个与数据点一样长的向量。对于pch,您需要指定1:20的有效数字,对于颜色,或者是因子,或者是字符。一种方法是调用预定义的pch或col向量:

palette(rainbow(3))
PCH = c(18,19)
names(PCH) = as.character(unique(test$number_cycle))
COL = palette(rainbow(3))
names(COL) = unique(test$Quality)
plot(test$Raw_reads,test$Concentration, 
col = COL[test$Quality], pch = PCH[as.character(test$number_cycle)])
legend(x = "topleft", legend = names(COL), fill = COL)
legend(x = 80000,y=42, legend = names(PCH), col = "black",pch = PCH)
您还需要两个图例,一个用于颜色,一个用于pch


当我尝试您的代码时,会收到关于“未实现的pch值‘26’”的警告,我认为您应该对此进行调查。(如果您将
number\u cycle
调低,使其所有数字都在
0:25
范围内,您将看到更多。请尝试
plot(0:25,0:25,pch=0:25)
进行演示。)或者只需将
pch=as.numeric(as.factor(test$number\u cycle))
设置为
数值即可。非常感谢您的帮助。欢迎!享受测序数据带来的乐趣:)