无法将雷达图存储到 R 对象中

Cannot store a radar plot into a R object

我试图将雷达图存储到一个对象中 p1,但每次我得到的结果都是 null。我尝试了其他 ggplot2 图,它们都可以很好地放入对象中。我的最终目的是使用 patchwork 并排放置一个雷达图和一个线图。有什么建议吗?

library(fmsb)
set.seed(99)
data <-as.data.frame(matrix( sample( 2:20 , 10 , replace=T) , ncol=10))
colnames(data) <- c("math" , "english" , "biology" , "music" , "R-coding", "data-viz" , "french" , "physic", "statistic", "sport" )

# To use the fmsb package, I have to add 2 lines to the dataframe: the max and min of each topic to show on the plot!
data <-rbind(rep(20,10) , rep(0,10) , data)


# Custom the radarChart !
par(mar=c(0,0,0,0))
p1 <- radarchart( data, axistype=1, 
                  
                  #custom polygon
                  pcol=rgb(0.2,0.5,0.5,0.9) , pfcol=rgb(0.2,0.5,0.5,0.5) , plwd=4 , 
                  
                  #custom the grid
                  cglcol="grey", cglty=1, axislabcol="grey", caxislabels=seq(0,20,5), cglwd=0.8,
                  
                  #custom labels
                  vlcex=0.6 
)

> p1
NULL

radarchart 不是 ggplot2 函数,因此使用基本图。您不能将基本情节写入对象,但可以

如果你想要ggplot

# Run Once Only
install.packages("devtools")
devtools::install_github("ricardo-bion/ggradar")
# Run rest as usual
library(ggplot2)
library(ggradar)
library(dplyr)
library(scales)
set.seed(99)
mydata <-as.data.frame(matrix( sample( 2:20 , 10 , replace=T) , ncol=10))
colnames(mydata) <- c("math" , "english" , "biology" , "music" , "R-coding", "data-viz" , "french" , "physic", "statistic", "sport" )

# To use the fmsb package, I have to add 2 lines to the dataframe: the max and min of each topic to show on the plot!
mydata <-rbind(rep(20,10) , rep(0,10) , mydata)


p1 <- ggradar(
  mydata[3, ], 
  values.radar = c("0", "10", "20"),
  grid.min = 0, grid.mid = 10, grid.max = 20
)

p1

您显然需要调整格式

或者如果你想使用 base R:

par(mfrow=c(3,1)) 会将每个新图形输出到 3 x 1 网格中......但远不如 ggplot

可控

并排放置 2 个地块

这是对您 'ultimate goal' 的回答,而不是对您问题的回答。

你为什么不使用 par(mfrow)

library(fmsb)
set.seed(99)
data <-as.data.frame(matrix( sample( 2:20 , 10 , replace=T) , ncol=10))
colnames(data) <- c("math" , "english" , "biology" , "music" , "R-coding", "data-viz" , "french" , "physic", "statistic", "sport" )

data <-rbind(rep(20,10) , rep(0,10) , data)

par(mfrow = c(2, 1)) # TWO IMAGES SIDE BY SIZE

# YOUR FIRST PLOT
radarchart( data, axistype=1, 
                  pcol=rgb(0.2,0.5,0.5,0.9), pfcol=rgb(0.2,0.5,0.5,0.5), plwd=4, 
                  cglcol="grey", cglty=1, axislabcol="grey",
                  caxislabels=seq(0,20,5), cglwd=0.8,
                  vlcex=0.6)

# YOUR SECOND PLOT
...... # no need to do anything if it uses base plot (like radarchart)
# if the 2nd plot does not use base plot (i.e., it's from ggplot), then do a
plot(yourSecondPlot)