【问题标题】:A plot with different colors and pch具有不同颜色和 pch 的绘图
【发布时间】:2023-04-09 15:54:01
【问题描述】:

我有这张桌子:

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 ~ 浓度),我可以观察到两个因素的分散:质量(颜色)和周期数(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)

那么我该怎么做才能得到我的散点图呢?

感谢您的帮助。

【问题讨论】:

  • 当我尝试你的代码时,我收到关于 "unimplemented pch value '26'" 的警告,我认为你应该对此进行调查。 (如果您将number_cycle 降级,使其所有数字都在0:25 范围内,您会看到更多。尝试plot(0:25, 0:25, pch=0:25) 进行演示。)
  • 或者直接设置pch = as.numeric(as.factor(test$number_cycle))

标签: r scatter-plot


【解决方案1】:

当您使用基本 R 绘图时,对于颜色和 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。

【讨论】:

  • 非常感谢 StupidWolf 的帮助。
  • 不客气!尽情享受您的测序数据吧 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-06
  • 1970-01-01
  • 2021-09-16
  • 1970-01-01
  • 2022-01-26
  • 1970-01-01
相关资源
最近更新 更多