簡體   English   中英

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM