1

I have the following data:

enter image description here

I would like to generate a bar plot that shows the frequency of each value of Var1 per each run. I want the x axis represents each run and the y axis represents the frequency of each Var1 value. To do that, I wrote the following R script:

df <- read.csv("/home/nasser/Desktop/data.csv")
g <- ggplot(df) +  
    geom_bar(aes(Run, Freq, fill = Var1, colour = Var1), position = "stack", stat = "identity") 

The result that I got is:

enter image description here

The issue is that the x axis does not show each run seperately (the axis should be 1, 2, .., etc) and the legend should show each value of Var1 seperately and in a different color. Also, the bars are not so clear since it is so difficult to see the frequency of each Var1 values. In other words, the generated plot is not the normal stacked bar like the one shown in this answer

How to solve that?

Nasser
  • 2,118
  • 6
  • 33
  • 56

1 Answers1

2

You need to convert both variables to factors. Otherwise, R sees them as numerical and not categorical data.

df <- read.csv("/home/nasser/Desktop/data.csv")
g <- ggplot(df) +  
    geom_bar(aes(factor(Run), Freq, fill = factor(Var1), colour = factor(Var1)), 
             position = "stack", stat = "identity") 
Arienrhod
  • 2,451
  • 1
  • 11
  • 19