0

I am an R beginner. What's the best way to create a stacked bar graph (summing to 100%)?

Below is my date sample. I have two variables, would like to use Var 1 for x axis, Var 2 for y axis, and Freq for data.

    Var1                                         Var2 Freq

1      1 01  ONE FAMILY HOMES                          127
2      2 01  ONE FAMILY HOMES                          633
3      3 01  ONE FAMILY HOMES                         1859
4      4 01  ONE FAMILY HOMES                         4722
5      5 01  ONE FAMILY HOMES                         2144
6      1 02  TWO FAMILY HOMES                          107
7      2 02  TWO FAMILY HOMES                          833
8      3 02  TWO FAMILY HOMES                         3503
9      4 02  TWO FAMILY HOMES                         2859
10     5 02  TWO FAMILY HOMES                          799
SurvivalMachine
  • 7,946
  • 15
  • 57
  • 87

1 Answers1

0

I'm not exactly sure what kind of bar chart you have in mind,but ggplot2 is my go-to plotting library for research and exploration.

I think this is what you need and at the very least should point you in the right direction.

Example:

library(ggplot2)

df = data.frame(year = rep(c(1,2,3,4,5), 2), 
                type = c(rep("ONE FAMILY", 5), rep("TWO FAMILY", 5)),
                Freq = c(127, 633, 1859, 4722, 2144, 107, 833, 3503, 2859, 799))

ggplot(df, aes(x = year, y = Freq, fill = type )) + 
  geom_bar(stat = "identity")

Produces:

enter image description here