1

I am a newbie with R (we are using it at university for marketing research).

So, here's the thing:

I want to create a stacked barplot using ggplot2 that show 3 rectangles, with three different colours, for every var on the x line! On the Y line instead I would like to put percentages, but it's ok even with the frequencies.

I've been googling three day before posting, I swear. I'm not a programmer, so sometimes even If i may have found what I needed I think I haven't recognized it :/

This are my tries:

require(ggplot2)
head(dati)
ggplot(dati, aes(B4_1,B4_2,B4_3)) + geom_bar(position="dodge", stat="identity")

(but of course the results isn't right, because it puts the B4_1 var on the X and the B4_2 var on the Y)

so I tried:

ggplot(dati, aes(B4_1)) + geom_bar(position="dodge",binwidth=x)

because i tought it would at least give me the roots! But it says that x wasn't found.

I tried messing around with this code but it keeps giving me different errors, like that x wasn't found, that bin was wrong. I honestly tried and I can post my chronology on the internet of the last two days if anyone thinks I'm just asking you to do my work for me!

Thank you for the support, any help will be VERY appreciated! happy new year!

Mark Heckmann
  • 10,943
  • 4
  • 56
  • 88
  • 3
    Welcome to SO. Please read [this](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – agstudy Dec 31 '13 at 10:27

1 Answers1

0

I need to make some assumptions because the structure of your data is not entirely clear. What I'm pretty sure of is that you are sampling pairs with two values:

  • x
  • something that can be either B4_1, B4_2, B4_3

I'm basing this on your comment about frequencies/counts. If B4_* are actual variables with values, then the answer will be different but will almost certainly involve "melting" your data into long format.

So, with that:

# Make up data

data <- data.frame(
  x=sample(c("a", "b", "c"), 150, replace=T),
  lab=sample(c("B4_1", "B4_2", "B4_3"), 150, replace=T)
)
# Plot

ggplot(data, aes(x=x, fill=lab)) + geom_bar()

sample bar chart

Notice how I only specify the x values, and then geom_bar automatically counts them for me. You definitely don't want position=dodge.

Either way, you really should at a minimum post the results of dput(head(dati)) so people can provide better answers.

BrodieG
  • 51,669
  • 9
  • 93
  • 146
  • thank you very much for you help! I've tried to use the lines you wrote, but that's what I had in result: 'Errore: ggplot2 doesn't know how to deal with data of class function'. '> dput(head(dati)) structure(list(x = structure(c(1L, 2L, 1L, 1L, 1L, 1L), .Label = c("a", "b", "c"), class = "factor"), lab = structure(c(2L, 3L, 3L, 2L, 3L, 3L), .Label = c("B4_1", "B4_2", "B4_3"), class = "factor")), .Names = c("x", "lab"), row.names = c(NA, 6L), class = "data.frame")' What I need is that for every var on the x axis there are 3 rectangles,with the frequencies of B4_1, B4_2, B4_3 for that var – user3148982 Jan 01 '14 at 20:42
  • `ggplot(dati, aes(x=x, fill=lab)) + geom_bar()` worked with your data. Are you 100% sure you're not doing something like `ggplot(data, aes(x=x, fill=lab)) + geom_bar()` (note `data` vs `dati`; in my example my data was called `data`, so it worked, but in yours it's called `dati`) – BrodieG Jan 02 '14 at 12:54