基于原始数据创建幂律分布图

时间:2014-02-17 16:15:21

标签: r plot ggplot2 power-law

所以,我有一个原始数据,如果图表,应该形成一个幂律分布。我不太确定如何平滑图表。我可以在Excel中完成它,但我想在R中完成它 我有一个2列的数据框。一个称为频率,另一个称为比例。 频率是文档中使用的单词的频率。比例是百分比。所以我想在X轴上绘制频率,在Y轴上绘制比例。 我试过barplot和ggplot。

调整空间后,条形图看起来很完美。但由于某种原因,我只能在Y轴上显示数字,并且不能使数字显示在X轴上。

ggplot不是那么顺利。

如果我将绘图转换为密度图,它将改变Y轴上的测量值。

如何绘制X和Y,并保留所有测量标签?

barplot(height=speech$proportion,width=speech$frequency,density=FALSE,space=10,border="green",xlab="Speech Frequency", ylab="Percentage of Words")

enter image description here

和ggplot

ggplot(speech,aes(x=speech$frequency,y=speech$proportion))+geom_bar(stat="identity",fill="green",colour="green") + xlab("Speech Frequency") +ylab("Proportion")

enter image description here

这就是它在excel中的样子,这就是我想要的。 enter image description here

1 个答案:

答案 0 :(得分:0)

使用条形图更改x轴上的标签非常繁琐。为此,我通常使用 gridBase 包。

CODE:

# 1: generating some mockup data
speech = data.frame(frequency=c(500,250,125,75,20,10,5,3,1,1,1),proportion=c(c(500,250,125,75,20,10,5,3,1,1,1)/100))
# 2: calling barplot with filled bars and with space=0 (no space between bars)
midpts=barplot(height=speech$proportion,col="green",space=0,border="green",xlab="Speech Frequency", ylab="Percentage of Words")
# 3: loading gridBase, and using it to generate the x-axis labels
library(gridBase)
vps <- baseViewports()
pushViewport(vps$inner, vps$figure, vps$plot)
grid.text(speech$frequency, x = unit(midpts, "native"), y=unit(-0.5, "lines"), just="right", rot=90)

结果:

barplot in R with x labels

相关问题