Skip to content

Instantly share code, notes, and snippets.

@RunyangWang
Created April 28, 2020 21:57
Show Gist options
  • Save RunyangWang/11a20297edd1c777d2e21bc715d8d852 to your computer and use it in GitHub Desktop.
Save RunyangWang/11a20297edd1c777d2e21bc715d8d852 to your computer and use it in GitHub Desktop.
R-radar chart
require(altair)
require(fmsb)
require(dplyr)
#Year 1996, Tier 1
d1996t1 <- as.data.frame(rbind(c(100,100,100,78,76),c(86,100,30,74,77),c(72,100,55,73,66)))
colnames(d1996t1) <- c("GDP" , "Primary School Enrollment" , "Population" , "Urban Population", "Life Expectancy at Birth")
rownames(d1996t1) <- c("US", "Germany", "Russia")
data <- rbind(rep(100,5) , rep(0,5) , d1996t1)
colors_border=c( rgb(0.2,0.5,0.5,0.9), rgb(0.8,0.2,0.5,0.9) , rgb(0.7,0.5,0.1,0.9) )
colors_in=c( rgb(0.2,0.5,0.5,0.4), rgb(0.8,0.2,0.5,0.4) , rgb(0.7,0.5,0.1,0.4) )
radarchart( data , axistype=1 ,
#custom polygon
pcol=colors_border , pfcol=colors_in , plwd=4 , plty=1,
#custom the grid
cglcol="grey", cglty=1, axislabcol="grey", caxislabels=seq(0,100,25), cglwd=0.8,
#custom labels
vlcex=0.8,
title = "Tier 1 Countries in 1996"
)
legend(x=1.7, y=1.2, legend = rownames(data[-c(1,2),]), bty = "n", pch=20 , col=colors_in , text.col = "grey", cex=1.2, pt.cex=3)
#Year 1996, Tier 2
d1996t2 <- as.data.frame(rbind(c(58,92,4,72,78),c(62,100,3,84,79),c(34,89,10,19,53)))
colnames(d1996t2) <- c("GDP" , "Primary School Enrollment" , "Population" , "Urban Population", "Life Expectancy at Birth")
rownames(d1996t2) <- c("Greece", "Sweden", "Kenya")
data <- rbind(rep(100,5) , rep(0,5) , d1996t2)
colors_border=c( rgb(0.2,0.5,0.5,0.9), rgb(0.8,0.2,0.5,0.9) , rgb(0.7,0.5,0.1,0.9) )
colors_in=c( rgb(0.2,0.5,0.5,0.4), rgb(0.8,0.2,0.5,0.4) , rgb(0.7,0.5,0.1,0.4) )
radarchart( data , axistype=1 ,
#custom polygon
pcol=colors_border , pfcol=colors_in , plwd=4 , plty=1,
#custom the grid
cglcol="grey", cglty=1, axislabcol="grey", caxislabels=seq(0,100,25), cglwd=0.8,
#custom labels
vlcex=0.8 ,
title = "Tier 2 Countries in 1996"
)
legend(x=1.7, y=1.2, legend = rownames(data[-c(1,2),]), bty = "n", pch=20 , col=colors_in , text.col = "grey", cex=1.2, pt.cex=3)
#Year 1996, Tier 3
d1996t3 <- as.data.frame(rbind(c(4,38,2,7,47),c(32,100,1,55,77),c(41,100,4,58,71)))
colnames(d1996t3) <- c("GDP" , "Primary School Enrollment" , "Population" , "Urban Population", "Life Expectancy at Birth")
rownames(d1996t3) <- c("Burundi", "Costa Rica", "Ecuador")
data <- rbind(rep(100,5) , rep(0,5) , d1996t3)
colors_border=c( rgb(0.2,0.5,0.5,0.9), rgb(0.8,0.2,0.5,0.9) , rgb(0.7,0.5,0.1,0.9) )
colors_in=c( rgb(0.2,0.5,0.5,0.4), rgb(0.8,0.2,0.5,0.4) , rgb(0.7,0.5,0.1,0.4) )
radarchart( data , axistype=1 ,
#custom polygon
pcol=colors_border , pfcol=colors_in , plwd=4 , plty=1,
#custom the grid
cglcol="grey", cglty=1, axislabcol="grey", caxislabels=seq(0,100,25), cglwd=0.8,
#custom labels
vlcex=0.8 ,
title = "Tier 3 Countries in 1996"
)
legend(x=1.7, y=1.2, legend = rownames(data[-c(1,2),]), bty = "n", pch=20 , col=colors_in , text.col = "grey", cex=1.2, pt.cex=3)
#Year 2012, Tier 1
d12t1 <- as.data.frame(rbind(c(100,93,99,23,81,79),c(92,29,99,100,52,75),c(77,76,100,11,74,70)))
colnames(d12t1) <- c("GDP" , "Tertiary school enrollment", "Primary School Enrollment" , "Population" , "Urban Population", "Life Expectancy at Birth")
rownames(d12t1) <- c("US", "China", "Russia")
data <- rbind(rep(100,6) , rep(0,6) , d12t1)
colors_border=c( rgb(0.2,0.5,0.5,0.9), rgb(0.8,0.2,0.5,0.9) , rgb(0.7,0.5,0.1,0.9) )
colors_in=c( rgb(0.2,0.5,0.5,0.4), rgb(0.8,0.2,0.5,0.4) , rgb(0.7,0.5,0.1,0.4) )
radarchart( data , axistype=1 ,
#custom polygon
pcol=colors_border , pfcol=colors_in , plwd=4 , plty=1,
#custom the grid
cglcol="grey", cglty=1, axislabcol="grey", caxislabels=seq(0,100,25), cglwd=0.8,
#custom labels
vlcex=0.8 ,
title = "Tier 1 Countries in 2012"
)
legend(x=1.7, y=1.2, legend = rownames(data[-c(1,2),]), bty = "n", pch=20 , col=colors_in , text.col = "grey", cex=1.2, pt.cex=3)
#Year 2012, Tier 2
m12t2 <- rbind(c(55,66,99,1,73,78),c(53,52,99,1,57,70),c(43,92,99,1,76,72))
d12t2 <- as.data.frame(m12t2)
colnames(d12t2) <- c("GDP" , "Tertiary school enrollment", "Primary School Enrollment" , "Population" , "Urban Population", "Life Expectancy at Birth")
rownames(d12t2) <- c("Czech", "Kazakhstan", "Belarus")
data <- rbind(rep(100,6) , rep(0,6) , d12t2)
colors_border=c( rgb(0.2,0.5,0.5,0.9), rgb(0.8,0.2,0.5,0.9) , rgb(0.7,0.5,0.1,0.9) )
colors_in=c( rgb(0.2,0.5,0.5,0.4), rgb(0.8,0.2,0.5,0.4) , rgb(0.7,0.5,0.1,0.4) )
radarchart( data , axistype=1 ,
#custom polygon
pcol=colors_border , pfcol=colors_in , plwd=4 , plty=1,
#custom the grid
cglcol="grey", cglty=1, axislabcol="grey", caxislabels=seq(0,100,25), cglwd=0.8,
#custom labels
vlcex=0.8 ,
title = "Tier 2 Countries in 2012"
)
legend(x=1.7, y=1.2, legend = rownames(data[-c(1,2),]), bty = "n", pch=20 , col=colors_in , text.col = "grey", cex=1.2, pt.cex=3)
#Year 2012, Tier 3
d12t3 <- as.data.frame(
rbind(c(39,36,100,1,67,75),
c(41,8,96,2,51,70),
c(70,30,100,18,51,69)))
colnames(d12t3) <- c("GDP" , "Tertiary school enrollment", "Primary School Enrollment" , "Population" , "Urban Population", "Life Expectancy at Birth")
rownames(d12t3) <- c("Tunisia
", "Uzbekistan
", "Indonesia
")
data <- rbind(rep(100,6) , rep(0,6) , d12t3)
colors_border=c( rgb(0.2,0.5,0.5,0.9), rgb(0.8,0.2,0.5,0.9) , rgb(0.7,0.5,0.1,0.9) )
colors_in=c( rgb(0.2,0.5,0.5,0.4), rgb(0.8,0.2,0.5,0.4) , rgb(0.7,0.5,0.1,0.4) )
radarchart( data , axistype=1 ,
#custom polygon
pcol=colors_border , pfcol=colors_in , plwd=4 , plty=1,
#custom the grid
cglcol="grey", cglty=1, axislabcol="grey", caxislabels=seq(0,100,25), cglwd=0.8,
#custom labels
vlcex=0.8 ,
title = "Tier 3 Countries in 2012"
)
legend(x=1.7, y=1.2, legend = rownames(data[-c(1,2),]), bty = "n", pch=20 , col=colors_in , text.col = "grey", cex=1.2, pt.cex=3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment