使用单个数据帧生成雷达图

Generating radar plots using single dataframe

我有一个数据框 dat,我用它来生成不同站点的不同天气条件(列)的雷达图(由每行 arc1045、arc1047 ...表示)数据框的前两行添加以指示每个气候条件的最大值和最小值,示例如下 here

        ambient_air_temperature relative_humidy barometric_pressure average_wind_speed particulate_density_2.5 particulate_density_10
1                     85.000000        100.0000            2000.000         160.000000              999.900000             1999.90000
2                    -40.000000          0.0000              10.000           0.000000                0.000000                0.00000
arc1045                9.176667         71.0700            1013.167           4.043333                5.133333               25.16667
arc1047                8.492500         80.9600            1014.000           2.035000                5.600000               25.10000
arc1048                8.477500         76.9875            1012.675           6.842500                6.275000               28.15000
arc1050                8.475000         76.5525            1013.775           6.335000                5.175000               30.20000

然后我按照 here 描述的方法生成要作为标记包含在传单地图上的地块的 URI

library(fmsb)

    makePlotURI <- function(expr, width, height, ...) {
      pngFile <- plotPNG(function() { expr }, width = width, height = height, ...)
      on.exit(unlink(pngFile))
      base64 <- httpuv::rawToBase64(readBin(pngFile, raw(1), file.size(pngFile)))
      paste0("data:image/png;base64,", base64)
    }

rep(makePlotURI(radarchart(dat), 300, 300, bg = "transparent"), 4)

但是,这里每个雷达图都包含多个多边形,每个多边形代表一个站点。我希望每个雷达图都有一个多边形,并在一个站点上可视化天气状况。

这样做相当简单,无需将整个数据帧传递给 radachart() 函数,只需传递一个站点所需的数据即可。

df = data.frame(ambient_air_temp = c(85, -40, 9.176667, 8.492500,  8.477500, 8.475000),
                rel_hum = c(100.0000,0.0000,71.0700, 80.9600, 76.9875,76.5525 ),
                bar_pre = c(2000.000, 10.000, 1013.167, 1014.000, 1012.675, 1013.775),
                ave_ws = c(160,0,4.043333,2.035000,6.842500,6.335000),
                pd2.5 = c(999.9, 0, 5.133333, 5.600000, 6.275000, 5.175000),
                pd10 = c(1999.9, 0, 25.16667, 25.10000, 28.15000, 30.20000))

row.names(df) = c("1","2","arc1045","arc1047","arc1048","arc1050")

library(fmsb)
pltrd = function(idx) radarchart(df[c(1,2,idx),], title = row.names(df)[idx])

由于您的数据位于行 = {3,4,5,6} 中,您可以使用这些行值调用此函数。请注意,rows = {1, 2} 必须是每个变量的 minimum/maximum 值...

pltrd(3)

pltrd(4)

pltrd(5)