0

I have a csv file like below, I want to make a stacked bar plot that x-axis is link column and and y-axis shows the frequency and each bar is grouped based on Freq_E and Freq_S. when I read the csv and give it to barplot it doesn't work. I searched a lot but all examples data is in form of contingency table. I donno what should I do...

           link  Freq_E  Freq_S
1          tube.com 214 214
2          list.net 120 120
3          vector.com 119 118
4          4cdn.co  95  96
Marjan
  • 123
  • 2
  • 11
  • 3
    A simple search for 'Stacked bar plot in R' gives multiple duplicates such as [this](http://stackoverflow.com/questions/12592041/plotting-a-stacked-bar-plot), [this](http://stackoverflow.com/questions/7583432/plot-stacked-bar-plot-in-r) and [this](http://stackoverflow.com/questions/19553605/stacked-barchart). Original poster is not new to SO and has shown no sign that he/she actually made the effort to find an answer. We're being too kind to the undeserving. Vote to close. – SlowLearner Nov 21 '13 at 16:18
  • well, I couldn't format my dataset as an appropriate input for barplot while the above examples you referred to explain how to make stacked plot. [this](http://stackoverflow.com/questions/19553605/stacked-barchart) is the most similar one but still didn't solve my problem. – Marjan Nov 21 '13 at 16:51
  • Come on, that's disingenuous. You offered no code of your own, nothing to show you'd tried anything to resolve the problem. There are dozens of existing questions and answers here on SO about reshaping data that would have given you the answer you sought. – SlowLearner Nov 21 '13 at 17:41

2 Answers2

4

"It doesn't work" is not an error message in R that I'm familiar with, but I'm guessing your problem is that you are trying to use barplot on a data.frame while you should be using a matrix or a vector.

Assuming your data.frame is called "df" (as defined at the start of Codoremifa's answer), you can try the following:

x <- as.matrix(df[-1])   ## Drop the first column since it's a character vector
rownames(x) <- df[, 1]   ## Add the first column back in as the rownames
barplot(t(x))            ## Transpose the new matrix and plot it

enter image description here

A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
  • ggplot is another old habit, I suppose. I've almost never used base plotting functions. +1. – TheComeOnMan Nov 21 '13 at 16:30
  • @Codoremifa, I don't actually plot very often, and I've gotten accustomed to the base plots for just exploring my data. As such the "ggplot" grammar doesn't come naturally to me. I've kept it on my "to learn" list for quite some time, but still haven't gotten around to it. – A5C1D2H2I1M1N2O1R2T1 Nov 21 '13 at 16:34
  • can you explain what does df[-1] do? – Marjan Nov 21 '13 at 16:41
  • 1
    @Marjan, I've added some comments to the code so you can see what is being done. A `matrix` in R can't have multiple data types (numeric, character, and so on), so I was creating a matrix of just the numeric portion, which meant having to drop the first column (`df[-1]`). – A5C1D2H2I1M1N2O1R2T1 Nov 21 '13 at 16:46
3

You should look at the excellent ggplot2 library, try this code snippet for your example -

df <- read.table(textConnection(
'link  Freq_E  Freq_S
tube.com 214 214
list.net 120 120
vector.com 119 118
4cdn.co  95  96'), header = TRUE)

library(ggplot2)
library(reshape2)

df <- melt(df, id = 'link')
ggplot(
   data = df,
   aes(
      y = value, 
      x = link, 
      group = variable, 
      shape = variable, 
      fill = variable
   )
) +
geom_bar(stat = "identity")
TheComeOnMan
  • 12,535
  • 8
  • 39
  • 54