Sushruta Iruvanti
Sushruta Iruvanti

Reputation: 1

How do I create a stacked bar chart from a frequency table in R?

If I have a table that looks like this:

  V1 V2 V3 V4
A 12 10 10 10
T 13 14 13 10
G 19 08 13 10
C 12 13 17 10

in which the number are the frequency of each letter in the column, how would I create a stacked bar chart in which the column names are on the x-axis and the frequency is the plotted value? I don't know how to do this with ggplot2. The columns don't have names because the number of columns is variable based on another function so I would like to automate creating the bar chart.

Upvotes: 0

Views: 1234

Answers (2)

RoB
RoB

Reputation: 1984

I was a bit late, but here's my take nevertheless.

txt <- "  V1 V2 V3 V4
A 12 10 10 10
T 13 14 13 10
G 19 08 13 10
C 12 13 17 10"

dat <- read.table(text = txt, h=T, row.names = 1)
dat

## with base graphics
barplot(as.matrix(dat), col = 2:5)
legend("topright", fill = 2:5, legend = rownames(dat))


## with ggplot
library(ggplot2)
library(reshape2)

# transform your data
dat2 <- dat
dat2$base <- rownames(dat)
dat2 <- melt(dat2)

# plot
ggplot(dat2, aes(fill = base, y = value, x = variable)) + geom_bar(stat = "identity")

outputs the following plots :

enter image description here enter image description here

Upvotes: 2

David
David

Reputation: 10152

Does something like this solve your question?

library(tidyverse)

df <- tibble(
   gene = c("A", "T", "G", "C"), 
   V1 = c(12, 13, 19, 12), 
   V2 = c(10, 14, 08, 13), 
   V3 = c(10, 13, 13, 17), 
   V4 = c(10, 10, 10, 10)
)
df
#> # A tibble: 4 x 5
#>   gene     V1    V2    V3    V4
#>   <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 A        12    10    10    10
#> 2 T        13    14    13    10
#> 3 G        19     8    13    10
#> 4 C        12    13    17    10

df_long <- df %>% 
  pivot_longer(-gene)
df_long
#> # A tibble: 16 x 3
#>    gene  name  value
#>    <chr> <chr> <dbl>
#>  1 A     V1       12
#>  2 A     V2       10
#>  3 A     V3       10
#>  4 A     V4       10
#>  5 T     V1       13
#>  6 T     V2       14
#>  7 T     V3       13
#>  8 T     V4       10
#>  9 G     V1       19
#> 10 G     V2        8
#> 11 G     V3       13
#> 12 G     V4       10
#> 13 C     V1       12
#> 14 C     V2       13
#> 15 C     V3       17
#> 16 C     V4       10

ggplot(df_long, aes(x = name, y = value, fill = gene)) +
  geom_col()

Created on 2020-03-20 by the reprex package (v0.3.0)

Upvotes: 1

Related Questions