首页 > 解决方案 > 当我在同一个图中绘制函数和条形图时出现轴问题

问题描述

当我绘制两个图表(一个函数和一个条形图)时,我遇到了轴问题。

轴问题

我已经尝试将轴 = FALSE。虽然效果不好...

这是我的代码:

n = 64
p = 0.5
tailleEchantillon<-1000
m = n * p
sigma = sqrt(n * p * (1 - p))
borne_sup = m+7*sigma
borne_inf = m-7*sigma

x = 0:n
echantillon = rbinom(tailleEchantillon,n,p)

tabEffectifs = NULL
for (i in x)  {tabEffectifs = c(tabEffectifs,length(echantillon[echantillon == i]))}


print("Tableau des effectifs")
print(table(echantillon,deparse.level=2))

texteLegende1<-bquote(p == .(p))
texteTitre1 = ""

barplot(tabEffectifs,las=1,names.arg=x,col="blue",ylim=c(0,(tailleEchantillon/4)),legend.text=texteLegende1,main=texteTitre1,xlab="k",ylab="Effectifs",cex.main=1)

tabFrequences = tabEffectifs/tailleEchantillon



plot(function(x) dnorm(x, m, sigma), borne_inf, borne_sup, legend.text="Courbe loi normale", ylim=c(0,0.10), xlab="x", ylab="prob")
par(new = T)
barplot(tabFrequences,las=1,names.arg=x, col="red", ylim=c(0,0.10),legend.text=texteLegende2,main=texteTitre2,xlab="k",ylab="Densites",cex.main=1)

有人知道我如何通过叠加轴避免这个问题吗?谢谢。

标签: rplot

解决方案


尝试这样的事情:

barplot(tabFrequences,las=1,names.arg=x, col="red", ylim=c(0,0.10))
lines(x, dnorm(x,m, sigma), col ="blue")

这样,您只会叠加线条而不是整个图表。

在此处输入图像描述

为了充分披露,我只回答了你关于绘图的问题。发行版似乎有些时髦。也许你的模拟有问题?


推荐阅读