1

I was trying to store a radar plot into an object p1 but every time I got a result of null. I tried other ggplot2 plots and they all worked fine of being put into objects. My ultimate intention is to use the patchwork to put one radar plot and one line plot side by side. Any suggestion?

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
codedancer
  • 1,504
  • 9
  • 20
  • 1
    If you read the help file for `?radarchart`, you can see under Value, `No value is returned.` Under the hood, `radarchart` is using `plot`, which also returns no value. This question is about saving a base plot as an object. Do any of the answers work for you? https://stackoverflow.com/questions/29583849/save-a-plot-in-an-object – Ben Norris Jan 31 '21 at 17:03

2 Answers2

0

Save a plot in an object

radarchart is not a ggplot2 function, so uses base plot. You can't write base plot to an object but can

If you want 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

You would obviously need to play with the formatting

OR if you want to use base R:

par(mfrow=c(3,1)) will output each new figure into a 3 x 1 grid... but far less controllable than ggplot

0

Put 2 plots side by side

This is an answer to your 'ultimate goal', and not to your question.

Why don't you use 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)

Paschalis
  • 11,929
  • 9
  • 52
  • 82