Bloodstone Programmer
Bloodstone Programmer

Reputation: 211

Creating boxplot in same graph and scale with two different length of dataset

I have a two set of data with different length. Sameple datatype is:

A=c(423,430,500,460,457,300,325,498,450,453,486,459)
B=c(300,325,356345,378,391,367)

I want to create boxplot for them within a same graph and same scale. I tried it in ggplot2 in R. I also tried default boxplot in R.

boxplot (A~B)

but it showed error. I would like to use ggplot2 in R.

Upvotes: 2

Views: 1835

Answers (1)

AntoniosK
AntoniosK

Reputation: 16121

You have to create a dataset with those 2 vectors and then plot.

library(ggplot2)

A=c(423,430,500,460,457,300,325,498,450,453,486,459)
B=c(300,325,356345,378,391,367)

# create a dataset for each vector
df_A = data.frame(value=A, id="A")
df_B = data.frame(value=B, id="B")

# combine datasets
df = rbind(df_A, df_B)

# create the box plot
ggplot(df, aes(id, value)) + geom_boxplot()

enter image description here

Upvotes: 2

Related Questions