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:
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?
CodePudding user response:
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)