单个ColorRamp调色板在两个绘图散点图中产生不同的颜色方案

时间:2016-11-22 23:13:00

标签: r plotly

正如标题所说,一个ColorRamp调色板用于两个不同的scetterlot由plotly构建,给出两个略有不同的外观(注意中间部分)颜色条,即使相应数据集的上限和下限被手动设置为在两个地块都是相同的。

我想让这些情节在视觉上具有可比性,为此,我显然必须拥有相同的颜色条。有没有办法做到这一点?

以下是代码:

myxaxis <- list(range = c(16, 44), dtick=2, gridwidth = 1, title = "Length of carbon chain") #setting the visible area of x axis
myyaxis <- list(range = c(0, 8), gridwidth = 1, title = "No. of double bonds") #setting the visible area of y axis
mycolors <- colorRampPalette(c('green', 'red', 'black'))(n = 100) #creating an RColorBrewer palette

ch_new1 <- cbind.data.frame(c('PA', 'PA', 'PA', 'PA', 'PA', 'PA', 'PA', 'PA', 'PA', 'upper bound', 'lower bound'), c(4.571087, 6.522441, 6.522441, 5.081869, 4.471815, 5.744834, 7.329796, 5.472866, 5.744834, 1, 1), c(10.52337, 16.75454, 16.0976, 16.47356, 18.94973, 17.46351, 10.97607, 18.11186, 11.64033, 0.2085327, 71.18021), c(32, 34, 34, 36, 36, 36, 38, 38, 38, 100, 100), c(1, 1, 2, 2, 3, 4, 4, 5, 6, 100, 100), c(0.4128963, 16.68394, 26.52718, 23.50851, 16.02339, 3.971546, 6.854153, 3.24342, 2.774968, 1, 1)) #the first dataset
colnames(ch_new1) <- c('Species', 'log_inversed_pval','fold_difference', 'N_of_carbons','N_of_double_bonds', 'rel_abund')
d <- plot_ly(ch_new1, x=~N_of_carbons, y=~N_of_double_bonds, text = ~paste('Percent of total', Species, '=', round(rel_abund, 0)), size=~rel_abund, color=~fold_difference, colors = mycolors)%>% #producing the scatter plot
  layout(
    xaxis = myxaxis,
    yaxis = myyaxis,
    title = paste('PA', '2b')
  )%>%
  colorbar(title="Fold difference", ypad=20)
export(d)

ch_new2 <- cbind.data.frame(c('LPC', 'LPC', 'LPC', 'lower limit', 'upper limit'), c(7.329796, 7.329796, 5.081869, 1, 1), c(2.952345, 5.042931, 3.700331, 0.2085327, 71.18021), c(18, 20, 22, 100, 100), c(0, 3, 5, 100, 100), c(82.87528, 13.56943, 3.555281, 1, 1)) #the second dataset
colnames(ch_new2) <- c('Species', 'log_inversed_pval','fold_difference', 'N_of_carbons','N_of_double_bonds', 'rel_abund')
d <- plot_ly(ch_new2, x=~N_of_carbons, y=~N_of_double_bonds, text = ~paste('Percent of total', Species, '=', round(rel_abund, 0)), size=~rel_abund, color=~fold_difference, colors = mycolors)%>% #creating the second scatterplot
  layout(
    xaxis = myxaxis,
    yaxis = myyaxis,
    title = paste(unique(ch$Species)[i], fraction)
  )%>%
  colorbar(title="Fold difference", ypad=20)
export(d)

chart #1 with bright red middle

chart #2 with dim red middle

1 个答案:

答案 0 :(得分:0)

我自己解决了这个问题。

事实证明,通过在绘图边缘之外添加一个或多个“锚定”虚拟点(因此它们未显示)有助于使绘图颜色条几乎相同。

初始数据集

  

ch_new1&lt; - cbind.data.frame(c(...))#the first dataset

应附加锚定虚拟点:

  

ch_new1&lt; - cbind.data.frame(c(...))#the first dataset

     

ch_new1&lt; - rbind(ch_new,list('middle anchor point',1,50,100,100,1))

     

ch_new1&lt; - rbind(ch_new,list('quarter anchor point',1,25,100,100,1))

tl; dr 将负责色条的变量锚定到多个参考点(10,20,30,40,50,...)