0

I am sorry for my basic question. I am new to R and trying to draw a box plot with the following data.

boxplot.csv

group1  group2  group3
5.18    7        4.18
4.61    7.5      3.52
3.3     4.5      1.5
4.56    7.58     3.39
3        4       2.5
3.8     4.67     3.43
1.95    3.5      1
2.67    3        2.6
2.77    3.5      2.17

I can draw the box plot with the following code.

df = read.csv ("/home/bud/Desktop/boxplot.csv")
boxplot(df, col=c("red","blue","green"),main = "my first boxplot", ylim=c(0,10),ylab = "marks")

But I would like to get the same box plot with ggplot2. How can I do that?

1
  • Welcome to SO. You can probably start by looking at this link. Otherwise, you can find information if you google the topic. Commented Feb 28, 2016 at 4:28

1 Answer 1

3

put data in long format

library(reshape2)
df <- melt(df)

And then simply

ggplot(data=df, aes(x=variable, y=value, fill=variable)) + 
  geom_boxplot()

You can add layers to define how you want the plot to look, e.g.

ggplot(data=df, aes(x=variable, y=value, fill=variable)) + 
  geom_boxplot() + 
  theme_bw() +
  labs(x="Group", y="Marks")

to remove the legend you can use either

guides(fill=FALSE)   
## use to turn off one or more legends, 
## depending on your `aes` values. 
## In this example we are only using the `fill` argument

or

theme(legend.position="none")
## removes legend from graph
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer. I don't need legends. How can I remove that?
I appreciate your help!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.