'How to plot bar charts of dataframe columns using ggplot and facet?

Slightly basic question, but maybe somebody could help me. I have a dataframe with a series of columns that represent answers to survey questions. I want to use ggplot and facet_wrap to plot these answers as bar charts side by side. How's it best to do this?

  Response f76a f76b
1        1   88   23
2        2  510  234
3        3  283  363
4        4  137  251

p <- ggplot(data=x, aes(x=Response, y=f76a)) +
  geom_bar(stat="identity")



Solution 1:[1]

Sample code:

library(ggpubr)
library(ggplot2)

x %>% 
  gather(Response) %>% 
  ggplot(aes(x=Response, y=value)) +
  geom_bar(stat="identity")+
  labs(x="", y="")+
  theme_classic2()+
  theme(axis.text.x = element_text(hjust = 1,face="bold", size=12, color="black"), 
        axis.title.x = element_text( face="bold", size=16, color="black"),
        axis.text.y = element_text( face="bold", size=12, color="black"),
        axis.title.y = element_text( face="bold", size=16, color="black"),
        strip.text = element_text(size=10, face="bold"),
        legend.text = element_text( color = "black", size = 16,face="bold"),
        legend.position="bottom",
        legend.box = "horizontal",
        plot.title = element_text(hjust = 0.5))

Plot:

enter image description here

For facet use facet_wrap(~Response)

Plot: enter image description here

Sample data:

    Response = c(1,2,3,4)
    f76a=c(88,510,283,137)
    f76b=c(23,234,363,251)

x=cbind(Response,f76a,f76b )

x<-data.frame(x)  

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1