How to plot Highcharter side by side in RStudio Viewer?

蹲街弑〆低调 提交于 2020-04-30 06:25:42

问题


I wanted to see an exact output of a Highcharter plot side by side in RStudio Viewer if it possible, exactly showed in this reference: http://jkunst.com/highcharter/highcharts.html, So let me define it like this for a simple usage

highcharter_all_plot <- function(){
  library(highcharter)
  library(dplyr)
  library(stringr)
  library(purrr)
  n <- 5
  set.seed(123)
  colors <- c("#d35400", "#2980b9", "#2ecc71", "#f1c40f", "#2c3e50", "#7f8c8d")
  colors2 <- c("#000004", "#3B0F70", "#8C2981", "#DE4968", "#FE9F6D", "#FCFDBF")

  df <- data.frame(x = seq_len(n) - 1) %>% 
    mutate(
      y = 10 + x + 10 * sin(x),
      y = round(y, 1),
      z = (x*y) - median(x*y),
      e = 10 * abs(rnorm(length(x))) + 2,
      e = round(e, 1),
      low = y - e,
      high = y + e,
      value = y,
      name = sample(fruit[str_length(fruit) <= 5], size = n),
      color = rep(colors, length.out = n),
      segmentColor = rep(colors2, length.out = n)
    )

  print(head(df))

  create_hc <- function(t) {
    dont_rm_high_and_low <- c("arearange", "areasplinerange",
                              "columnrange", "errorbar")
    is_polar <- str_detect(t, "polar")
    t <- str_replace(t, "polar", "")
    if(!t %in% dont_rm_high_and_low){
      df <- df %>% dplyr::select(-e, -low, -high)
    } 
    highchart() %>%
      hc_title(text = paste(ifelse(is_polar, "polar ", ""), t),
               style = list(fontSize = "15px")) %>% 
      hc_chart(type = t,
               polar = is_polar) %>% 
      hc_xAxis(categories = df$name) %>% 
      hc_add_series(df, name = "Fruit Consumption", showInLegend = FALSE) 
  }

  hcs <- c("line", "spline",  "area", "areaspline",
           "column", "bar", "waterfall" , "funnel", "pyramid",
           "pie" , "treemap", "scatter", "bubble",
           "arearange", "areasplinerange", "columnrange", "errorbar",
           "polygon", "polarline", "polarcolumn", "polarcolumnrange",
           "coloredarea", "coloredline")  %>% map(create_hc) 
  return(hcs)
}

x <- highcharter_all_plot()

#Then plot can be accessed in by calling x[[1]], x[[2]], x[[3]]..

As far as my understanding of side by side plot, I only know of 2 these handy methods, which is:

1) Using par(mfrow)

par(mfrow=c(3,4)) -> (which only can by applied to base plot)

2) Using grid.arrange from gridExtra

library(gridExtra)
grid.arrange(x[[1]], x[[2]], x[[3]], x[[4]], nrow=2, ncol=2)

-> (Cannot work since x not a ggplot type)

So I wanted to know if there is a way that this can be applied? I am new using Highcharter


回答1:


If you inspect the Highcharter website you provided, you will see that those charts are not sided by side using R, but they are just renderer in separate HTML containers and positioned by bootstrap (CSS). So, if you want to render your charts in an HTML environment, I suggest rendering every chart into a separate div.

But maybe Shiny is a tool you are looking for. Maybe this is a duplicate of Shiny rcharts multiple chart output

Maybe this will help you too: https://github.com/jbkunst/highcharter/issues/37



来源:https://stackoverflow.com/questions/61463631/how-to-plot-highcharter-side-by-side-in-rstudio-viewer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!