如何在RStudio Viewer中并排绘制Highcharter? [英] How to plot Highcharter side by side in RStudio Viewer?

查看:207
本文介绍了如何在RStudio Viewer中并排绘制Highcharter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果可能的话,我想在RStudio Viewer中并排查看Highcharter图的确切输出,并在此参考文献中精确显示: http://jkunst.com/highcharter/highcharts.html ,所以让我像这样简单定义它

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]]..

据我对并排图的理解,我只知道以下2种方便的方法,即:

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

1)使用par(mfrow)

1) Using par(mfrow)

par(mfrow=c(3,4))->(只能应用于基本图)

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

2)从gridExtra使用grid.arrange

2) Using grid.arrange from gridExtra

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

->(由于x不是ggplot类型而无法使用)

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

所以我想知道是否可以应用此方法?我是使用Highcharter的新手

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

推荐答案

如果检查提供的Highcharter网站,您会发现这些图表不是使用R并排显示的,它们只是在单独的HTML容器和由引导程序(CSS)定位.因此,如果您想在HTML环境中呈现图表,建议将每个图表呈现到一个单独的div中.

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.

但是也许 Shiny 是您正在寻找的工具.也许这是 Shiny rcharts多个图表输出

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

也许这也会对您有帮助: https://github.com/jbkunst/highcharter/Issues/37

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

这篇关于如何在RStudio Viewer中并排绘制Highcharter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆