【问题标题】:Creating power law distribution chart based on raw data根据原始数据创建幂律分布图
【发布时间】:2014-02-17 16:15:20
【问题描述】:

所以,我有一个原始数据,如果绘制成图表,应该形成幂律分布。我不确定如何平滑图表。我可以在 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")

和ggplot

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

这就是它在 excel 中的样子,这就是我想要的。

【问题讨论】:

    标签: r plot ggplot2 power-law


    【解决方案1】:

    使用 barplot 更改 x 轴上的标签很繁琐。为此,我通常使用 gridBase 包。

    代码:

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

    结果:

    【讨论】:

      猜你喜欢
      • 2011-03-16
      • 1970-01-01
      • 1970-01-01
      • 2011-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-07
      • 1970-01-01
      相关资源
      最近更新 更多