【问题标题】:data plotted in ggplot to highcharter在 ggplot 中绘制的数据到 highcharter
【发布时间】:2019-03-03 23:34:11
【问题描述】:

我有这个数据框

df <- data.frame(x = 1:8,
                 y1 = c(1:2, NA, NA,3:4, NA, NA),
                 y2 = c(NA, NA,3:4, NA, NA, 5:6), 
                 type = c("A", "A","A","A","B","B","B","B"))

#df
x y1 y2 type
1  1 NA    A
2  2 NA    A
3 NA  3    A
4 NA  4    A
5  3 NA    B
6  4 NA    B
7 NA  5    B
8 NA  6    B

我可以使用 ggplot2 以我想要的方式绘制它:

ggplot(df, aes(x = x, y = y1, col = type))+ 
      geom_line()+
      geom_line(aes(y=y2), linetype=3)

它看起来像这样

我想使用 R highcharter 包制作相同的情节,但我无法弄清楚。谢谢您的帮助。

【问题讨论】:

    标签: r ggplot2 r-highcharter


    【解决方案1】:
    library(tidyverse)
    library(highcharter)
    
    highchart() %>%
      hc_xAxis(categories = df$x) %>%
      hc_xAxis(title = list(text = "x",
                            style = list(
                              fontSize = "16px", 
                              fontWeight = "bold",
                              color = "black"))) %>% 
      hc_yAxis(title = list(text = "y",
                            style = list(
                              fontSize = "16px", 
                              fontWeight = "bold",
                              color = "black"))) %>% 
      hc_add_series(df, "line", hcaes(x, y1, color = type),
                    name = "y1") %>%
      hc_add_series(df, "line", hcaes(x, y2, color = type),
                    dashStyle = "DashDot",
                    name = "y2") 
    

    有一些很好的例子herehere

    【讨论】:

      猜你喜欢
      • 2021-03-23
      • 2019-12-11
      • 2012-10-17
      • 2017-05-26
      • 2020-11-25
      • 2016-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多