-1

I have a data frame which looks like this below. I have the variable ToF.Freq1_Hit1 , ToF.Freq1_Hit2, ToF.Freq1_Hit3 .... and so on till ToF.Freq20_Hit5. ( So 20 Freq and 5 hits each). The Data frame is already melted using melt().

I am trying to plot mean and sd for each freq. I tried the below but it is really cluttered. Any ideas on how to improve this.

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

Is there a way within ggplot to split the variables as ToF.Freq1 : 20 and the Hits separate. ?

Many thanks for putting up with this.

stochastiker
  • 25
  • 1
  • 11
  • Can you add a sample of your data? – cmaher Jul 27 '17 at 19:00
  • 2
    Don't post picture of data. See [how to create a reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). What exactly do you mean by "really cluttered"? What exactly do you want the desired output to be? – MrFlick Jul 27 '17 at 19:00

3 Answers3

1

You can do this:

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

Facet_grid does the graph by each of those categorical fields stored within your "variable" field.

skhan8
  • 121
  • 4
0

Maybe something like that?

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

Sample of data would be usefull.

Piotr
  • 153
  • 7
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)
Megan.Zzz
  • 29
  • 5