Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pivoting data but keeping it aligned in R?

I have a big dataframe in R that looks like the following:

Age   Gene1   Gene2   Gene3 
33     0       1       1
57     1       0       1
90     1       1       1

I’m trying to plot this data in a bar graph, with the frequency that the genes are plotted against the age. When I try to pivot the data using:

pivoted_df <- df %>%
pivot_longer(AGE, names_to="genes", values_to="count")

This does pivot the data but my resulting dataframe looks like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Age  Gene  Count
33   Gene1   0
33   Gene2   1
33   Gene3   1  
57   Gene1   1
57   Gene2   0
57   Gene3   1
90   Gene1   1
90   Gene2   1
90   Gene3   1

Is there a way for me to pivot the data without repeating the ages? I.E still keep the genes and the count columns, but assign multiple values from that to one age?

>Solution :

Maybe you want this where you reshape your data from wide to long using melt and create the "Age" as.factor to plot stacked bars like this:

df <- read.table(text = "Age   Gene1   Gene2   Gene3 
33     0       1       1
57     1       0       1
90     1       1       1", header = TRUE)

library(dplyr)
library(ggplot2)
library(reshape)
df %>%
  melt(id.vars = "Age") %>%
  mutate(Age = as.factor(Age)) %>%
  ggplot(aes(x = Age, y = value, fill = variable)) +
  geom_col() +
  labs(fill = "gene", y = "count") +
  theme_bw()

Created on 2022-07-26 by the reprex package (v2.0.1)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading