0

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

8
  • 1
    The fact that your first row is full of characters and the rest of your data are numbers is going to cause you a massive headache. Fix this first. The easiest way is probably to apply the fix during the import (e.g. read.csv(file="myfile.csv", header=TRUE).
    – DanY
    Commented Nov 7, 2018 at 20:30
  • This is actually what I've produced from unstructured data. Is there not a way I could make a box plot from the numeric data and attach the title somehow?
    – Sid Jones
    Commented Nov 7, 2018 at 20:41
  • Remove the titles from your dataframe, store them in a separate vector (let's call it namevec), and then make your boxplot like this: boxplot(df, names=namevec).
    – DanY
    Commented Nov 7, 2018 at 20:46
  • Sorry, I will try that, but I have just made the column headers the titles. Does this make it easier? Picture above
    – Sid Jones
    Commented Nov 7, 2018 at 20:52
  • Check that your data are numeric with str(df). If so, then just do boxplot(df).
    – DanY
    Commented Nov 7, 2018 at 20:53

3 Answers 3

5
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

2
  • I can't seem to get this working, what should I replace 'variable' and 'value' with? I have posted my exact data frame which I simplified
    – Sid Jones
    Commented Nov 7, 2018 at 20:27
  • variable and value are actually correct; they are just the default names provided by melt() when you reshape your data Commented Nov 7, 2018 at 21:02
2

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")
1
  • I get the error Error in oldClass(stats) <- cl : adding class "factor" to an invalid object - I have posted a screenshot of my exact data frame which I simplified
    – Sid Jones
    Commented Nov 7, 2018 at 20:26
0
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]])
})
1
  • While this might answer the authors' question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer. Commented Nov 8, 2018 at 0:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.