[英]How to change the default colors in plotly chart?
我正在创建一个绘制图形的条形图,并根据我的数据集更改的过滤器。
数据集如下所示:
tabNew <- structure(list(Group = c("2016-11", "2016-12", "2017-01", "2017-
02", "2017-03"),
`Did Not Meet Expectations` = c(3, 0.8, 1.5, 0.8, 1.7),
`Exceeded Expectations` = c(45, 50.6, 32.3, 49.5, 55.6),
`Met Expectations` = c(51.2, 48.5, 66.2, 49.5, 42.4),
Unacceptable = c(0.7, 0, 0, 0.1, 0.2)),
.Names = c("Group", "Did Not Meet Expectations",
"Exceeded Expectations", "Met Expectations", "Unacceptable"),
row.names = c(NA, -5L), class = "data.frame")
绘制图表的代码如下:
x <- list(
title = "Time"
)
y <- list(
title = "Percent"
)
p <- plot_ly(tabNew, x = ~Group, y = ~`Unacceptable`, colors = c("red", "yellow", "green", "blue"),
name = 'Unacceptable', type = 'scatter', mode = 'lines') %>%
add_trace(y = ~`Did Not Meet Expectations`, name = 'Did Not Meet Expectations') %>%
add_trace(y = ~`Met Expectations`, name = 'Met Expectations') %>%
add_trace(y = ~`Exceeded Expectations`, name = 'Exceeded Expectations') %>%
layout(xaxis = x, yaxis = y)
该图表如下:
此数据集是Group
表示Months
的示例。 有时基于过滤器, Group
可以表示Quarters
,在这种情况下,可能不会有所有其他列。 因此,我们有可能只是Did Not Meet Expectations
, Exceeded Expectations
和Met Expectations
。
在任何一种情况下,我都不想要默认颜色。 我希望Unacceptable
如果它显示为Red
, Did Not Meet Expectations
如果可用显示为Yellow
,同样Met Expectations
Blue
和Exceeded Expectations
为Green
。 有没有办法指定这个顺序?
require(plotly)
df <- data.frame(
Group = c("2016-11", "2016-12", "2017-01", "2017-02", "2017-03"),
DidNot = c(3, 0.8, 1.5, 0.8, 1.7),
Exceeded = c(45, 50.6, 32.3, 49.5, 55.6),
Met = c(0.7, 0, 0, 0.1, 0.2),
Unacceptable = c(0.7, 0, 0, 0.1, 0.2)
)
plot_ly(df, x = ~Group) %>%
add_trace(y = ~Exceeded,
name = "Exceeded Expectations",
type = "scatter",
mode = "lines",
line = list(color = "green")) %>%
add_trace(y = ~Unacceptable,
name = "Unacceptable",
type = "scatter",
mode = "lines",
line = list(color = "red")) %>%
layout(xaxis = list(title = "Time"),
yaxis = list(title = "Percent"))
此代码生成:
我修改了你的数据帧语法,以便我更整洁,我显然没有为你绘制所有的行,但你得到了图片。
p <- plot_ly(tabNew, x = ~Group, y = ~`Unacceptable`,
name = 'Unacceptable', type = 'scatter', mode = 'lines',
line=list(color="red")) %>%
add_trace(y = ~`Did Not Meet Expectations`, name = 'Did Not Meet Expectations',
line=list(color="yellow")) %>%
add_trace(y = ~`Met Expectations`, name = 'Met Expectations',
line=list(color="green")) %>%
add_trace(y = ~`Exceeded Expectations`, name = 'Exceeded Expectations',line=list(color="blue")) %>%
layout(xaxis = x, yaxis = y)
如果您使用的是虹膜数据集
p <- plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length, color = ~Species, , colors=c("Red","Green","Blue"))
另一个选择是以长格式融合data.frame并简单地将标准名称映射到特定颜色(另请参阅R中的散点图和线图 ):
library(plotly)
library(tidyr) ## to melt data.frame
## create long-format data.frame
df <- data.frame(
Group = c("2016-11", "2016-12", "2017-01", "2017-02", "2017-03"),
DidNot = c(3, 0.8, 1.5, 0.8, 1.7),
Exceeded = c(45, 50.6, 32.3, 49.5, 55.6),
Met = c(51.2, 48.5, 66.2, 49.5, 42.4),
Unacceptable = c(0.7, 0, 0, 0.1, 0.2)
) %>%
gather(key = "criterion", value = "measurement", -Group)
## specify color maps
colors <- c("red", "gold", "blue", "green")
names(colors) <- c("Unacceptable", "DidNot", "Met", "Exceeded")
## create plot
plot_ly(df, x = ~Group) %>%
add_lines(y = ~measurement, color = ~criterion, colors = colors) %>%
layout(xaxis = list(title = "Time"),
yaxis = list(title = "Percent")
)
当data.frame中不存在所有列名时,这也应该有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.