2016-08-20 18 views
1

私はしばしば自分自身がこのやって見つける:ggplot:データフレーム内の値のプロット頻度カウント(前処理なしで)

# 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を使用して行うことができますか?頻度数については

答えて

4

、あなたがcountとしてgeom_pointstatパラメータを指定することができます。

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

enter image description here

+0

をグレート、ありがとう!正規化された頻度(確率)はどうですか? – alberto

+1

'stat_summary'を使った方が良い解決法があるかもしれませんが、私は手前でデータを準備するほうがずっと簡単です。次のようなものがあります: 'ggplot(data.frame(prop.table(table(df.test)))、aes(x = df.test、y = Freq))+ geom_point()'。 – Psidom

関連する問題