0

I'm using R for the analysis of my master thesis and I have a data with age categories 1 through 6 and I have different time points (1 through 7) I took the average of each time point. So now I have a 6 by 8 table and I want to make a histogram say putting the age category on the x-axis and the different time points on the y-axis to compare them.

The data:

Group.1 T0   T1   T2   T3   T4   T5   T6
1       0.52 0.64 0.65 0.54 0.87 0.65 0.73
2       0.87 0.54 0.65 0.60 0.87 0.65 0.87
3       0.97 0.48 0.65 0.60 0.87 0.36 0.88 
4       0.45 0.67 0.66 0.87 0.87 0.51 0.98
5       0.70 0.99 0.84 0.88 0.87 0.54 0.98
6       0.77 0.80 0.87 NaN  NaN  NaN  1.00

I used the following command:

library(reshape2) 

new.df<-melt(data,id.vars="Group.1")
names(new.df)=c("Group.1","variable","value") 

library(ggplot2)

ggplot(data=new.df, aes(x=Group.1, y=value,fill=(variable)))+
    geom_histogram()

At first I was getting an error code saying "error: Unknown parameters: binwidth, bins, pad" So I tried specifying and now I am getting an error code "Error: stat_bin() must not be used with a y aesthetic."

Can somebody help me? what I want is something like this:

Draw histograms per row over multiple columns in R

(The first one on top with multiple colors)

Thank you.

Zas

Community
  • 1
  • 1
Z.Chanell
  • 35
  • 8
  • Hi, welcome to Stack Overflow. Please provide a minimal working example so that we can help better. See http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Felix Aug 11 '16 at 12:42
  • Hi, I added a hypothetical data is this better? Thank you – Z.Chanell Aug 11 '16 at 12:53
  • Ok, I see. Do you mean something like this: http://pasteboard.co/7mKtOYcHm.png? I yes, I'd be happy to write up an answer – Felix Aug 11 '16 at 13:28
  • yes, that is exactly what I want. – Z.Chanell Aug 11 '16 at 13:29

2 Answers2

1

Ok, so first what you're looking for is a simple "grouped bar chart", not a histogram. See here for an explanation. With that out of the way, here is a simple example using your toy data:

First we reshape the data, assuming that you've read the data you pasted as df <- read.table("clipboard", header = T). Note that I use tidyr's gather() function to reshape the data, which is similar to reshape

# with dplyr & tidyr
library(dplyr)
library(tidyr)

df <- df %>% 
   gather(key, value, -Group.1)

# without dplyr
df <- reshape2::melt(df, id.var = "Group.1", variable.name = "key")

Now, it's simply a matter of getting the groups and colors in ggplot right.

  ggplot(df, aes(x = Group.1, y = value)) + # 'Group.1' on the x-axis
  geom_bar(stat = "identity",               # stat = "identity", so we can use y values
           position = "dodge",              # this puts all the bars next to each other
           aes(group = key, fill = key)) +  # group and fill by time slot
  scale_fill_brewer(palette = "Set1") +     # this gives you nicer colors
  scale_x_continuous(breaks = 1:6)  

Which gives you:

Grouped Bar Chart

Felix
  • 1,611
  • 13
  • 22
  • I am trying to download the package "dplyr" and I get an error msg saying "Installing package into ‘H:/R-Libraries/V3’ (as ‘lib’ is unspecified) Warning in install.packages : package ‘dplyr’ is not available (for R version 3.1.1)" any idea ? – Z.Chanell Aug 11 '16 at 13:54
  • yes, the latest version of dplyr requires at least version 3.1.2. This might help you with updating R: https://www.r-statistics.com/2013/03/updating-r-from-r-on-windows-using-the-installr-package/ – Felix Aug 11 '16 at 13:58
  • For the sake of solving the issue without updating R, I've edited my answer and added a reshape solution – Felix Aug 11 '16 at 14:02
  • Thank you, however I am getting another error code "Error: Discrete value supplied to continuous scale" – Z.Chanell Aug 11 '16 at 14:07
  • In your original data frame, "Group.1" might be a factor variable. Try it without the `scale_x_continuous` addition to the ggplot command – Felix Aug 11 '16 at 14:12
0

Perhaps these can help you get started:

#scatterplot
ggplot(data=new.df, aes(x=Group.1, y=value,colour=variable))+
       geom_point()

#barchart
ggplot(data=new.df, aes(x=Group.1, y=value,fill=variable))+
       geom_bar(stat="identity",position = "dodge")

#line + scatterplot
ggplot(data=new.df, aes(x=Group.1, y=value,colour=variable))+
       geom_point()+
       geom_line()
Jeroen Boeye
  • 580
  • 4
  • 18