Sid Jones
Sid Jones

Reputation: 59

Creating a boxplot for each column in R

I have a dataframe like:

       X1    X2   X3 ...

Title  One  Two   Three

X1    0     10     19
X2    4     20      3
X3   17     39      3
..     

I would like to create a Boxplots with the title of it being 'Title' (one, two or three) and the corresponding data in each column being used. So, I want a Boxplot for each column. How can I do this?

The Y-axis is X1, X2... (in the leftmost column) (which should just be 1, 2..) and the x-axis is the Title.

My data frame

Data frame with columns headers

Upvotes: 0

Views: 8815

Answers (3)

G. Cocca
G. Cocca

Reputation: 2541

df <- data.frame(
 'One'   = c( 0,  4, 17), 
 'Two'   = c(10, 20, 39),
 'Three' = c(19,  3,  3))

lapply(seq_along(df), function(x){
 boxplot(df[[x]], main = paste("Title", colnames(df))[[x]])
})

Upvotes: 0

DanY
DanY

Reputation: 6073

In base R:

df <- data.frame(
     'One'   = c( 0,  4, 17), 
     'Two'   = c(10, 20, 39),
     'Three' = c(19,  3,  3))

boxplot(df, main="My Title")

Upvotes: 2

12b345b6b78
12b345b6b78

Reputation: 1015

library(reshape2)
library(ggplot2)

x <- data.frame('One' = c(0, 4, 17), 'Two' = c(10, 20, 39), 'Three' = c(19, 3, 3))
x <- melt(x)

plt <- ggplot(data = x, aes(x = variable, y = value))
plt + geom_boxplot() + theme_minimal() + labs(x = "Title", y = "x")

enter image description here

Upvotes: 5

Related Questions