Density plot help
1
0
Entering edit mode
3 months ago
Adyasha • 0

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

enter image description here

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()
plot Linux • 311 views
ADD COMMENT
0
Entering edit mode
3 months ago

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 variable to the fill aesthetic, although your columns are called "collection.date", "sample", "lineage", "abundance". Same with y.
  • To get the stacking right, geom_density() needs to have postion="fill" attribute. See the bottom example on this page.
ADD COMMENT

Login before adding your answer.

Traffic: 1739 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6