I want to create similar radar plots from numerous country "data sets" and title each chart accordingly. Can someone help me get the loop right where R recognizes my dataframe? The error I get is:
> Error in is.data.frame(df) : object 'data_i' not found
Code:
data_China=rbind(rep(100,1) , rep(1,100) , data)
data_Indonesia=rbind(rep(100,1) , rep(1,100) , data)
data_India=rbind(rep(100,1) , rep(1,100) , data)
data_Kenya=rbind(rep(100,1) , rep(1,100) , data)
par(mfrow=c(2,2),mar=c(1, 1, 1, 1))
clist <- c("Indonesia", "China", "India", "Kenya")
for (i in clist) {
# Custom the radarChart !
radarchart(data_i , 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="black", caxislabels=seq(0,100,20), cglwd=0.8,
#custom labels
vlcex=0.6 , title="i"
)
}
You can place all the datasets into a list, and iterate over that:
dataList <- list(China = data_China,
Indonesia = data_Indonesia,
India = data_India,
Kenya = data_Kenya)
for (i in 1:length(dataList)) {
radarchart(dataList[[i]] , 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="black", caxislabels=seq(0,100,20), cglwd=0.8,
#custom labels
vlcex=0.6 , title=names(dataList)[i]
)
}
Related
I have generated a radarchart using the following code:
p3 <- radarchart(radar.plot, seg = 5, axislabcol = "grey", caxislabels = seq(0,50,5), pcol=colors_border,
pfcol=scales::alpha(colors_in, 0.5), plwd=2 , plty=1, cglcol = "black", cglty = 1, cglwd = 0.8)
It generates a preview of the plot that I can save in R.
However, I want to use the Cairo package to save in a much better quality.
The problem is that, while Cairo does give me a preview of the graph, I'm unable to save it using ggsave():
ggsave(filename = 'myfilename.tiff', dpi = 300, type = 'cairo',
width = 8, height = 4, units = 'in', path = "mypath")
Here is the mock data present in the radarchart tutorial if anyone wants to reproduce this issue:
# Library
library(fmsb)
# Create data: note in High school for Jonathan:
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)
# Check your data, it has to look like this!
# head(data)
# Custom the radarChart !
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.8
)
I think the issue lies in the fact that I cannot load the plot in a variable, as I usually do with my plot, here p3 returns NULL. The saved plot is the previously loaded plot, that is in fact loaded in p2 variable.
My guess is that the anwser is similar to the answer of this post: Why does ggplot not plot when storing to variable?
Does anyone have any idea how I can resolve or circumvent this issue?
Thank you.
I am referencing this example code to generate a plot that contains multiple subplots with their own titles but having trouble getting the titles of the subplots properly labeled by loop.
For the ease of exposition, I use the 1 by 2 plots environment as an example.
par(mfrow(1, 2))
require(fmsb)
data1=as.data.frame(matrix( sample(2:20, 10, replace=T) , ncol=10))
data2=as.data.frame(matrix( sample(2:20, 10, replace=T) , ncol=10))
colnames(data1)=c("math" , "english" , "biology" , "music" , "R-coding", "data-viz" , "french" , "physic", "statistic", "sport" )
colnames(data2)=c("math" , "english" , "biology" , "music" , "R-coding", "data-viz" , "french" , "physic", "statistic", "sport" )
df1=rbind(rep(20,10) , rep(0,10) , data1)
df2=rbind(rep(20,10) , rep(0,10) , data2)
my.list <- list(df1, df2)
for (i in my.list[[i]]) {
radarchart(i, 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.8
)
}
However, this code block didn't return anything as it requires the input to be a dataframe. Also, if I want to put a title argument in radarchart() by assigning each dataframe a unique name (say, student 1, student 2) and using them as titles for each subplot, how can I achieve that in a loop?
Your code had some errors, here is the expected output:
par(mfrow=c(1, 2)) #debugged
library(fmsb)
data1=as.data.frame(matrix( sample(2:20, 10, replace=T) , ncol=10))
data2=as.data.frame(matrix( sample(2:20, 10, replace=T) , ncol=10))
colnames(data1)=c("math" , "english" , "biology" , "music" , "R-coding", "data-viz" , "french" , "physic", "statistic", "sport" )
colnames(data2)=c("math" , "english" , "biology" , "music" , "R-coding", "data-viz" , "french" , "physic", "statistic", "sport" )
df1=rbind(rep(20,10) , rep(0,10) , data1)
df2=rbind(rep(20,10) , rep(0,10) , data2)
my.list <- list("df1" = df1, "df2" = df2) #name the list's elements
for (i in 1:length(my.list)) { #use i as list indexer, not to call the elements
radarchart(my.list[[i]], axistype=1 , #call the list's elements
#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.8,
#title; calling the list's names
title = names(my.list)[i]
)
}
I have a data set for which I want to create a radar plot
str(trial4)
'data.frame': 4 obs. of 6 variables:
$ Var1 : Factor w/ 4 levels "2009-04-01","2010-04-01",..: 1 2 3 4
$ Arsenic : int 66 8 8 5
$ Fluoride: int 11775 10788 10724 7130
$ Iron : int 103 60 54 46
$ Nitrate : int 927 856 1106 624
$ Salinity: int 24787 23168 20258 18924
My radar plot code executes fine and I get the plot like this
radarchart(trial4,seg=6)
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( trial4 , axistype=1 ,
#custom polygon
pcol=colors_border , pfcol=colors_in , plwd=2 , plty=1,
#custom the grid
cglcol="grey", cglty=1, axislabcol="grey", caxislabels=seq(0,20,5), cglwd=0.8,
#custom labels
vlcex=0.8
)
legend(x=0.7, y=1, legend = rownames(trial4[-c(1,2),]), bty = "n", pch=20 , col=colors_in , text.col = "grey", cex=1.2, pt.cex=3)
As one can see, the problem is the scaling I cannot fit in the plot. I searched online, but it's not clear how I can make the polygon fit inside the grid of the radar plot. I tried adding xlim and ylim:
radarchart(trial4,seg=6)
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( trial4 , axistype=1 ,
#custom polygon
pcol=colors_border , pfcol=colors_in , plwd=2 , plty=1,
#custom the grid
cglcol="grey", cglty=1, axislabcol="grey", caxislabels=seq(0,20,5), cglwd=0.8,
#custom labels
vlcex=0.8,xlim=c(0,2),ylim=c(0,10)
)
legend(x=0.7, y=1, legend = rownames(trial4[-c(1,2),]), bty = "n", pch=20 , col=colors_in , text.col = "grey", cex=1.2, pt.cex=3)
but still I am not able to scale this properly.
Is there any parameter I am missing?.
so here is my data , and the package i am using is fmsb
var1<-c("2009-04-01","2010-04-01","2011-04-01","2012-04-01")
Arsenic<-c(66,8,8,5)
Fluoride<-c(11775,10788,10724,7130)
Iron<-c(103,60,54,46)
Nitrate<-c(927,856,1106,624)
Salinity<-c(24787,23168,20258,18924)
trail4<-as.data.frame(var1,Arsenic,Fluoride,Iron,Nitrate,Salinity)
Is there a way to avoid overlapping p axis labels? Have seen others suggest creating jitter, using direct.labels, etc., but have not found a way to make this work for radarchart{fmsb}
Would be fine with a general pointer toward a radarchart option or another function, but here is some reproducible code anyway:
library(fmsb)
MM<-data.frame(matrix(c(rep(1,35), rep(0,35),
runif(35, 0, 1), runif(35, 0, 1)), nrow=4, ncol=35, byrow=TRUE))
colnames(MM)<-rep('long variable name', 35)
colors_border=c( rgb(0,0,0,0.9), rgb(1,0,0,0.9))
colors_in=c( rgb(0,0,0,0.2), rgb(1,0,0,0.3))
radarchart(MM , axistype=1 ,
pcol=colors_border , pfcol=colors_in , plwd=2 , plty=1,
cglcol="grey", cglty=1, axislabcol="grey", cglwd=0.8,
caxislabels=c(0, 0.25, 0.5, 0.75, 1),
vlcex=0.8,
title='title',
na.itp=FALSE)
(Making the text smaller works, but that would be my last resort).
I personally like the cat's eye visualization of Cumming that superimposes a sampling distribution over a point estimate:
I would also like to do this with the posterior distribution that is obtained by the Scripts of Kruschke (2015):
plotMCMC( mcmcCoda , data=myData ,
compValMu=100.0 , ropeMu=c(99.0,101.0) ,
compValSigma=15.0 , ropeSigma=c(14,16) ,
compValEff=0.0 , ropeEff=c(-0.1,0.1) ,
pairsPlot=TRUE , showCurve=FALSE ,
saveName=fileNameRoot , saveType=graphFileType )
# Set up window and layout:
openGraph(width=6.0,height=8.0*3/5)
layout( matrix( c(2,3,5, 1,4,6) , nrow=3, byrow=FALSE ) )
par( mar=c(3.5,3.5,2.5,0.5) , mgp=c(2.25,0.7,0) )
# Select thinned steps in chain for plotting of posterior predictive curves:
nCurvesToPlot = 20
stepIdxVec = seq( 1 , chainLength , floor(chainLength/nCurvesToPlot) )
# Compute limits for plots of data with posterior pred. distributions
y = data
xLim = c( min(y)-0.1*(max(y)-min(y)) , max(y)+0.1*(max(y)-min(y)) )
xBreaks = seq( xLim[1] , xLim[2] ,
length=ceiling((xLim[2]-xLim[1])/(sd(y)/4)) )
histInfo = hist(y,breaks=xBreaks,plot=FALSE)
yMax = 1.2 * max( histInfo$density )
xVec = seq( xLim[1] , xLim[2] , length=501 )
#-----------------------------------------------------------------------------
# Plot data y and smattering of posterior predictive curves:
histInfo = hist( y , prob=TRUE , xlim=xLim , ylim=c(0,yMax) , breaks=xBreaks,
col="red2" , border="white" , xlab="y" , ylab="" ,
yaxt="n" , cex.lab=1.5 , main="Data w. Post. Pred." )
for ( stepIdx in 1:length(stepIdxVec) ) {
lines(xVec, dt( (xVec-mu[stepIdxVec[stepIdx]])/sigma[stepIdxVec[stepIdx]],
df=nu[stepIdxVec[stepIdx]] )/sigma[stepIdxVec[stepIdx]] ,
type="l" , col="skyblue" , lwd=1 )
}
text( max(xVec) , yMax , bquote(N==.(length(y))) , adj=c(1.1,1.1) )
#-----------------------------------------------------------------------------
histInfo = plotPost( mu , cex.lab = 1.75 , showCurve=showCurve ,
compVal=compValMu , ROPE=ropeMu ,
xlab=bquote(mu) , main=paste("Mean") ,
col="skyblue" )
#-----------------------------------------------------------------------------
histInfo = plotPost( sigma , cex.lab=1.75 , showCurve=showCurve ,
compVal=compValSigma , ROPE=ropeSigma , cenTend="mode" ,
xlab=bquote(sigma) , main=paste("Scale") ,
col="skyblue" )
#-----------------------------------------------------------------------------
effectSize = ( mu - compValMu ) / sigma
histInfo = plotPost( effectSize , compVal=compValEff , ROPE=ropeEff ,
showCurve=showCurve , cenTend="mode" ,
xlab=bquote( ( mu - .(compValMu) ) / sigma ),
cex.lab=1.75 , main="Effect Size" ,
col="skyblue" )
#-----------------------------------------------------------------------------
postInfo = plotPost( log10(nu) , col="skyblue" , # breaks=30 ,
showCurve=showCurve ,
xlab=bquote("log10("*nu*")") , cex.lab = 1.75 ,
cenTend="mode" ,
main="Normality" ) # (<0.7 suggests kurtosis)
Which looks in the end like this for the mean
Is it possible to superimpose the histogram on the mean like Cumming does it?
I've created a function called plotViolin for another application that should do what you want, or at least be easily modifiable to the specifics of your application. You can find the function about half way down the R script linked here: https://osf.io/wt4vf/