I would like to know how could I create a highchart chart that would contain all the observations for each id on y-axis for the dataset(df) like this:
id time value
1 6:00 1
1 12:00 0
1 18:00 0
1 0:00 1
2 6:00 1
2 12:00 1
2 18:00 1
2 0:00 0
3 6:00 0
3 12:00 0
3 18:00 0
3 0:00 1
I am able to create a chart showing the observations for individual ids, but I would like to combine it.
The preferred result would be like this:
1| * *
2| * * *
3| *
6:00 12:00 18:00 0:00
Is there any way how to do this? Thank you very much.
I was eventually able to create the required chat by assigning different values to each group, ie [id:1, value:3], [id:2, value;2].
The original df:
id time value
1 6:00 1
1 12:00 0
1 18:00 0
1 0:00 1
2 6:00 1
2 12:00 1
2 18:00 1
2 0:00 0
3 6:00 0
3 12:00 0
3 18:00 0
3 0:00 1
Since the id=1 was supposed to be on the top of the chart, I assigned the highest value to it and went down to the last id with the smallest value.
df$value <- ifelse(df$id == "1", 3, df$value)
df$value <- ifelse(df$id == "2", 2, df$value)
df$value <- ifelse(df$id == "3", 1, df$value)
The updated df:
1 6:00 3
1 12:00 0
1 18:00 0
1 0:00 3
2 6:00 2
2 12:00 2
2 18:00 2
2 0:00 0
3 6:00 0
3 12:00 0
3 18:00 0
3 0:00 1
Each group could then its own column. The idea is to get each id in a separate column, so it could be easily added to the chart as a series. For this subsetting and merge could be used:
df_one <- subset(df, id == "1")
df_one <- select(df_one, time, value)
colnames(df_one) <- c("time", "value1")
df_two <- subset(df, id == "2")
df_two <- select(df_two, time, value)
colnames(df_one) <- c("time", "value2")
df_three <- subset(df, id == "3")
df_three <- select(df_three , time, value)
colnames(df_one) <- c("time", "value3")
df_final <- merge(df_one, df_two, by = "time", all.x = TRUE)
df_final <- merge(df_final, df_three, by = "time", all.x = TRUE)
The resulting final df looks like this:
time value1 value2 value3
6:00 3 2 0
12:00 0 2 0
18:00 0 2 0
0:00 3 0 1
This could be then be added to a chart:
hc <- highchart() %>%
hc_chart(type = "scatter") %>%
hc_xAxis(categories = df$time) %>%
hc_add_series(data = c(df$value1),
name = "Id: 1"
) %>%
hc_add_series(data = c(df$value2),
name = "Id: 2"
) %>%
hc_add_series(data = c(df$value3),
name = "Id: 3"
) %>%
hc_exporting(enabled = TRUE)
hc
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.