简体   繁体   English

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

[英]Plot side by side boxplots for two columns from the same table in R

I have the following data: 我有以下数据:

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

How can I get a plot which will include a total of 6 boxplots, where the data will be grouped first by type and then for each type there will a boxplot for the osc column and a boxplot for the phone column? 如何获得一个包含总共6个箱图的图,其中数据将按类型分组,然后对于每种类型,将有一个用于osc列的箱图和一个用于phone列的箱图?

here's an option using reshape : 这是一个使用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)

在此输入图像描述

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

Hope this helps! 希望这可以帮助! ![在此处输入图像说明 ] 1 ] 1

Are you looking for something like this? 你在找这样的东西吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM