1
votes

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!

1
Welcome to SO. Please read thisagstudy

1 Answers

0
votes

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.