0

我正在尝试使用 R 中的 Highchart 包制作一些图。我的图是 qqcomp() 和 ppcomp(),我想使用 highchart 复制它。我可以使用 ggplot2 来做到这一点,但我认为它们在 Highchart 中看起来会更好。

这是我的代码:

library(fitdistrplus)
library(highcharter)
library(ggplot2)
x <- c(164.26,  229.61, 25.07,17.82,111.71,15.33,9849.52,35.8,354.69,255.12,166.36,1371.07,362.58,4212.29,8424.57)

fit.lnorm <- fitdist(x,"lnorm",method="mle", start = NULL)
fit.gamma <- fitdist(x,"gamma", method = "mle", lower = c(0, 0))

qcomp <- qqcomp(list(fit.lnorm, fit.gamma), 
                xlegend = "bottomright",
                xlab = "Cuantiles teóricos",
                ylab = "Cuantiles empíricos",
                fitlty = 1,
                fitcol = c("red1","springgreen2"),
                plotstyle = "ggplot",
                addlegend = FALSE)

pcomp <- ppcomp(list(fit.lnorm, fit.gamma),
                xlegend = "bottomright",
                xlab = "Probabilidades teóricas",
                ylab = "Probabilidades empíricas",
                fitlty = 1,
                fitcol = c("red1","springgreen2"),
                plotstyle = "ggplot",
                addlegend = FALSE)

qcomp <- qcomp + theme_minimal() + ggtitle("Q-Q Plot")
pcomp <- pcomp + theme_minimal() + ggtitle("P-P Plot")
4

1 回答 1

0

您可以从 ggplot 对象中提取几乎所有重新创建绘图所需的信息。尤其是:

数据

qcomp$data
#>          values   ind      sdata
#> 1  6.496176e+00 lnorm   15.32480
#> 2  1.992613e+01 lnorm   17.83156
#> 3  3.769184e+01 lnorm   25.06524
#> 4  6.127904e+01 lnorm   35.78934
#> 5  9.260956e+01 lnorm  111.69299
...

映射

qcomp$mapping
#> Aesthetic mapping: 
#> * `group`  -> `ind`
#> * `colour` -> `ind`
#> * `shape`  -> `ind`
#> * `size`   -> `ind`
#> * `x`      -> `values`
#> * `y`      -> `sdata`

有了上面的内容,我们可以构建一个像这样的高图:

# data to draw the diagonal line
max_val <- max(qcomp$data[,c("values", "sdata")])
diag_data <- data.frame(x = c(0, max_val), y = c(0, max_val))


highchart() %>% 
  hc_plotOptions(
    scatter = list(marker = list(symbol = "circle")),
    line = list(marker = list(enabled = FALSE), enableMouseTracking = FALSE)
  ) %>% 
  hc_add_series(diag_data, "line", color ="gray", hcaes(x, y),
                showInLegend = FALSE) %>% 
  hc_add_series(qcomp$data, "scatter", hcaes(values, sdata, group = ind)) %>% 
  hc_xAxis(title = list(text = qcomp$labels$x)) %>% 
  hc_yAxis(title = list(text = qcomp$labels$y)) %>% 
  hc_title(text = qcomp$labels$title) %>% 
  hc_add_theme(hc_theme_elementary())

在此处输入图像描述

于 2020-12-30T11:02:52.287 回答