如何基于VCF文件统计的Indel与SNP数量绘制柱状图?
Great question! You’ve already nailed the hard part by calculating your INDEL and SNP counts from the VCF file’s INFO column. Now let’s turn those numbers into a clear, easy-to-read bar chart. Below are two straightforward, widely-used methods in R:
Method 1: Using Base R
Base R has a built-in barplot() function that’s perfect for quick, simple plots. First, organize your counts into a named vector, then plot:
# Use the values you already calculated indel_count <- length(s) snp_count <- s2 # Create a named vector to map variant types to their counts variant_counts <- c(INDEL = indel_count, SNP = snp_count) # Generate the bar chart with basic customizations barplot( variant_counts, main = "INDEL vs SNP Counts in VCF File", xlab = "Variant Type", ylab = "Number of Variants", col = c("#3498db", "#e74c3c"), # Custom colors for better distinction ylim = c(0, max(variant_counts) * 1.1) # Add small buffer above the tallest bar )
Method 2: Using ggplot2 (More Customizable)
If you want a polished, flexible plot with more control over aesthetics, ggplot2 is the way to go. It works best with tidy data, so first structure your counts into a data frame:
# Install ggplot2 if you haven't already: install.packages("ggplot2") library(ggplot2) # Create a tidy data frame with your variant data variant_df <- data.frame( Variant_Type = c("INDEL", "SNP"), Count = c(length(s), s2) ) # Build the bar chart with clean styling ggplot(variant_df, aes(x = Variant_Type, y = Count, fill = Variant_Type)) + geom_col(width = 0.6) # Adjust bar width for a balanced look labs( title = "Variant Distribution in VCF File", x = "Variant Type", y = "Number of Variants" ) + scale_fill_manual(values = c("#3498db", "#e74c3c")) # Match base R colors for consistency theme_minimal() # Use a clean, modern theme theme(legend.position = "none") # Remove legend since x-axis already labels types
Both methods will give you a clear visual comparison of your INDEL and SNP counts. Feel free to tweak colors, titles, or axis labels to fit your needs!
内容的提问来源于stack exchange,提问作者J0ki

