我有一个图表,我在其中比较工厂的劳动时间和平均值。数据来自两个不同的集合,我希望“Plant A”的散点图颜色与“Plant A”的平均线颜色相同(等等)。
我了解到我必须将颜色分配给植物名称,因为 Highcharter 颜色坐标按时间顺序排列,并且数据始终按时间排序,而不是植物名称。
但是,当我这样做时,我可以获得散点图的正确颜色,但线条不会采用相同的颜色。线上的标记会,但不是物理线。(此外,图例似乎根本没有进行颜色分配)。
以下是我的问题的可重现示例:
library (ggplot2)
library (date)
library (tidyr)
library (dplyr)
library (data.table)
library (highcharter)
Plant.Name <- c("PlantB","PlantA","PlantC","PlantA","PlantA","PlantA","PlantA","PlantB","PlantB","PlantB","PlantC","PlantC","PlantC","PlantC")
Date <- c("1/1/2018","1/1/2018","1/1/2018","1/1/2018","1/2/2018","1/2/2018","1/2/2018","1/2/2018","1/3/2018","1/3/2018","1/3/2018","1/4/2018","1/4/2018","1/4/2018")
Time <- c(5,6,4,3,6,5,2,1,4,3,1,3,5,2)
Ship_data <- data.frame(cbind(Plant.Name,Date,Time))
Ship_data$Plant.Name <- as.character(Ship_data$Plant.Name)
Ship_data$Time <- as.numeric(as.character(Ship_data$Time))
Ship_data$Date <- as.Date(as.character(Ship_data$Date))
Ship_data <- mutate(Ship_data, coloract = ifelse(Plant.Name == "PlantA", "#A1CAF1",
ifelse(Plant.Name == "PlantB", "#BE0032",
ifelse(Plant.Name == "PlantC", "#C2B280",
"#000000"
))))
Plant.Name <- c("PlantA","PlantA","PlantA","PlantA","PlantB","PlantB","PlantB","PlantB","PlantC","PlantC","PlantC","PlantC")
Date <- c("1/1/2018","1/2/2018","1/3/2018","1/4/2018","1/1/2018","1/2/2018","1/3/2018","1/4/2018","1/1/2018","1/2/2018","1/3/2018","1/4/2018")
Average <- c(2,2,2,2,3,3,3,3,4,4,4,4)
Avg_data <- data.frame(cbind(Plant.Name,Date,Average))
Avg_data$Plant.Name <- as.character(Avg_data$Plant.Name)
Avg_data$Average <- as.numeric(as.character(Avg_data$Average))
Avg_data$Date <- as.Date(as.character(Avg_data$Date))
Avg_data <- mutate(Avg_data, coloract = ifelse(Plant.Name == "PlantA", "#A1CAF1",
ifelse(Plant.Name == "PlantB", "#BE0032",
ifelse(Plant.Name == "PlantC", "#C2B280",
"#000000"
))))
hc <- highchart() %>%
hc_chart(zoomType = "x") %>%
hc_add_series(data = Ship_data, hcaes(x=Date, y = Time, group = Plant.Name, color = coloract),
type = "scatter", marker = list(radius = 2)) %>%
hc_add_series(data = Avg_data, hcaes(x=Date, y = Average, color = coloract, group = Plant.Name),
type = "line", marker = list(enabled = FALSE)) %>%
hc_yAxis(min = 0, title = list(text = "Labor Hours")) %>%
hc_xAxis(type = "datetime", dateTimeLabelFormats = list(day = '%b %d %Y')) %>%
hc_tooltip(valueDecimals = 1)
hc
更新:找到了一个可行的解决方案,但它不是最有说服力的解决方案。希望有人在那里有更好的人。我添加了“PlantD”,因为我的正常代码是动态的,并非所有植物都可能根据用户输入显示。
library (ggplot2)
library (date)
library (tidyr)
library (dplyr)
library (data.table)
library (highcharter)
Plant.Name <- c("PlantB","PlantA","PlantC","PlantA","PlantA","PlantA","PlantA","PlantB","PlantB","PlantB","PlantC","PlantC","PlantC","PlantC")
Date <- c("1/1/2018","1/1/2018","1/1/2018","1/1/2018","1/2/2018","1/2/2018","1/2/2018","1/2/2018","1/3/2018","1/3/2018","1/3/2018","1/4/2018","1/4/2018","1/4/2018")
Time <- c(5,6,4,3,6,5,2,1,4,3,1,3,5,2)
Ship_data <- data.frame(cbind(Plant.Name,Date,Time))
Ship_data$Plant.Name <- as.character(Ship_data$Plant.Name)
Ship_data$Time <- as.numeric(as.character(Ship_data$Time))
Ship_data$Date <- as.Date(as.character(Ship_data$Date))
Plant.Name <- c("PlantA","PlantA","PlantA","PlantA","PlantB","PlantB","PlantB","PlantB","PlantC","PlantC","PlantC","PlantC")
Date <- c("1/1/2018","1/2/2018","1/3/2018","1/4/2018","1/1/2018","1/2/2018","1/3/2018","1/4/2018","1/1/2018","1/2/2018","1/3/2018","1/4/2018")
Average <- c(2,2,2,2,3,3,3,3,4,4,4,4)
Avg_data <- data.frame(cbind(Plant.Name,Date,Average))
Avg_data$Plant.Name <- as.character(Avg_data$Plant.Name)
Avg_data$Average <- as.numeric(as.character(Avg_data$Average))
Avg_data$Date <- as.Date(as.character(Avg_data$Date))
hc <- highchart() %>%
hc_chart(zoomType = "x") %>%
hc_yAxis(min = 0, title = list(text = "Labor Hours")) %>%
hc_xAxis(type = "datetime", dateTimeLabelFormats = list(day = '%b %d %Y')) %>%
hc_tooltip(valueDecimals = 1)
if (nrow(Ship_data[Ship_data$Plant.Name == "PlantA",]) == 0) {} else {
hc <- hc %>%
hc_add_series(data = Ship_data[Ship_data$Plant.Name == "PlantA",], hcaes(x=Date, y = Time, group = Plant.Name),
type = "scatter", marker = list(radius = 2),color = "#A1CAF1") %>%
hc_add_series(data = Avg_data[Avg_data$Plant.Name == "PlantA",], hcaes(x=Date, y = Average, group = Plant.Name),
type = "line", marker = list(enabled = FALSE),color = "#A1CAF1")}
if (nrow(Ship_data[Ship_data$Plant.Name == "PlantB",]) == 0) {} else {
hc <- hc %>%
hc_add_series(data = Ship_data[Ship_data$Plant.Name == "PlantB",], hcaes(x=Date, y = Time, group = Plant.Name),
type = "scatter", marker = list(radius = 2),color = "#BE0032") %>%
hc_add_series(data = Avg_data[Avg_data$Plant.Name == "PlantB",], hcaes(x=Date, y = Average, group = Plant.Name),
type = "line", marker = list(enabled = FALSE),color = "#BE0032")}
if (nrow(Ship_data[Ship_data$Plant.Name == "PlantC",]) == 0) {} else {
hc <- hc %>%
hc_add_series(data = Ship_data[Ship_data$Plant.Name == "PlantC",], hcaes(x=Date, y = Time, group = Plant.Name),
type = "scatter", marker = list(radius = 2),color = "#C2B280") %>%
hc_add_series(data = Avg_data[Avg_data$Plant.Name == "PlantC",], hcaes(x=Date, y = Average, group = Plant.Name),
type = "line", marker = list(enabled = FALSE),color = "#C2B280")}
if (nrow(Ship_data[Ship_data$Plant.Name == "PlantD",]) == 0) {} else {
hc <- hc %>%
hc_add_series(data = Ship_data[Ship_data$Plant.Name == "PlantD",], hcaes(x=Date, y = Time, group = Plant.Name),
type = "scatter", marker = list(radius = 2),color = "#604E97") %>%
hc_add_series(data = Avg_data[Avg_data$Plant.Name == "PlantD",], hcaes(x=Date, y = Average, group = Plant.Name),
type = "line", marker = list(enabled = FALSE),color = "#604E97")}
hc