Density plot help
Hello everyone,
I have a dataset like this :
collection.date sample lineage abundance
27-12-2023 Sample_ID_W_V_849 B.1.617 0.193548
27-12-2023 Sample_ID_W_V_849 AY.102 0.16846905
27-12-2023 Sample_ID_W_V_849 AY.39.1 0.16846905
27-12-2023 Sample_ID_W_V_849 AY.44 0.16846905
27-12-2023 Sample_ID_W_V_849 AY.39 0.16846905
27-12-2023 Sample_ID_W_V_849 AY.116 0.0909091
27-12-2023 Sample_ID_W_V_849 AY.46.6 0.0416667
27-12-2023 Sample_ID_W_V_848 JN.1.1 0.2
27-12-2023 Sample_ID_W_V_848 JN.1 0.142857
27-12-2023 Sample_ID_W_V_848 AY.79 0.111111
27-12-2023 Sample_ID_W_V_848 AY.1 0.100218
27-12-2023 Sample_ID_W_V_848 AY.39 0.100218
27-12-2023 Sample_ID_W_V_848 AY.127 0.100218
27-12-2023 Sample_ID_W_V_848 BA.2.1 0.07132782
27-12-2023 Sample_ID_W_V_848 JN.5 0.0689655
27-12-2023 Sample_ID_W_V_848 BA.2.23 0.03978418
I want to plot density plot for each sample showing its lineages density according its abundance.
I want to plot something like this
I have tried this R script:
# Read data from TSV file
data_long <- read.delim("path/to/data_long.tsv", stringsAsFactors = FALSE)
# Plot density using ggplot2
library(ggplot2)
# Convert the data to long format
data_long_long <- reshape2::melt(data_long, id.vars = c("collection.date", "sample"), measure.vars = c("lineage", "abundance"))
# Plot density using ggplot2, colored by lineage
ggplot(data_long_long, aes(x = as.Date(collection.date, format="%d-%m-%Y"), fill = variable, y = as.numeric(value))) +
geom_density(alpha = 0.5) +
labs(title = "Density Plot of Lineage Abundance Over Time",
x = "Date",
y = "Density") +
scale_x_date(date_labels = "%d-%m-%Y", date_breaks = "1 week") +
theme_minimal()
• 981 views
•
link
1 answer
Without asking a specific question or posting the exact error message, we can't really help.
But for a starter:
- Take care that the variables you assign to aesthetics match actual columns in your data frame. For example, you are assigning
variableto thefillaesthetic, although your columns are called "collection.date", "sample", "lineage", "abundance". Same withy. - To get the stacking right,
geom_density()needs to havepostion="fill"attribute. See the bottom example on this page.
• 0 views
•
link
Log in to answer this question.