代码之家  ›  专栏  ›  技术社区  ›  Moh

ggplot中绘制到highcharter的数据

  •  5
  • Moh  · 技术社区  · 7 年前

    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)
    

    看起来像这样

    ggplot

    我想做同样的情节使用R高宪章包,但我还没有能够计算出来。谢谢你的帮助。

    1 回复  |  直到 7 年前
        1
  •  5
  •   Tung    7 年前
    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") 
    

    有一些很好的例子 here here