Creating a Boxplot for Each Column in R

R: Plot multiple box plots using columns from data frame

You could use the reshape package to simplify things

data <- data.frame(v1=rnorm(100),v2=rnorm(100),v3=rnorm(100), v4=rnorm(100))
library(reshape)
meltData <- melt(data)
boxplot(data=meltData, value~variable)

or even then use ggplot2 package to make things nicer

library(ggplot2)
p <- ggplot(meltData, aes(factor(variable), value))
p + geom_boxplot() + facet_wrap(~variable, scale="free")

Boxplot in for-loop over multiple columns in r

You could go via column numbers:

# random example data as no reproducible example was given
df <- data.frame(
real = sample(1:4, 20, TRUE),
one = runif(20),
two = runif(20),
three = runif(20))
)

# graphics paramaters so we see all at once
par(mfrow = c(3,1), mar = c(2, 2, 1, 1))

# the easiest way is through column numbers
for(column in 2:4)
boxplot(df[[column]] ~ df$real)

how to create multiple boxplots from the same dataframe?

Using ggplot::facet_wrap() makes it easy to make a bunch of panels of a graph from one dataset. However for it to work the variable that defines the different panels has to be in a single column. In this case that means you have to get from the 'wide' format to a 'long' format of your data. For this I suggest tidyr::pivot_longer(). Last point is that you need to treat your label as a factor otherwise you won't get separate boxes as you do in base R because I think that converts the x variable into a factor by default which {ggplot2} won't do (although it will give an informative warning). Finally, if you want to have separate y axes for each plot you can set scales = "free_y" inside facet_wrap().

library(tidyverse)

df <- data.frame(var_1 = c(1,2,3,4,5,6,7,8,9),
var_2 = c(21,23,34,45,23,56,76,54,65),
var_3 = c(6,5,4,3,5,7,3,2,5),
label = c(1,1,1,2,1,2,2,1,2))

df %>%
pivot_longer(-label) %>%
ggplot(aes(factor(label), value)) +
geom_boxplot() +
facet_wrap(vars(name), nrow = 1)

Sample Image

Created on 2022-02-12 by the reprex package (v2.0.1)

How to make a box plot for every column from dataframe using ggplot

I created a dataframe below first.

> data
X X1 X2 X3 X4
X1H 8 2 0 0
X2H 2 0 2 2
X3H 0 2 0 0
X4H 0 0 0 2
X5H 2 0 0 2
X6H 2 0 2 0

Then, reshaped it by using the melt function.

data.melt<-melt(data, id="X")

Given that you wanted to draw a boxplot with your own computations, I calculated mean, sd, min, max for each column

data.sum<-ddply(data.melt, .(variable), summarise,
mean = mean(value),
sd = sd(value),
min = min(value),
max = max(value))

Then, you can create a boxplot for each column with this code below.

ggplot(data.sum, aes(x=variable))+geom_boxplot(aes(ymin =min, lower = mean-sd, middle = mean, upper = mean+sd, ymax =max), stat="identity")

Sample Image

Building a box plot from all columns of data frame with column names on x in ggplot2

You can use stack to transform the data frame:

library(ggplot2)
ggplot(stack(df), aes(x = ind, y = values)) +
geom_boxplot()

Sample Image

Make boxplots of columns in R

You can do it as well using tidyverse

library(tidyverse)
SUS %>%
#create new column and save the row.names in it
mutate(variable = row.names(.)) %>%
#convert your data from wide to long
tidyr::gather("var", "value", 1:7) %>%
#plot it using ggplot2
ggplot(., aes(x = variable, y = value)) +
geom_boxplot()+
theme(axis.text.x = element_text(angle=35,hjust=1))

Sample Image



Related Topics



Leave a reply



Submit