R ggplot:数据帧中值的绘图频率计数(无预处理)

R ggplot:数据帧中值的绘图频率计数(无预处理),r,ggplot2,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 fr

我经常发现自己在这样做:

# 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)
<代码>#原始数据
df.test对于频率计数,您可以将
geom_point
中的
stat
参数指定为
count

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

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

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

太好了,谢谢!关于标准化频率(概率)呢?使用
stat\u summary
可能会有更好的解决方案,但我发现事先准备数据要容易得多。类似于:
ggplot(data.frame(prop.table)(table(df.test))、aes(x=df.test,y=Freq))+geom_point()
。太好了,谢谢!关于标准化频率(概率)呢?使用
stat\u summary
可能会有更好的解决方案,但我发现事先准备数据要容易得多。类似于:
ggplot(data.frame(prop.table)(table(df.test))、aes(x=df.test,y=Freq))+geom_point()