设置子图颜色 plotly R
Posted
技术标签:
【中文标题】设置子图颜色 plotly R【英文标题】:Set subplot colors plotly R 【发布时间】:2021-12-26 20:56:53 【问题描述】:我有这个子图代码,我想通过自定义调色板设置颜色,但似乎没有任何颜色。
data.frame(x = sample(letters[1:3],30,replace = TRUE)) %>%
mutate(y = rnorm(30),
z = runif(30,10,20)) %>%
group_by(x) %>%
do(p=plot_ly(.,x = ~y, y=~z, type = "scatter", mode = "lines", line = list(color = "blue","red","green")))) %>%
subplot(nrows = 1,
shareX = FALSE,
shareY = TRUE,
margin = 0.0001)
【问题讨论】:
【参考方案1】:实现所需结果的一个选项是将分组变量映射到color
“审美”并通过colors
参数设置自定义调色板,我个人更喜欢使用命名向量来分配颜色分类:
library(plotly)
set.seed(123)
data.frame(x = sample(letters[1:3], 30, replace = TRUE)) %>%
mutate(
y = rnorm(30),
z = runif(30, 10, 20)
) %>%
group_by(x) %>%
do(p = plot_ly(., x = ~y, y = ~z, color = ~x, type = "scatter", mode = "lines", colors = c(a = "blue", b = "red", c = "green"))) %>%
subplot(
nrows = 1,
shareX = FALSE,
shareY = TRUE,
margin = 0.0001
)
【讨论】:
以上是关于设置子图颜色 plotly R的主要内容,如果未能解决你的问题,请参考以下文章
R语言plotly包可视化线图(line plot)使用restyle参数自定义设置可视化结果中线条的颜色使用按钮动态切换线条的颜色(change line color with button)
R语言使用R原生函数plot和lines可视化线图并使用lty参数自定义线条类型lwd自定义设置线条的粗细col参数自定义线条颜色(Change R base plot line types)
R语言plotly可视化:plotly可视化基础二维直方图自定义设置二维直方图的颜色二维直方图是二元分布的直方图可视化(Basic 2D Histogram)