geom_bar with several variables - preferably stacked bar
1
1
Entering edit mode
4.4 years ago
sarahamdan8 ▴ 10

Hi

I am quite new to R and ggplot - I need to make a stacked barplot with the variables below

           -     +      1:2,5   1:12,5   1:1     1:2      1:5    1:10
alive    15800  28000   89000   52000   34000   28000   12000   28000
dead     11500  23000   60000   33000   25000   18000   23000   18000
sawed    53000  53000   73000   57000   130000  78000   63000   58000

So I want stacked barplots for the groups alive / dead and then join it with a normal barplot for the sawed category... is that even possible in ggplot? I managed to do it with normal R studio ... can't figure it out with ggplot.

This is how I want my plots to look like https://imgur.com/a/V87GyFJ

Thank you for any of your advice / help :)

R ggplot2 • 1.8k views
ADD COMMENT
3
Entering edit mode
4.4 years ago
zx8754 11k

Here is a start:

library(data.table)
library(ggplot2)

df1 <- fread("
status - + 1:2,5 1:12,5 1:1 1:2 1:5 1:10
alive 15800 28000 89000 52000 34000 28000 12000 28000
dead 11500 23000 60000 33000 25000 18000 23000 18000
sawed 53000 53000 73000 57000 130000 78000 63000 58000
")

plotDat <- melt(df1, id.vars = "status")
plotDat$grp <- ifelse(plotDat$status %in% c("dead", "alive"), 1, 2)

ggplot(plotDat, aes(variable, value, group = grp, fill = status)) +
  geom_bar(stat = "identity", position = "dodge")
ADD COMMENT

Login before adding your answer.

Traffic: 2551 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6