Question: My intention is to construct a stacked bar graph using R with the following data

```
dat <- read.table(text = "user_id tier share
1 Tier1 0.50
1 Tier2 0.20
1 Tier3 0.30
2 Tier1 0.75
2 Tier2 0.56
2 Tier3 0.34", header = TRUE)

barplot(as.matrix(dat))

```

Here each unique user\_id will be given one bar where each tier will be one stack.

However, the code gives me the wrong image
[![enter image description here](https://i.stack.imgur.com/Md35m.png)](https://i.stack.imgur.com/Md35m.png)

I am fairly new to Rlang. Should I create a list instead or are there options that I need to insert in the barplot function?

Answer: 
I would use `ggplot2` package to do this:

```
require('ggplot2')

dat$user_id <- as.factor(dat$user_id) # user_id as factor variable

ggplot(dat, aes(x = user_id, y = share, fill = tier)) + 
   geom_bar(stat = 'identity')

```

gives you:

[![enter image description here](https://i.stack.imgur.com/IlHb3.png)](https://i.stack.imgur.com/IlHb3.png)
Here's a solution using base R:

```
tbl <- table(dat[2:1])
tbl[] <- dat$share
barplot(tbl)

```

[![enter image description here](https://i.stack.imgur.com/7qIWH.png)](https://i.stack.imgur.com/7qIWH.png)