r语言 - 多个变量的 ggplot(拆分变量)



>我有一个数据框,如下所示。我有变量ToF.Freq1_Hit1,ToF.Freq1_Hit2,ToF.Freq1_Hit3....以此类推,直到ToF.Freq20_Hit5。(所以 20 次频率和 5 次点击(。数据框已使用melt()熔化。

我正在尝试绘制每个频率的平均值和 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和命中分开。

非常感谢您忍受这一点。

你可以这样做:

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

Facet_grid通过存储在"变量"字段中的每个分类字段来绘制图表。

也许是这样的东西?

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

数据样本将是有用的。

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

最新更新