0

I have a data table like this

ind1 ind2 ind3 indn
0.1 0.5 1.0 0.0
0.9 0.6 1.0 0.1
0.9 0.7 0.9 0.3

Each column is an individual, each row is the individuals genotype probability at that row's genome coordinate.

I want to make a single, stacked histogram where the x-axis is GP ranging from 0 to 1, and the y axis is number of sites in that range (with the stacks colored based on the individual they originated from). I previously did something like this before:

plN <- ggplot(ndf, aes(x=GP, fill=indiv, color=indiv)) + geom_histogram(binwidth = 0.01)
plot(plN)

by in that case, there were only two columns.

ind1 0.1
ind1 0.9
ind1 0.9
ind2 0.5
ind2 0.6
ind2 0.7...
dnv89
  • 23
  • 4
  • The general answer to this is to reshape your example dataset into that second format, with one column for the grouping variable and another for the value. I didn't see an exact duplicate but see, e.g., [this](https://stackoverflow.com/questions/48940156/ggplot-geom-bar-where-x-multiple-columns) or search "ggplot2 bar multiple columns". – aosmith Jul 21 '20 at 00:55
  • 1
    `ggplot(tidyr::pivot_longer(df, cols = everything()), aes(x=value, fill=name, color=name)) + geom_histogram(binwidth = 0.01)` ? – Ronak Shah Jul 21 '20 at 00:56
  • @RonakShah, can you explain what this means, I don't know how tidyr works. Do I have to replace value or name with something? Right now I do not have any row name, and the column names are just the individual names. – dnv89 Jul 21 '20 at 01:14
  • No just replace `df` with your dataframe name which I guess is `ndf` – Ronak Shah Jul 21 '20 at 01:20

1 Answers1

0

As @aosmith commented the usual (tidy) way is to get data in long format for plotting. You can get data in long format by using pivot_longer from tidyr. It will get the data in two-column format where name column is the column name and value column is the value in that column.

library(ggplot2)
ggplot(tidyr::pivot_longer(ndf, cols = everything()), 
       aes(x=value, fill=name, color=name)) + geom_histogram(binwidth = 0.01)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213