adR
adR

Reputation: 325

How to plot Radar chart

Would you please show me how to do Radar plot with the following dataset. In this dataset I want to see the distribution of fut column by the value total and categorized by a variable called Group . Here is the simulation data set

maxmin <- data.frame(
      total=c(5, 1,5,7),
      phys=c(15, 3, 9,3),
      psycho=c(3, 0,3,5),
      social=c(5, 1,4,6),
      env=c(5, 1,5,2),
      fut = c("am", "ber", "am", "ber"),
      group = rep(c("I", "M"), each = 2))

Thanks for your help! Amare

Upvotes: 0

Views: 656

Answers (1)

matushiq
matushiq

Reputation: 66

You can use fmsb package.

library(fmsb)
library(dplyr)
# Group names needs to be as row names of data.frame
x <- maxmin %>% 
  mutate(name = paste(fut, group, sep = "_")) %>%
  select(-c(fut, group))
df <- as.data.frame(x %>% select(-c(name)))
rownames(df) <- x$name
# max & min needs to be added as first and second row, respectively
df <- rbind(rep(20,5) , rep(0,5) , df)
par(mar=rep(0.8,4))
par(mfrow=c(1,2))
radarchart(df[c(1:2, 3:4), ], title = "I")
radarchart(df[c(1:2, 5:6), ], title = "M")

resulting graph

EDIT: Fol All in one:

## All in one
library(fmsb)
library(dplyr)
# Group names needs to be as row names of data.frame
x <- maxmin %>% 
  mutate(name = paste(fut, group, sep = "_")) %>%
  select(-c(fut, group))
df <- as.data.frame(x %>% select(-c(name)))
rownames(df) <- x$name
# max & min needs to be added as first and second row, respectively
df <- rbind(rep(20,5) , rep(0,5) , df)

radarchart(df,
           pcol = c("steelblue", "steelblue", "tomato", "tomato"), # colors define Group
           plty = c(1, 2, 1, 2), # line type defines fut
           plwd = 2) # line width
# Add a legend
legend(x=0.7, y=1, legend = rownames(df[-c(1,2),]), bty = "n", pch=20 , col = c("steelblue", "steelblue", "tomato", "tomato"), text.col = "grey", cex=1.1, pt.cex=2)

All in one

ADDED: An example with new dataset: For radarchart you need to have data.frame with variables in columns and groups in rows. Thus for your specific dataset data you need to create 3 separate plots (FClog, pvalue, count). Here is the example for FClog:

#Example with new dataset "data"
library(fmsb)
library(dplyr)
library(tidyr)
set.seed(100) # you may want to set this for reproducibility example
data <- data.frame(da = c("total", "phys","psycho","social","fut", "total","phys", "psycho","social","fut"), logFC = runif(10, -1, 1), pvalue = runif(10, max= 0.05, min= 0.001), count = runif(10,max = 20, min = 12), group = rep(c("WT","KO"), each = 5))

x1 <- data %>% select(da, logFC, group) %>%
  pivot_wider(names_from = group, values_from = logFC) %>%
  as.data.frame()
rownames(x1) <- x1[,1]
x1 <- x1[,2:ncol(x1)]
x1 <- x1 %>%
  t() %>%
  as.data.frame()

# max & min needs to be added as first and second row, respectively
df <- rbind(rep(1,ncol(x1)) , rep(-1,ncol(x1)) , x1)
par(mar=rep(0.8,4))
radarchart(df,
           title = "FClog",
           pcol = c("steelblue", "tomato"), # colors define Group
           plty = c(1, 2), # line type defines fut
           plwd = 2) # line width
# Add a legend
legend(x=0.7, y=1, legend = rownames(df[-c(1,2),]), bty = "n", pch=20 , col = c("steelblue", "tomato"), text.col = "grey", cex=1.1, pt.cex=2)

enter image description here

Upvotes: 1

Related Questions