2016-02-28 66 views
0

我很抱歉我的基本问题。我是R新手,试图用以下数据绘制箱形图。箱形图与ggplot2

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相同的盒子图。我怎样才能做到这一点?以长格式

+0

欢迎这样的传说。你可以先看看[这个链接](http://docs.ggplot2.org/current/geom_boxplot.html)。否则,你可以找到信息,如果你谷歌的话题。 – jazzurro

回答

3

将数据

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 
+0

谢谢你的回答。我不需要传说。我该如何删除它? – bud

+0

@bud我已更新我的回答 – SymbolixAU

+0

我感谢您的帮助! – bud