Skip to content

Commit 6574683

Browse files
committed
fixed order on x axis with large number of PCs
1 parent d5d13b2 commit 6574683

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

workflow/scripts/plot_PCA.R

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ library(scales)
1616
data <- read.delim(snakemake@input[[1]],
1717
header = TRUE,
1818
skip = 1)
19+
colnames(data) <- gsub("^X", "", colnames(data))
1920

2021
# Unique sample conditions
2122
colnames(data) <- gsub(".ext300", "", colnames(data))
@@ -27,7 +28,7 @@ if (length(samples) == 1) {
2728
colours <- "#1B9E77"
2829
} else if (length(samples) == 2) {
2930
colours <- c("#1B9E77", "#D95F02")
30-
} else if (length(genotypes) > 2) {
31+
} else if (length(samples) > 2) {
3132
colours <- brewer.pal(length(samples), "Dark2")
3233
}
3334
names(colours) <- samples
@@ -84,6 +85,9 @@ df <- data %>%
8485
mutate(Component = paste0("PC", Component)) %>%
8586
mutate(cumulative_variance = (cumsum(Eigenvalue) / sum(Eigenvalue) * 100 * scalefactor))
8687

88+
# Re-level PC factors to ensure correct order
89+
df$Component <- factor(df$Component, levels = df$Component)
90+
8791
# Create scree plot
8892
s <- ggplot(df, aes(Component, cumulative_variance)) +
8993
geom_bar(aes(Component, Eigenvalue),

0 commit comments

Comments
 (0)