如何在同一基础R图中使用两个不同的调色板,同时在该调色板中按组分配颜色?

g0czyy6m  于 2023-05-26  发布在  其他
关注(0)|答案(1)|浏览(149)

我在画一个简单的以R为底的散点图。我绘制了x =延伸时间对y =荧光的曲线。我有两个不同的分组:一个是strain,另一个是cit。我尝试使用两种不同的调色板为两个不同的菌株,并有每个cit有自己的颜色在该调色板。
我通过将两个不同的菌株分离到单独的 Dataframe (NMmedian和cit20median)来解决这个问题,希望通过顺序绘制点来为每个菌株分配不同的调色板:

#defining my two palettes:
darkcols <- c(cit0 = "#1548E0", cit3 = "#008F00", cit6 = "#CCAD00", cit9 = "#B85C00", citMax =  "#B80000", citMin = "#8F008F") #for normalized
cols  <-    c(cit0 = "royalblue2", cit3 = "green3", cit6 = "gold1", cit9 = "darkorange2", citMax = "red2", citMin = "magenta3") #for WT

palette = cols

pdf("20citvsNMwt.pdf", width = 2, height = 1.67, pointsize = 7, useDingbats = F, bg = "white" )
# sets margin stuff:
par( mex = 0.65 ) 
par( mar = c(7,6.5,4,3) )
par( oma = c(0,0.5,1,0) )

#first plotting one strain (works as intended)
plot( NMmedians$elongation_time, NMmedians$ratio, 
      col = as.factor(NMmedians$cit),
      pch = 20,
      #cex = 0.6,
      axes = F,
      xlim = c(150,400),
      ylim = c(0,1),
      xlab = "",
      ylab = "fluorescence"
)

#now trying to plot the other strain, the col parameter is where I've been focusing my efforts:
points(cit20medians$elongation_time, cit20medians$ratio, 
 pch = 20, 
 col = list(darkcols, as.factor(cit20medians$cit))
 )

axis( 1 )
axis( 2 )
title( xlab = "elongation time", line = 4.5 )

dev.off()

here is the graph when I just use col = as.factor(cit20medians$cit) which doesn't change the palette for the second set of points
我已经成功地让它对第二组点使用darkcol调色板,只需要执行col = darkcols,但它不保留与每个“城市”的对应关系:here it is with both palettes, but darkcols at random
强调:从图形上看,这里的目标是根据它们属于哪个cit使每个点集沿着X轴呈彩虹状,并且根据它们属于哪个应变使每个点集沿着Y轴变亮或变暗。

euoag5mw

euoag5mw1#

我认为这是一个适当索引颜色向量名称的问题。下面是一个更简单的代表性例子:

nms <- names(cols)
NM <- data.frame(value=1:6, cit=nms)
c20 <- data.frame(value=2:7, cit=nms)

然后代码是:

plot(NM$value, col=cols[NM$cit], pch=19, cex=2, ylim=c(0,7))
points(c20$value, col=darkcols[c20$cit], pch=19, cex=2)

顺便说一句,你也可以通过编程方式将cols更改为darkcols

plot(NM$value, col=cols[NM$cit], pch=19, cex=2, ylim=c(0,7))
darkcols <- setNames(adjustcolor(cols, red.f=0.7, green.f=0.7, blue.f=0.7), names(cols))
points(c20$value, col=darkcols[c20$cit], pch=19, cex=2)

相关问题