1

I want to create a panel of 4 barplots using ggplot. This stack overflow post contains an example: Simple bar plot with multiple variables in R - Similar to Excel

When I run the code, I obtain an error because there obviously is no variable called "variable". I would appreciate advice regarding how I can run this code.

library(ggplot2)

Unit <- c("A", "B", "C", "D") #character objects need quotes
Yes <- c(50, 65, 20, 41)
No <- c(70, 67, 40, 20)
Missing <- c(10, 12, 8, 7)
df <- data.frame(Unit, Yes, No, Missing)

require(tidyr)
df.long <- gather(df, Unit)
colnames(df.long) <- c("Unit", "variable", "value")

Error in names(x) <- value : 'names' attribute [3] must be the same length as the vector [2]

ggplot(data = df.long, aes(x = Unit, y = value, fill = variable)) +
       geom_col(position = position_dodge()) 

Error in FUN(X[[i]], ...) : object 'variable' not found

Parfait
  • 104,375
  • 17
  • 94
  • 125
bgreen
  • 63
  • 6

0 Answers0