r - 使用 ggplot 在一个图中绘制多个直方图
问题描述
我正在尝试用 R 在一个图中绘制多个直方图。我在一个循环中用“plot”和“line”绘制了图,但我想用 ggplot 绘制图并使用函数 geom_step()。
在这里,我的代码:
png(file = "MismatchAll_300_test.png", width = 1500, height = 1000, pointsize = 30)
# Calcul correction
##########################################################
for (i in 1:length(fileNames1)) {
TablS1 <- as.data.frame(DataS1[i], col.names = "") #DataS1 is a vector of 300 datasets
Hist <- hist(TablS1$modif1, breaks = seq(-0.025, 0.25, by=0.003), plot=F)
mismatch1 <- data.frame(Hist$counts)
mismatch1 <- transform(mismatch1, NbDiff = Hist$mids)
mismatch1 <- transform(mismatch1, FreqS = round(prop.table(Hist.counts)*100, digits = 3))
mismatch1 <- mismatch1[,-1]
#Plot with "plot"
if (i==1) {
plot(mismatch1, type="l", ylim=c(0,15), col="grey", main=expression('TCMD for '*italic(Epi)*' = 10, '*italic(m)*' = 0.0024 and '*italic(Kmax)*' = 250'),
xlab = "Time corrected pairwise differences (d̆)", ylab = "Frequency (%)")
}
lines(mismatch1, col="grey")
# Plot with ggplot
if (i==1){
p <- ggplot(mismatch1, aes(NbDiff, y = value, color = Legend)) +
geom_step(aes(y = FreqS, col = "Simulation"), size=1) +
labs(y="Frequency (%)", x="Differences") +
#geom_vline(aes(xintercept=piS1, col="Simulation"), size=1) +
#geom_vline(aes(xintercept=piD, col="Data"), size=1) +
theme(legend.position = "right", legend.key.size = unit(2, "line"),
legend.text = element_text(size=25, face="bold"), legend.title = element_blank(),
axis.title.x = element_text(face="bold", size=25, vjust = -0.5), axis.text.x = element_text(size=25),
axis.title.y = element_text(face="bold", size=25, vjust = 0.5), axis.text.y = element_text(size=25))
print(p)
}
g <- p + geom_step(aes(x= mismatch1$NbDiff, y = mismatch1$FreqS, col = "Simulation"), size=1)
print(g)
}
lines(DataD, col="red", lwd=2)
lines(DataS, col="black", lwd=2)
dev.off()
这是我用“情节”得到的照片。
但是,我不能用 ggplot 做同样的情节。有人可以帮我在循环中用 ggplot 做一个类似的图吗?
谢谢您的帮助。
解决方案
推荐阅读
- mysql - 如何使用 Laravel、MySql、JSON 按不同城市对订单进行分组
- heroku - Heroku - 应用程序在使用时可以回收吗?
- r - 在 R 中将表格绘制为 grob
- c - 查找最长的不重复子数组
- javascript - 在添加新过滤器之前重置过滤器
- c# - 我的 addforce 没有在应有的地方工作
- reactjs - 材质 UI:使用 onMouseLeave 道具时弹出窗口中断
- php - 如何在控制器中使用 laravel Auditor
- c++ - Divide et impera 数组错误的元素的总和
- c++ - 在 WM_CREATE 中加载多个位图的 C++ Win32 不会加载