如何使用曲线图改变条形图中的颜色方案?

时间:2016-05-30 20:44:05

标签: r plotly r-plotly

我想使用绘图库为我的条形图使用不同的颜色方案,其代码现在看起来像这样:

library(plotly)

p <- plot_ly(
 x=c("1","2", "3", "4", "5", "6", "7", "8", "9"),
 y=c(0.79, 0.57, 0.57, 0.59, 0.46, 0.38, 0.33, 0.57),
 name = "2007",
 type = "bar"
)
p

p2 <- add_trace(
  p,
  x=c("1","2", "3", "4", "5", "6", "7", "8", "9"),
  y=c(0.79, 0.57, 0.57, 0.59, 0.46, 0.38, 0.25, 0.58),
  name = "2008",
  type = "bar")
p2

1 个答案:

答案 0 :(得分:4)

你的意思是这样的:

p <- plot_ly(
 x=c("1","2", "3", "4", "5", "6", "7", "8", "9"),
 y=c(0.79, 0.57, 0.57, 0.59, 0.46, 0.38, 0.33, 0.57),
 name = "2007",
 type = "bar", marker = list(color = toRGB("yellow"))
)
p

p2 <- add_trace(
  p,
  x=c("1","2", "3", "4", "5", "6", "7", "8", "9"),
  y=c(0.79, 0.57, 0.57, 0.59, 0.46, 0.38, 0.25, 0.58),
  name = "2008",
  type = "bar", marker = list(color = toRGB("black")))
p2