R highcharter中的DataLabels打印为png或jpg后看不到

问题描述 投票:0回答:1

我正在尝试打印我的图表图表。

library(highcharter)
webshot::install_phantomjs()
colors_ <- colorize(1:6, c("#FFA500", "#000000"))
df <- data.frame(y = round(rnorm(5, 10, 2), digits = 1),
                 name = paste0("Name", c(1:5)),
                 color = colors_[1:5])

hc <- highchart() %>%
  hc_chart(type = "column") %>%
  hc_xAxis(categories = df$name) %>%
  hc_add_series(
    df,
    dataLabels = list(
      enabled = T,
      shadow = F,
      color = "black",
      style = list(
        textShadow = F,
        textOutline = F,
        fontWeight = 'normal',
        opacity = 1
      )
    )
  ) 

htmlwidgets::saveWidget(widget = hc, file = "hc.html")
webshot::webshot(url = "hc.html", file = "hc.png", delay = 1, zoom = 4, vheight = 500)

这是查看器中的HTML图表:enter image description here

这是png或jpg:enter image description here

有标签,但非常透明。我尝试了不同的风格。没有成功。你能帮忙吗?

installed.packages():highcharter,htmlwidgets,webshot ...

r highcharts htmlwidgets
1个回答
0
投票

而不是使用webshot,您应该考虑在不受此问题困扰的https://github.com/rstudio/webshot2上尝试webshot2。我已使用webshot2复制了您的方案,问题已解决,如下图所示。

注意:在尝试安装webshot2软件包之前,请不要忘记删除websot。为了删除它,请转到Rstudio右下角的Packages,搜索该包的名称,然后单击相邻的X图标将其删除,或者您可以通过Rstudio控制台以这种方式进行处理:

remove.packages("webshot", lib="~/R/win-library/3.6")

代码

library(highcharter)
library(webshot2)

colors_ <- colorize(1:6, c("#FFA500", "#000000"))
df <- data.frame(y = round(rnorm(5, 10, 2), digits = 1),
                 name = paste0("Name", c(1:5)),
                 color = colors_[1:5])

hc <- highchart() %>%
  hc_chart(type = "column") %>%
  hc_xAxis(categories = df$name) %>%
  hc_add_series(
    df,
    dataLabels = list(
      enabled = T,
      shadow = F,
      color = "black",
      style = list(
        textShadow = F,
        textOutline = F,
        fontWeight = 'normal',
        opacity = 1
      )
    )
  ) 

htmlwidgets::saveWidget(widget = hc, file = "hc.html")
webshot(url = "hc.html", file = "hc.png", delay = 1, zoom = 4, vheight = 500)

png文件(hc.png)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.