在R中为同一个表中的两列并排绘制框图

时间:2016-05-13 11:22:12

标签: r boxplot

我有以下数据:

structure(list(osc = c(14944966.1544549, 21761557.221199, 22468349.3727696, 
25347942.08608, 23753968.4211987, 21988336.4211988, 24782010.0211988, 
23466022.821199, 24862768.4211988, 24753030.8211989, 6290574.27199998, 
6347336.50713587, 6133022.93504007, 6096474.45708794, 6310948.70553584, 
6730668.06553585, 7041845.44716779, 6834310.72460792, 7099525.34016, 
7427605.81836809, 6489665.40799994, 3923620.15923189, 5597911.8079998, 
5246583.80800014, 5770297.40799987, 5486879.80800004, 6084583.80800003, 
6719183.80800007, 6575468.60799991, 10992555.0079998), phone = c(14012229.0213694, 
21428112.3570197, 21387319.7062893, 23452910.4634375, 23561326.6405997, 
21295405.7333708, 24791075.2993385, 23077156.3984319, 24595401.2681341, 
24576961.6364291, 6436497.31657422, 6273285.60788477, 5998908.36035547, 
5837113.11736719, 6138679.45996679, 6229959.76306446, 6790623.7462578, 
6752280.48147853, 6625959.55551369, 6979249.86094727, 6642155.1844375, 
3965572.43175781, 5462667.62250977, 5232575.67903125, 5466322.91543554, 
5380533.88633204, 5814654.14943164, 6482157.46073438, 6269535.74795312, 
10938578.8058379), type = c("local", "local", "local", "local", 
"local", "local", "local", "local", "local", "local", "up", "up", 
"up", "up", "up", "up", "up", "up", "up", "up", "down", "down", 
"down", "down", "down", "down", "down", "down", "down", "down"
)), .Names = c("osc", "phone", "type"), row.names = c(NA, 30L
), class = "data.frame")

如何获得一个包含总共6个箱图的图,其中数据将首先按类型分组,然后对于每种类型,将有osc列的箱线图和phone列的箱图。 {1}}栏?

4 个答案:

答案 0 :(得分:4)

这是使用reshape的选项:

library(reshape2)
df2 <- melt(df, id.vars="type")
op <- par(ps=12, mar=c(5,4,1,1))
boxplot(value ~ variable + type, df2, las=2, col=c(5:6))
par(op)

enter image description here

答案 1 :(得分:3)

require(ggplot2)
require(tidyr)
td<-tidyr::gather(d,key=type)
names(td)<-c("type","osc_local","value")
ggplot(data=td,aes(y=value,x=type,fill=osc_local))+geom_boxplot()

希望这会有所帮助!![enter image description here] 1

答案 2 :(得分:0)

你在找这样的东西吗?

par(mfcol=c(1,2))
boxplot(phone ~ type, data=d, main="phone")
boxplot(osc ~ type, data=d, main="osc")

答案 3 :(得分:0)

 par(mfrow = c(1,2))
 boxplot(dataframe$osc ~ dataframe$type , main = "phone")
 boxplot(dataframe$phone ~ dataframe$type , main = "osc")