【问题标题】:Creating highchart line graph with Date and time使用日期和时间创建 highchart 折线图
【发布时间】:2018-06-01 19:25:36
【问题描述】:

我想用我的my data file 创建一个简单的高线图线图。 所以首先我更改了我的数据,以便 R 识别日期和时间。

library("tidyverse")
library("ggplot")
library("highcharter")
Moisture_kurokawa <- read_csv("~/Data/Moisture kurokawa.csv")

 mutate(Moisture_kurokawa, 
   timestamp = lubridate::mdy_hms(sprintf("%s %s", Date, Time))) %>% 
     select(-Date, -Time) %>% 
      gather(W, value, -timestamp) -> moisture_long

所以在此之后我尝试创建一个简单的高图,所以我尝试了

hchart(moisture_long, "line", hcaes(x = timestamp, y = value, group = W))

我得到了图表,但它说没有要显示的数据。任何帮助将不胜感激。

谢谢。

结果图片:

【问题讨论】:

  • 不应该是ggplot2吗?另外,你的 mutate 命令给我一个错误:Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘select’ for signature ‘"tbl_df"’ In addition: Warning message: 24 failed to parse.

标签: r ggplot2 highcharts tidyverse


【解决方案1】:

尝试将timestamp 转换为字符:

mutate(Moisture_kurokawa, 
 timestamp = lubridate::mdy_hms(sprintf("%s %s", Date, Time))) %>% 
 select(-Date, -Time) %>% 
 gather(W, value, -timestamp) -> moisture_long

moisture_long$timestamp <- as.character(moisture_long$timestamp)
hchart(moisture_long, type="line", hcaes(x=timestamp, y=value, group = W))

更好的解决方案是:

library("tidyverse")
library("highcharter")
Moisture_kurokawa <- read_csv("./Moisture kurokawa.csv") %>%
  na.omit() %>%
  mutate(timestamp = lubridate::mdy_hms(sprintf("%s %s", Date, Time)))

hc <- highchart(type="stock")
for (k in names(Moisture_kurokawa)[3:7]) {
  hc <- hc_add_series_times_values(hc=hc, dates=Moisture_kurokawa$timestamp, 
            values=pull(Moisture_kurokawa, k), name = k)
}
hc

【讨论】:

  • 桑德里非常感谢。我刚开始学习highchart。我不明白这一行“for (k in names(Moisture_kurokawa)[3:7])”。如果你有时间请告诉我这条线的用法
  • @MrGoodNews names(Moisture_kurokawa)[3:7] 是带有要绘制的变量名称的向量(W3W5 等)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-05
  • 2022-04-15
  • 1970-01-01
相关资源
最近更新 更多