R - violin plot with varying columns
dataframe, graph, math, plot, r
Solution
You need to remove the NA's which precludes you from having a `data.frame` (unequal length columns) as your container data structure, but you also want to use `do.call` which takes a list. Therefore I would use `lapply` to strip the values from each column of the data.frame that are `NA`s because each one will be returned as a list element and you can still use `do.call` (assume your data is called `df`):
do.call( vioplot, lapply(df, function(x) x[!is.na(x)]) )
Or as @BrianDiggs points out, you could use the even more succinct and pretty:
do.call(vioplot, lapply(df, na.omit))
Problem
I am looking for a way to plot a violin plots with many violins (columns). The issue is that my columns all vary in lengths. For example, it's something like this: ``` "V1" "V2" "V1" 9 255.5 "V2" 432 286 "V3" 161 322.5 "V4" 320.5 277 "V5" 253.5 153.5 "V6" 301 155.5 "V7" 113 218.5 "V8" 341 394 "V9" 138 93.5 ........ "V38166" 62 152 "V38167" NA 20.5 "V38168" NA 12 "V38169" NA 40.5 "V38170" NA 88 "V38171" NA 2.5 "V38172" NA 279.5 "V38173" NA 161.5 "V38174" NA 14.5 ``` As you can see, there are some NA's in the first column as there are fewer entries. Keep in mind that there might be more columns as well. The question is, can I have a violin plot with NA's in any of the columns? I've tried this: ``` jpeg("violinplot.jpg", width = 1000, height = 1000); do.call(vioplot,c(statsDataFrame, list(names=nameList))) dev.off() ``` statsDataFrame being the full data frame I've posted above. When I run the script, however, I get the following error: ``` Error in quantile.default(data, 0.25) : missing values and NaN's not allowed if 'na.rm' is FALSE Calls: do.call -> <Anonymous> -> quantile -> quantile.default Execution halted ``` which essentially is complaining about the NA's. I've tried both na.rm = FALSE and na.rm = TRUE like so: ``` jpeg("stats/AllDistanceViolinPlot.jpg", width = 1000, height = 1000); do.call(vioplot,c(columnViolinDistanceDataUnlist,na.rm=FALSE,list(names=tfListRow))) dev.off() ``` and ``` jpeg("stats/AllDistanceViolinPlot.jpg", width = 1000, height = 1000); do.call(vioplot,c(columnViolinDistanceDataUnlist,na.rm=TRUE,list(names=tfListRow))) dev.off() ``` but to no avail. Does anyone have any suggestions as to how to do this or if it can be done? Thank you for your help.