我试图将雷达图存储到对象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函数,所以使用base plot。不能将基本绘图写入对象,但可以
如果你想要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
显然你需要使用格式
OR如果你想使用基数R:
par(mfrow=c(3,1))将把每个新图形输出到一个3 × 1的网格中…但远不如ggplot
可控将2个地块并排放置
这是对你的"最终目标"的回答,而不是对你的问题的回答。
为什么不用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)