2017-07-27 102 views
-1

我有一个数据框,如下所示。我有变量ToF.Freq1_Hit1,ToF.Freq1_Hit2,ToF.Freq1_Hit3 ....等等,直到ToF.Freq20_Hit5。 (所以20个频率和每个5个点击)。 data frame。数据框已使用melt()融化。ggplot为多个变量(拆分变量)

我想绘制每个频率的平均值和sd。我尝试了下面的内容,但它确实很混乱。任何想法如何改善这一点。

p4 <- ggplot(B_TOF_melt, aes(x = variable, y = value)) + geom_boxplot() + theme(axis.text.x = element_text(angle = 90)) +ggtitle("Geraete B TOF means")

有ggplot内的方式来分割变量ToF.Freq1:20和命中分开。 ?

非常感谢您接受这一点。

+0

您可以添加数据样本吗? – cmaher

+2

请勿发布数据图片。请参阅[如何创建可重现的示例](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)。 “真正混乱”是什么意思?你想要的产出是什么? – MrFlick

回答

0

你可以这样做:

ggplot (...) + facet_grid(. ~ variable) 

Facet_grid每个存储在您的“变量”字段中的分类字段的做图。

0

也许这样的事情?

library(dplyr) 
library(ggplot2) 
data <-B_TOF_melt %>% group_by(variable) %>% summarize(mean=mean(value), sd=sd(value)) 

ggplot(data, aes(x = variable, y = mean)) + geom_boxplot() 
ggplot(data, aes(x = variable, y = sd)) + geom_boxplot() 

数据样本将是有用的。

+0

是的,我添加了这张照片。 – stochastiker

0
#generating key to mimic your data variable "Freq1_Hit1" 
hit<-rep(1:5,20) 
freq<-rep(1:20,each=5) 
freq_name=paste("freq",freq,sep="") 
hit_name=paste("hit",hit,sep="") 
key=paste(freq_name,"_",hit_name,sep="") #this is equal to your "variable" 
########################################################################### 
y<-unlist(strsplit(key,"_")) #split "variable into two string, convert into vector 
ind1<-seq(1,length(y),by=2) #create odd index that would be use to extract "freq" 
ind2<-seq(2,length(y),by=2) #creaet even index to extract "hit" 
freq2<-y[ind1] #using indexing to create freq2 variable 
hit2<-y[ind2] #useing indexing to create hit2 variable 
your.newdata<-data.frame(your.data, freq2, hit2) #combine data 
########################################################################### 
ggplot(your.newdata, aes(x=...,y=...) + 
geom_boxplot() + facet.grid(. ~ freq2)