2016-02-28 91 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