R ggplot generates only one boxplot for different x values

Viewed 89

I want to generate a box plot with ggplot. I have generated a dataset with the following code:

names <- c(1,1,1,1,2,2,2,2,3,3,3,3)

data <- c(2,6,8,1,3,6,3,4,3,8,6,5)

frame <- data.frame(data, names)

I would like to have a box plot within one diagram for each value in names. But with the following code I only get a single boxplot:

ggplot(frame, aes(names,data)) + geom_boxplot()

What's my mistake?

Here you can see the diagram that I get: img

2 Answers

Convert names to a factor:

names <- factor(c(1,1,1,1,2,2,2,2,3,3,3,3))

Just you need add one line of code convert names to factors as @Stephane Laurent mentioned.

frame$names <- as.factor(frame$names)

enter image description here

Related