I am trying to create a line chart with Highcharter in R where the color of the line will be the one that is is at the column COLOR of the dataframe.
The dataframe has at the first column: row_names, different dates that can be the same day more than once. The second column: name, different names that can be the more than once, one per each date. Third column: y, the values And fourth column: COLOR, the color that are per each name different The
df <- structure(list(row_labels = c("2024-03-18", "2024-03-18", "2024-03-18",
"2024-03-18", "2024-03-18", "2024-03-18", "2024-03-18", "2024-03-18",
"2024-03-18", "2024-03-18", "2024-03-18", "2024-03-18", "2024-03-18",
"2024-03-18", "2024-03-19", "2024-03-19", "2024-03-19", "2024-03-19",
"2024-03-19", "2024-03-19", "2024-03-19", "2024-03-19", "2024-03-19",
"2024-03-19", "2024-03-19", "2024-03-19", "2024-03-19", "2024-03-19",
"2024-03-20", "2024-03-20", "2024-03-20", "2024-03-20", "2024-03-20",
"2024-03-20", "2024-03-20", "2024-03-20", "2024-03-20", "2024-03-20",
"2024-03-20", "2024-03-20", "2024-03-20", "2024-03-20", "2024-03-21",
"2024-03-21", "2024-03-21", "2024-03-21", "2024-03-21", "2024-03-21",
"2024-03-21", "2024-03-21", "2024-03-21", "2024-03-21", "2024-03-21",
"2024-03-21", "2024-03-21", "2024-03-21", "2024-03-22", "2024-03-22",
"2024-03-22", "2024-03-22", "2024-03-22", "2024-03-22", "2024-03-22",
"2024-03-22", "2024-03-22", "2024-03-22", "2024-03-22", "2024-03-22",
"2024-03-22", "2024-03-22"), name = c("car", "boat", "bike",
"walk", "truck", "limo", "tuktuk", "plane", "helicopter", "monopedal",
"pedal", "paraplane", "swim", "run", "car", "boat", "bike", "walk",
"truck", "limo", "tuktuk", "plane", "helicopter", "monopedal",
"pedal", "paraplane", "swim", "run", "car", "boat", "bike", "walk",
"truck", "limo", "tuktuk", "plane", "helicopter", "monopedal",
"pedal", "paraplane", "swim", "run", "car", "boat", "bike", "walk",
"truck", "limo", "tuktuk", "plane", "helicopter", "monopedal",
"pedal", "paraplane", "swim", "run", "car", "boat", "bike", "walk",
"truck", "limo", "tuktuk", "plane", "helicopter", "monopedal",
"pedal", "paraplane", "swim", "run"), y = c(25, 24.71, 9.69,
6.46, 2.9, 1.64, 1.34, 0.2, 0.5, 0.51, 1.47, 4.92, 16.59, 4.08,
26.59, 27.87, 9.68, 5.37, 2.55, 1.77, 1.74, 0.33, 0.62, 0.78,
1.39, 3.52, 14.49, 3.3, 25.64, 29.88, 8.73, 3.91, 2.49, 1.08,
2.08, 0.21, 0.46, 0.5, 1.24, 3.49, 16.53, 3.76, 23.12, 29.58,
12.17, 6.15, 1.53, 3.23, 0.81, 0.37, 0.17, 0.87, 0.48, 2.71,
15.89, 2.93, 30.32, 29.01, 7.85, 5.43, 1.58, 2.36, 1.37, 0.58,
0, 0.23, 0.25, 2.72, 15.91, 2.4), COLOR = c("#a2d214", "#228b22",
"#e4010b", "#0055a4", "#E51C55", "#63be21", "#6B1E5E", "#adbe18",
"#0a0a0a", "#0b0b0b", "#0c0c0c", "#0d0d0d", "#0e0e0e", "#0f0f0f",
"#a2d214", "#228b22", "#e4010b", "#0055a4", "#E51C55", "#63be21",
"#6B1E5E", "#adbe18", "#0a0a0a", "#0b0b0b", "#0c0c0c", "#0d0d0d",
"#0e0e0e", "#0f0f0f", "#a2d214", "#228b22", "#e4010b", "#0055a4",
"#E51C55", "#63be21", "#6B1E5E", "#adbe18", "#0a0a0a", "#0b0b0b",
"#0c0c0c", "#0d0d0d", "#0e0e0e", "#0f0f0f", "#a2d214", "#228b22",
"#e4010b", "#0055a4", "#E51C55", "#63be21", "#6B1E5E", "#adbe18",
"#0a0a0a", "#0b0b0b", "#0c0c0c", "#0d0d0d", "#0e0e0e", "#0f0f0f",
"#a2d214", "#228b22", "#e4010b", "#0055a4", "#E51C55", "#63be21",
"#6B1E5E", "#adbe18", "#0a0a0a", "#0b0b0b", "#0c0c0c", "#0d0d0d",
"#0e0e0e", "#0f0f0f")), row.names = c(NA, -70L), class = c("tbl_df",
"tbl", "data.frame"))
What the following code is doing is creating the chart and it assigns the color to random values, not the ones that are at the same row of the df:
hchart(df,
'line',
hcaes(x=row_labels, y=y, group=name),
dataLabels=list(enabled=T)) %>%
hc_colors(color=df$COLOR)
The following code sets the correct color but just to the point not the entire line:
hchart(df4bis, 'line', hcaes(x = row_labels, y = y, group = name,color = COLOR), dataLabels = list(enabled = TRUE))
How can I do it correctly?
One option would be to pass a vector of colors to hc_colors
where the order of the colors is set according to the order of your categories:
library(highcharter)
colors <- df |>
dplyr::distinct(name, COLOR) |>
dplyr::arrange(name) |>
dplyr::pull(COLOR)
hchart(df,
"line",
hcaes(x = row_labels, y = y, group = name),
dataLabels = list(enabled = TRUE)
) %>%
hc_colors(color = colors)