【发布时间】:2011-10-18 09:24:00
【问题描述】:
我有来自已处理的维基百科语料库的单词的频率和排名。只需一行 x(单词排名)和 y(频率)数字,并希望 R 中的对数图如下所示:http://en.wikipedia.org/wiki/File:Wikipedia-n-zipf.png
我该怎么做?我不断得到颠倒或不正确的版本。谢谢。
【问题讨论】:
我有来自已处理的维基百科语料库的单词的频率和排名。只需一行 x(单词排名)和 y(频率)数字,并希望 R 中的对数图如下所示:http://en.wikipedia.org/wiki/File:Wikipedia-n-zipf.png
我该怎么做?我不断得到颠倒或不正确的版本。谢谢。
【问题讨论】:
仅使用基本功能:
plot(x, y, log="xy")
这将在对数刻度上绘制您的点。
【讨论】:
使用lattice 和latticeExtra:
library(lattice)
library(latticeExtra)
xyplot((1:200)/20 ~ (1:200)/20, type = c('p', 'g'),
scales = list(x = list(log = 10), y = list(log = 10)),
xscale.components=xscale.components.log10ticks,
yscale.components=yscale.components.log10ticks)
更多示例here。
【讨论】:
通过获取单词的频率和排名,您已经完成了艰苦的工作。您只需要将它们绘制在对数刻度上。
##Word frequencies in Moby dick
dd = read.csv("http://tuvalu.santafe.edu/~aaronc/powerlaws/data/words.txt")
##Rename the columns and add in the rank
colnames(dd) = "freq"
dd$rank = 1:nrow(dd)
##Plot using base graphics
plot(dd$rank, dd$freq, log="xy")
或者你可以使用ggplot2
require(ggplot2)
ggplot(data=dd, aes(x=rank, y=Freq)) +
geom_point() + scale_x_log10() +
scale_y_log10()
【讨论】: