It is possible to simplify the code, especially the lines where the cyl_ # columns are created. This is an example code. But the real need is to create 12 columns. And make the boxplot graphs for each column. It is a priority to use the data.table package
my code:
library(vioplot)
library(data.table)
dt=setDT(copy(mtcars))[,.(mpg,cyl)][]
dt[,mpg,keyby=cyl]
dt[cyl == 4,cyl_4:=mpg]
dt[cyl == 6,cyl_6:=mpg]
dt[cyl == 8,cyl_8:=mpg]
dt[,.SD,.SDcols=patterns("cyl_")]
> dt[,vioplot(.SD,col="gold"),.SDcols=patterns("cyl_")]
upper lower median q1 q3
1: 33.900 21.400 26.0 22.80 30.40
2: 21.400 17.800 19.7 18.65 21.00
3: 19.025 11.625 15.2 14.40 16.25
CodePudding user response:
Using data.table for dynamical columns creation (which is possibly not necessary):
cyls <- sort(unique(dt$cyl))
dt[,(paste0("cyl_",cyls)):=lapply(cyls,function(x) fifelse(x == cyl,mpg,NA))]
dt[,vioplot(.SD,col="gold"),.SDcols=patterns("cyl_")]
Or with ggplot2:
library(ggplot2)
ggplot(dt) geom_violin(aes(x=paste0('cyl_',cyl),y=mpg),fill='gold')
xlab('cylinders')
CodePudding user response:
I am not sure why the OP is asking to create individual columns for each value of cyl (with many NA values in each column) but if the goal is to create a violin plot for each individual value of cyl this is what I would do:
library(vioplot)
library(data.table)
as.data.table(mtcars)[, vioplot(mpg ~ cyl, col="gold")]
upper lower median q1 q3 <num> <num> <num> <num> <num> 1: 33.900 21.400 26.0 22.80 30.40 2: 21.400 17.800 19.7 18.65 21.00 3: 19.025 11.625 15.2 14.40 16.25
This approach uses the formula parameter of vioplot(). There is no need for filtering or manipulating the mtcars data (except for coercing to class data.table).
If it is required to rename the labels on the x-axis this can be achieved by
as.data.table(mtcars)[, vioplot(mpg ~ paste0("cyl_", cyl), xlab = NULL, col = "gold")]
The formula parameter is quite handy as it allows for switching easily between different variables, e.g.,
as.data.table(mtcars)[, vioplot(hp ~ gear, col = "lightblue")]
upper lower median q1 q3 <num> <num> <num> <num> <num> 1: 245 97 180 150.00 210 2: 123 52 94 65.75 110 3: 335 91 175 113.00 264






