用多种颜色为轴刻度文本着色 [英] Coloring the axis tick text by multiple colors
问题描述
我正在尝试使用R
的plotly
程序包绘制heatmap
,在此我想为y轴刻度文本的特定标签设置特定的颜色.
I'm trying to plot a heatmap
using R
's plotly
package, where I'd like to have specific colors to specific labels of the y-axis tick text.
这是一个示例数据集:
set.seed(1)
df <- reshape2::melt(matrix(rnorm(100),10,10,dimnames = list(paste0("G",1:10),paste0("S",1:10))))
这是我正在尝试的:
library(plotly)
library(dplyr)
plot_ly(z=c(df$value),x=df$Var2,y=df$Var1,colors=grDevices::colorRamp(c("darkblue","gray","darkred")),type="heatmap",colorbar=list(title="Scaled Value",len=0.4)) %>%
layout(yaxis=list(title=list(color=c(rep("darkred",5),rep("darkblue",5)))))
由于我收到以下消息,它不起作用:
It's not working since I'm getting:
更改:yaxis=list(title=list(color=c(rep("darkred",5),rep("darkblue",5))))
收件人:yaxis=list(title=list(color=list(c(rep("darkred",5),rep("darkblue",5)))))
或:yaxis=list(title=list(tickcolor=c(rep("darkred",5),rep("darkblue",5))))
或:yaxis=list(title=list(tickcolor=list(c(rep("darkred",5),rep("darkblue",5)))))
似乎没有帮助.
有什么主意吗?
推荐答案
有人在这里为Python询问并回答了一个具有不同设置的类似问题:
A similar question with a different setup has been asked and answered for Python here: Setting a different font color for specific x axis ticks. Below is my best attempt with your setup for R.
情节:
代码:
library(plotly)
library(dplyr)
# data
set.seed(1)
df <- reshape2::melt(matrix(rnorm(100),10,10,dimnames = list(paste0("G",1:10),paste0("S",1:10))))
# plotly setup
p1 <- plot_ly(z=c(df$value),x=df$Var2,y=df$Var1,colors=grDevices::colorRamp(c("darkblue","gray","darkred")),
type="heatmap",colorbar=list(title="Scaled Value",len=0.4))
p2 <- p1 %>% add_trace(xaxis='x2', showscale=FALSE)
p3 <- p2 %>% layout(xaxis=list(range=list(0,9),
tickvals=list(0,1,2,3,4),
tickfont=list(color='red')),
xaxis2=list(range=list(0,9), overlaying='x',
ticktext = list('s9', 's10'),
tickvals=list(5, 6, 7, 8, 9),
tickfont=list(color='blue')))
# plot it
p3
这篇关于用多种颜色为轴刻度文本着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!