高线图 - 线条和散点图组的特定颜色

时间:2018-03-07 17:33:53

标签: r highcharts r-highcharter

我有一个图表,我将工厂的劳动时间和平均值相互比较。数据来自两个不同的集合,我希望"工厂A"的散点图颜色与"工厂A"的平均线颜色相同(等等)。

我已经知道我必须将颜色分配给植物名称,因为按时间顺序的高图颜色坐标和数据总是按时间排序,而不是植物名称。

然而,当我这样做时,我能够获得散点图的正确颜色,但线条不会采用相同的颜色。线上的标记可以,但不是物理线。 (另外,看起来传说根本没有进行颜色分配。)

以下是我的问题的可重复示例:

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

更新: 找到一个有效的解决方案,但它不是最有说服力的解决方案。希望有人有更好的一个。我添加了一个&#34; PlantD&#34;因为我的普通代码是动态的,并且根据用户输入不能显示所有植物。

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

0 个答案:

没有答案