0

I want to make a histogram for each column. Each Column has three values (Phase_1_Mean, Phase_2_Mean and Phase_3_Mean)

The output should be: 12 histograms (because we have 12 rows), and per histogram the 3 values showed in a bar (Y axis = value, X axis = Phase_1_Mean, Phase_2_Mean and Phase_3_Mean).

Stuck: When I search the internet, almost everyone is making a "long" data frame. That is not helpful with this example (because than we will generate a value "value". But I want to keep the three "rows" separated.

At the bottom you can find my data. Appreciated!

I tried this (How do I generate a histogram for each column of my table?), but here is the "long table" problem, after that I tried Multiple Plots on 1 page in R, that solved how we can plot multiple graphs on 1 page.

dput(Plots1)
structure(list(`0-0.5` = c(26.952381, 5.455598, 28.32947), `0.5-1` = 
c(29.798635, 
25.972696, 32.87372), `1-1.5` = c(32.922764, 41.95935, 41.73577
), `1.5-2` = c(31.844156, 69.883117, 52.25974), `2-2.5` = c(52.931034, 
128.672414, 55.65517), `2.5-3` = c(40.7, 110.1, 63.1), `3-3.5` =         
c(73.466667, 
199.533333, 70.93333), `3.5-4` = c(38.428571, 258.571429, 95), 
`4-4.5` = c(47.6, 166.5, 233.4), `4.5- 5` = c(60.846154, 
371.730769, 74.61538), `5-5.5` = c(7.333333, 499.833333, 
51), `5.5-6` = c(51.6, 325.4, 82.4), `6-6.5` = c(69, 411.5, 
134)), class = "data.frame", .Names = c("0-0.5", "0.5-1", 
"1-1.5", "1.5-2", "2-2.5", "2.5-3", "3-3.5", "3.5-4", "4-4.5", 
"4.5- 5", "5-5.5", "5.5-6", "6-6.5"), row.names = c("Phase_1_Mean", 
"Phase_2_Mean", "Phase_3_Mean"))

Something which is showed in this example (which didn't worked for me, because it is Python) https://www.google.com/search?rlz=1C1GCEA_enNL765NL765&biw=1366&bih=626&tbm=isch&sa=1&ei=Yqc8XOjMLZDUwQLp9KuYCA&q=multiple+histograms+r&oq=multiple+histograms+r&gs_l=img.3..0i19.4028.7585..7742...1.0..1.412.3355.0j19j1j0j1......0....1..gws-wiz-img.......0j0i67j0i30j0i5i30i19j0i8i30i19j0i5i30j0i8i30j0i30i19.j-1kDXNKZhI#imgrc=L0Lvbn1rplYaEM:

R overflow
  • 1,292
  • 2
  • 17
  • 37
  • If you make the transformations in another object then the original object with the original dataframe will be preserved, thus, the transformed dataframe will only be used to plot and nothing else. What is the real problem here? creating multiple graphs in one device or transforming the dataframe? – Cris Jan 14 '19 at 15:36

1 Answers1

2

I think you have to reshape to long to make this work, but I don't see why this is a problem. I think this code achieves what you want. Note that there are 13 plots because you have 13 (not 12) columns in the dataframe you posted.

# Load libraries
library(reshape2)
library(ggplot2)

Plots1$ID <- rownames(Plots1) # Add an ID variable
Plots2 <- melt(Plots1) # melt to long format
ggplot(Plots2, aes(y = value, x = ID)) + geom_bar(stat = "identity") + facet_wrap(~variable)

Below is the resulting plot. I've kept it basic, but of course you can make it pretty by adding further layers.

enter image description here

Niek
  • 1,594
  • 10
  • 20
  • You are right. I have to recheck why I got stuck (with the assumption that a long data frame is not correct). Anyways, really appreciated! – R overflow Jan 14 '19 at 15:59