ggplot:绘制数据帧中值的频率计数(无预处理) [英] ggplot: plot frequency counts of values in a dataframe (with no preprocessing)

查看:212
本文介绍了ggplot:绘制数据帧中值的频率计数(无预处理)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常发现自己这样做:

I often find myself doing this:

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

可以使用 ggplot 不创建中间数据集?

Can it be done just using ggplot without creating an intermediate dataset?

推荐答案

对于频率计数,您可以指定 stat 参数 geom_point 作为 count

For frequency count, you can specify the stat parameter in geom_point as count:

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

这篇关于ggplot:绘制数据帧中值的频率计数(无预处理)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆