0

I want to create an histogram for every column in small multiples using a grouping variable which is stored in a column as well, with ggplot.

My dataframe is something like this:

   col1 col2 col3 group
 1    1    4    7 site1
 2    2    5    8 site2
 3    3    6    9 site1
 4    0    4    1 site2
 5   87    5   23 site1
 6   54   78   43 site2

My code for the plot is:

plot_A<-ggplot(dataframe, aes(x = col1)) +
      geom_histogram(fill = "indianred2", col = "white") +
      facet_wrap("group")+
      theme(strip.text.x = element_text(face = "bold", size=30), 
      axis.title.x= element_text(size=30),
      axis.title.y=element_text(size=30),
      axis.text.y=element_text(size=15),
      axis.text.x=element_text(size=15))+
      labs(x="col1"))

Since I have 53 columns to plot, how could I automate this task? Ideally storing each small multiple plot in an object with the name of the column.

thanks!

I am a begginer in code, so I didnt try anything

zx8754
  • 52,746
  • 12
  • 114
  • 209
maria_m
  • 1
  • 2
  • We need to reshape from wide-to-long before plotting, something like: `ggplot(melt(d, id.vars = "group"), aes(value)) + geom_histogram() + facet_grid(group~variable)` – zx8754 Mar 16 '23 at 12:28

0 Answers0