【问题标题】:Creating Relative Frequency Table and Graph in R在 R 中创建相对频率表和图表
【发布时间】:2014-03-09 11:13:38
【问题描述】:

下面是数据集。 https://docs.google.com/spreadsheet/ccc?key=0AjmK45BP3s1ydEUxRWhTQW5RczVDZjhyell5dUV4YlE#gid=0

代码:

counts = table(finaldata$satjob, finaldata$degree) barplot(counts, xlab="完成 9-12 年级后的最高学位",col = c("Dark Blue","Blueviolet","deepPink4","goldenrod"), legend =(rownames(counts)))

下面的条形图是上面代码的结果。 https://docs.google.com/file/d/0BzmK45BP3s1yVkx5OFlGQk5WVE0/edit

现在,我想为“计数”的相对频率表创建图。

为了创建相对频率表,我需要将列的每个单元格除以列总数,以获得该单元格的相对频率,以及其他单元格的相对频率。如何去做。

我已经尝试过这个公式 counts/sum(counts) ,但这不起作用。 counts[1:4]/sum(counts[1:4]),这给了我第一列的相对频率。

帮助我为同一个表中的其他列获取相同的内容。

【问题讨论】:

标签: r


【解决方案1】:

我是plyr 和ggplot2 的忠实粉丝,因此您可能需要下载一些软件包才能使用以下内容。

install.packages('ggplot2') # only have to run once
install.packages('plyr')    # only have to run once
install.packages('scales')  # only have to run once

library(plyr)
library(ggplot2)
library(scales)

# dat <- YOUR DATA
dat_count <- ddply(ft, .(degree, satjob), 'count')
dat_rel_freq <- ddply(dat, .(degree), transform, rel_freq = freq/sum(freq))

ggplot(dat_rel_freq, aes(x = degree, y = rel_freq, fill = satjob)) +
  geom_bar(stat = 'identity') +
  scale_y_continuous(labels = percent) +
  labs(title = 'Highest Degree After finishing 9-12th Grade\n', 
       x = '', 
       y = '',
       fill = 'Job Satisfaction')

【讨论】:

  • 嗨,下面是数据集。 docs.google.com/spreadsheet/… 代码:counts = table(finaldata$satjob, finaldata$degree) barplot(counts, xlab="完成 9-12 年级后的最高学位",col = c("Dark Blue","Blueviolet","deepPink4" ,"goldenrod"), legend =(rownames(counts))) 下面的条形图是上面代码的结果。 docs.google.com/file/d/0BzmK45BP3s1yVkx5OFlGQk5WVE0/edit 现在,我想为“计数”的相对频率表创建图。希望我现在清楚了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-09
  • 2012-06-24
  • 1970-01-01
  • 2018-12-13
  • 1970-01-01
  • 2016-05-12
  • 2020-01-14
相关资源
最近更新 更多