0

Below is the dataset. https://docs.google.com/spreadsheet/ccc?key=0AjmK45BP3s1ydEUxRWhTQW5RczVDZjhyell5dUV4YlE#gid=0

Code:

counts = table(finaldata$satjob, finaldata$degree) barplot(counts, xlab="Highest Degree after finishing 9-12th Grade",col = c("Dark Blue","Blueviolet","deepPink4","goldenrod"), legend =(rownames(counts)))

The below barplot is the result of the above code. https://docs.google.com/file/d/0BzmK45BP3s1yVkx5OFlGQk5WVE0/edit

Now, i want to create the plot for relative frequency table of "counts".

For creating a relative frequency table, I need the divide each cell of the column by the column total to get the relative frequency for that cell and so for others as well. How to go about doing it.

I have tried this formula counts/sum(counts) , but this is not working. counts[1:4]/sum(counts[1:4]), this gives me the relative frequency of the first column.

Help me obtain the same for other columns as well in the same table.

user3398251
  • 33
  • 1
  • 3
  • 6
  • What about these: https://www.google.com/search?q=r+relative+frequency+table ? – lukeA Mar 09 '14 at 11:23
  • 3
    Welcome to SO! People are generally much more happy to help if you provide a [**minimal, self-contained example**](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) with data in a copy-pasteable format (i.e. not screenshots). – Henrik Mar 09 '14 at 11:25
  • Hi, Below is the dataset. https://docs.google.com/spreadsheet/ccc?key=0AjmK45BP3s1ydEUxRWhTQW5RczVDZjhyell5dUV4YlE#gid=0 Code: counts = table(finaldata$satjob, finaldata$degree) barplot(counts, xlab="Highest Degree after finishing 9-12th Grade",col = c("Dark Blue","Blueviolet","deepPink4","goldenrod"), legend =(rownames(counts))) The below barplot is the result of the above code. https://docs.google.com/file/d/0BzmK45BP3s1yVkx5OFlGQk5WVE0/edit Now, i want to create the plot for relative frequency table of "counts". Hope i am clear now. – user3398251 Mar 10 '14 at 08:50

1 Answers1

1

I'm a big fan of plyr & ggplot2, so you may have to download a few packages for the below to work.

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

enter image description here

maloneypatr
  • 3,562
  • 4
  • 23
  • 33
  • Hi, Below is the dataset. https://docs.google.com/spreadsheet/ccc?key=0AjmK45BP3s1ydEUxRWhTQW5RczVDZjhyell5dUV4YlE#gid=0 Code: counts = table(finaldata$satjob, finaldata$degree) barplot(counts, xlab="Highest Degree after finishing 9-12th Grade",col = c("Dark Blue","Blueviolet","deepPink4","goldenrod"), legend =(rownames(counts))) The below barplot is the result of the above code. https://docs.google.com/file/d/0BzmK45BP3s1yVkx5OFlGQk5WVE0/edit Now, i want to create the plot for relative frequency table of "counts". Hope i am clear now. – user3398251 Mar 10 '14 at 08:51