在同一张图中绘制函数和条形图时出现轴问题

时间:2018-12-19 20:47:23

标签: r plot

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

axes issue

我已经尝试过放     轴=假。 虽然效果不佳...

这是我的代码:

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)

有人知道如何通过重叠轴来避免此问题吗?谢谢。

2 个答案:

答案 0 :(得分:1)

尝试这样的事情:

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

这样,您只需要叠加直线而不是整个图形即可。

enter image description here

为全面披露,我只回答了您有关绘图的问题。分布似乎有些时髦。您的模拟中可能有问题吗?

答案 1 :(得分:-1)

问题在于,通常该行必须遵循小节图,在此情况并非如此。我不理解为什么,因为在正常法律中,最大值应为x = m,而在此m = 36中而不是25 ..

我认为这两个(线和条形图)没有相同的x轴。您对此有什么解决方案吗?谢谢。

相关问题