如何更改R文件中每个绘图图形的默认线条样式

时间:2020-03-05 18:22:47

标签: r plotly

我正在写一篇论文,每篇论文都需要多张数字。我希望所有数字的格式相同。例如:所有行都应为2pt,我的第一行是蓝色和纯色,第二行是红色和虚线,第三行是绿色和虚线,等等。目前,我正在每个图中手动设置所有这些属性(如下所示)。是否有更有效的方法来处理大量数字?我可以更改plotly的默认线条样式吗?除了colorway命令外,我没有看到任何与此问题相关的信息。预先感谢!

示例代码:

library(plotly)
x <- c(0,1,2)
y1 <- c(0,1,2)
y2 <- c(2,3,4)
y3 <- c(4,3,2)
figData <- data.frame(x,y1,y2,y3)
fig1 <- plot_ly(figData, x=~x, y=~y1, type = 'scatter', mode = 'lines',
         line = list(color = rgb(.04,.27,.66), width = 2)) %>%
add_trace(y=~y2, line = list(color = rgb(.78,.04,.08), width = 2,dash="dash"))
fig1
fig2 <- plot_ly(figData, x=~x, y=~y2, type = 'scatter', mode = 'lines',
         line = list(color = rgb(.04,.27,.66), width = 2)) %>%
add_trace(y=~y3, line = list(color = rgb(.78,.04,.08), width = 2,dash="dash"))
fig2

我知道matplotlib具有类似以下功能:

mpl.rcParams['axes.prop_cycle'] = mpl.cycler(color=["b", "r", "g"]) 

所以我想知道情节是否也有类似的东西。

1 个答案:

答案 0 :(得分:0)

我们可以创建一个对象,其中包含相似线条的详细信息,并引用可打印代码中的细节。如果要更改这些行,只需调整line1line2中的值,它将应用于您针对该行类型调用的任何地方。

library(plotly)
x <- c(0,1,2)
y1 <- c(0,1,2)
y2 <- c(2,3,4)
y3 <- c(4,3,2)

line1 <- list(color = rgb(.04,.27,.66), width = 2)
line2 <- list(color = rgb(.78,.04,.08), width = 2,dash="dash")

figData <- data.frame(x,y1,y2,y3)

fig1 <- plot_ly(figData, x=~x, y=~y1, type = 'scatter', mode = 'lines',
                line = line1) %>%
  add_trace(y=~y2, line = line2)


fig2 <- plot_ly(figData, x=~x, y=~y2, type = 'scatter', mode = 'lines',
                line = line1) %>%
  add_trace(y=~y3, line = line2)

fig2