highcharter - y轴上的类别

时间:2018-03-09 22:15:29

标签: r highcharts r-highcharter

我很难过!我试图在highcharter中使用y轴上的类别进行简单的散点图,但我无法弄明白。

以下是我的代码:

library(highcharter)

LoginTrackerAggHourly <- aggregate(LoginTracker$X1, by = list(LoginTracker$Time,LoginTracker$Username),FUN=sum)
colnames(LoginTrackerAggHourly)[which(names(LoginTrackerAggHourly) == "Group.1")] <- "Time"
colnames(LoginTrackerAggHourly)[which(names(LoginTrackerAggHourly) == "Group.2")] <- "Username"
colnames(LoginTrackerAggHourly)[which(names(LoginTrackerAggHourly) == "x")] <- "Aggregate"

hc <- highchart() %>%
  hc_chart(zoomType = "x") %>%
  hc_yAxis(type = "category", title = list(text = "Dashboard Visits", style = list(color = "#ffffff")), labels = list(style = list(color = "#ffffff"))) %>%
  hc_xAxis(title = list(text = "Hour of day", style = list(color = "#ffffff")), labels = list(style = list(color = "#ffffff"))) %>%
  hc_add_series(data = LoginTrackerAggHourly, hcaes(x = Time, y = 1, size = Aggregate, group = Username),
                type = "scatter",
                tooltip = list(headerFormat = "<b>Site: {point.Username} <b><br>",
                               pointFormat = "Visits: {point.Aggregate} on Hour {point.Time}"))

hc

它目前设置为没有类别y = 1,但是当我在y = Username中更改hc_add_series时,它没有绘制。我在type = "category"中有hc_yAxis。不知道为什么它不起作用!

以下是dput输出:

dput(LoginTracker)
structure(list(X1 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), Username = c("Maastricht", 
"Monroe", "Monroe", "Monroe", "Singapore", "Singapore", "Singapore", 
"Singapore", "Singapore", "Singapore", "Maastricht", "Maastricht", 
"Maastricht", "Maastricht", "Maastricht", "Monroe", "Maastricht", 
"Maastricht", "Central", "Maastricht", "Maastricht", "Monroe", 
"Central", "Central", "Monroe", "Monroe", "Central", "Phoenix", 
"Maastricht", "Maastricht", "Maastricht", "Temp", "Monroe", "Monroe", 
"Central", "Temp", "Central", "Singapore", "Singapore", "Singapore", 
"Singapore", "Singapore", "Singapore", "Maastricht", "Maastricht", 
"Maastricht", "Maastricht", "Maastricht", "Singapore", "Maastricht", 
"Monroe", "Maastricht", "Maastricht", "Maastricht", "KammererJ"
), Date = structure(c(-712889, -712889, -712889, -712889, -712889, 
-712888, -712888, -712888, -712888, -712888, -712888, -712888, 
-712888, -712888, -712888, -712888, -712888, -712888, -712888, 
-712888, -712888, -712888, -712888, -712888, -712888, -712888, 
-712888, -712888, -712887, -712887, -712887, -712887, -712887, 
-712887, -712887, -712887, -712887, -712887, -712887, -712887, 
-712887, -712887, -712887, -712886, -712886, -712886, -712886, 
-712886, -712886, -712886, -712886, -712886, -712886, -712886, 
-712886), class = "Date"), Time = c("11", "14", "15", "15", "22", 
"01", "01", "02", "02", "02", "02", "03", "03", "04", "07", "07", 
"08", "09", "11", "11", "12", "14", "15", "15", "15", "15", "16", 
"17", "03", "05", "05", "07", "07", "09", "13", "16", "16", "19", 
"19", "19", "20", "20", "20", "02", "02", "02", "02", "02", "04", 
"04", "09", "10", "11", "11", "12")), .Names = c("X1", "Username", 
"Date", "Time"), row.names = c(3L, 5L, 10L, 11L, 13L, 14L, 15L, 
16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 26L, 29L, 30L, 33L, 
39L, 41L, 43L, 45L, 46L, 48L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 
62L, 71L, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 
85L, 86L, 87L, 88L, 90L, 91L, 93L, 94L, 98L), class = "data.frame")

1 个答案:

答案 0 :(得分:0)

LoginTrackerAggHourly <- aggregate(LoginTracker$X1, by = list(LoginTracker$Time,LoginTracker$Username),FUN=sum)
colnames(LoginTrackerAggHourly)[which(names(LoginTrackerAggHourly) == "Group.1")] <- "Time"
colnames(LoginTrackerAggHourly)[which(names(LoginTrackerAggHourly) == "Group.2")] <- "Username"
colnames(LoginTrackerAggHourly)[which(names(LoginTrackerAggHourly) == "x")] <- "Aggregate"

LoginTrackerAggHourly$Username <- as.factor(LoginTrackerAggHourly$Username)
LoginTrackerAggHourly <- mutate(LoginTrackerAggHourly, typenum = as.numeric(LoginTrackerAggHourly$Username) -1)
lvls <- LoginTrackerAggHourly %>% pull(Username) %>% levels()

max <- length(lvls) - 1

hc <- highchart() %>%
  hc_chart(zoomType = "x") %>%
  hc_yAxis(categories = lvls, title = list(text = "Dashboard Visits", style = list(color = "#ffffff")), labels = list(style = list(color = "#ffffff")), min = 0, max = max) %>%
  hc_xAxis(title = list(text = "Hour of day", style = list(color = "#ffffff")), labels = list(style = list(color = "#ffffff")), step = 1) %>%
  hc_add_series(data = LoginTrackerAggHourly, hcaes(x = Time, y = typenum, size = Aggregate, group = Username),
                type = "scatter",
                tooltip = list(headerFormat = "<b>Site: {point.Username} <b><br>",
                               pointFormat = "Visits: {point.Aggregate} on Hour {point.Time}"))