ggplot:绘制数据帧中值的频率计数(没有预处理)

时间:2016-08-20 12:10:08

标签: r ggplot2

我经常发现自己这样做:

# Original data
df.test <- data.frame(value=floor(rexp(10000, 1/2)))

# Compute the frequency of every value
# or the probability
freqs <- tabulate(df.test$value)
probs <- freqs / sum(freqs)

# Create a new dataframe with the frequencies (or probabilities)
df.freqs <- data.frame(n=1:length(freqs), freq=freqs, probs=probs) 

# Plot them, usually in log-log
g <- ggplot(df.freqs, aes(x=n, y = freq)) + geom_point() + 
  scale_y_log10() + scale_x_log10()
plot(g)

enter image description here

可以在不创建中间数据集的情况下使用ggplot来完成吗?

1 个答案:

答案 0 :(得分:4)

对于频率统计,您可以将stat中的geom_point参数指定为count

ggplot(df.test, aes(x = value)) + geom_point(stat = "count") + 
    scale_x_log10() + scale_y_log10()

enter image description here