0

I need to display a faceted bar plot for the count of levels in all the columns of the dataset.

the code,

aas <- sapply(AAA, is.factor)
aas1 <- AAA[,aas]

overview of aas1 dataset

enter image description here

aas2 <- data.frame(t(aas1))
aas2 <- cbind(names = rownames(aas2), aas2)

library(reshape2)
aas3 <- melt(aas2,id = "names")
aas3$variable <- 1
aas3$value <- as.factor(aas3$value)

aas4 <- aggregate(.~ names + value, aas3, sum)

overview of aas4 dataset

enter image description here

ggplot(aas4, aes(x=factor(value), y= variable)) +
  geom_bar(stat="identity") + facet_grid(.~names) +
  scale_fill_discrete(name="value") +  xlab("")

facet bar plot

enter image description here

when i use aas4 dataset for faceted bar plot, each facet is consisting of all levels of "value", but i only need facets with levels corresponding to the variable "names"

Please suggest any better way to plot counts of levels of all factor variables in the dataset.

James Z
  • 12,209
  • 10
  • 24
  • 44
Balaji N
  • 363
  • 1
  • 5
  • 15

1 Answers1

0

You need to specify that you want scales="free_x" and, in order not to have the same space for each facet, space="free".

+ facet_grid(.~names, scales="free_x", space="free_x")
Luke Hayden
  • 692
  • 4
  • 8