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)
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'm just trying to plot three curves on a chart, which all must start at (0,1) and end at (30, 0.51). The problem is the curves seems to start at about (2,1) and end at (31,0.51). My code is:
require(graphics)
library(latex2exp)
Linear<-c( 1,0.983666667,0.967333333,0.951,0.934666667,0.918333333,0.902,0.885666667,
0.869333333,0.853,0.836666667,0.820333333,0.804,0.787666667,0.771333333,
0.755,0.738666667,0.722333333,0.706,0.689666667,0.673333333,0.657,0.640666667,
0.624333333,0.608,0.591666667,0.575333333,0.559,0.542666667,0.526333333,0.51)
SqRt<-c( 1 , 0.910538649 , 0.873482544 , 0.845048395 , 0.821077298 ,
0.799958338 , 0.780865338 , 0.763307513 , 0.746965088 , 0.731615947 ,
0.717098368 , 0.703290265 , 0.690096789 , 0.677442512 , 0.665266275 ,
0.653517677 , 0.642154596 , 0.6311414 , 0.620447632 , 0.610047011 ,
0.599916675 , 0.590036587 , 0.580389069 , 0.570958432 , 0.561730676 ,
0.552693245 , 0.543834825 , 0.535145184 , 0.526615026 , 0.518235881 , 0.51 )
CubeRt<-c( 1 , 0.842124513 , 0.801135304 , 0.772387515 , 0.749504067 , 0.730181506 ,
0.71329249 , 0.698190887 , 0.684467813 , 0.671846185 , 0.660128536 ,
0.649168592 , 0.638854625 , 0.629099142 , 0.619832193 , 0.610996874 ,
0.602546197 , 0.59444086 , 0.586647616 , 0.579138061 , 0.571887715 ,
0.56487531 , 0.558082241 , 0.551492126 , 0.545090459 , 0.538864322 ,
0.532802159 , 0.526893586 , 0.521129232 , 0.515500609 , 0.51 )
g_range <- range(0, Linear, SqRt,CubeRt,na.rm = TRUE)
plot(Linear, type="l",lwd=2, col="blue", ylim=g_range,
axes=FALSE, ann=FALSE,lty=1)
axis(1, at=c(0,5,10,15,20,25,30))
axis(2, las=1)
box()
lines(SqRt, type="l", pch=22, lty=1,lwd=2, col="red")
lines(CubeRt, type="l", pch=24, lty=1,lwd=2, col="black")
title(xlab="mmh/RH", col.lab=rgb(0,0,0))
title(ylab="Availability", col.lab=rgb(0,0,0))
plot_colors <- c("blue","red", "black")
text <- c("Linear", "Square Root", "Cube Root")
legend(x=14, y=1.0, legend=text, fill=plot_colors, ncol=3, xpd=NA)
and the plot looks like this.
If you don't supply both x and y values, R will just start plotting 1, 2, ..., to the total number of values you provide. If you want to start at zero, it's better to pass in the x values explicitly. For example
plot(x=0:30, y=Linear, type="l",lwd=2, col="blue", ylim=g_range,
axes=FALSE, ann=FALSE,lty=1)
lines(x=0:30, y=SqRt, type="l", pch=22, lty=1, lwd=2, col="red")
lines(x=0:30, y=CubeRt, type="l", pch=24, lty=1, lwd=2, col="black")
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]
)
}
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/