2017-07-27 111 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)