ggplot2 箱线图

box plot with ggplot2

很抱歉我的基本问题。我是 R 的新手,正在尝试使用以下数据绘制箱线图。

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

我可以用下面的代码画出箱线图

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")

但我想得到与ggplot2 相同的箱线图。我该怎么做?

以长格式输入数据

library(reshape2)
df <- melt(df)

然后就

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

您可以添加图层来定义绘图的外观,例如

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

要删除图例,您可以使用

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

theme(legend.position="none")
## removes legend from graph