You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R中绘制线条分隔清晰的镜像直方图?

Fixing Your Mirrored Histogram: Separated Lines & Axis Symbols

Let's tackle your two main issues head-on: overlapping lines in the mirrored histogram and adding "+"/"-" symbols around the zero axis. Here's a step-by-step breakdown and revised code tailored to your needs:

Key Issues in Your Original Code

  • Overlapping Lines: You were plotting neg$coverage directly (positive values) on top of pos$coverage—both ended up on the positive y-axis, causing the messy overlap. To create a true mirror effect, you need to invert the negative strand values to negative numbers.
  • Missing Axis Symbols: There was no code to place the required "+" (above zero) and "-" (below zero) markers on the plot.

Revised Code with Explanations

load("rdata.rdata")

# Set up high-resolution TIFF output
tiff(filename = paste0("Mirrored plot of ", sample.name), 
     width = 4, height = 2, pointsize = 6, units = 'in', res = 300)

# Initialize plot with positive coverage (y-axis spans -20 to 20 for mirroring)
plot(pos$coverage, 
     type = 'h', 
     ylim = c(-20, 20),
     col = 'deepskyblue2',
     ylab = "",
     xlab = paste0("Aligned with ", aligned.with),
     xaxt = 'n',
     xaxs = 'i',  # Remove extra x-axis padding for a tighter, cleaner fit
     lwd = 1.2    # Slightly thicker lines to improve visibility
)

# Add formatted y-axis label
title(ylab = expression("Number of aligned reads" ~ (10^-3)), 
      line = 2, cex.lab = 1.2, family = "Calibri Light")

# Plot NEGATIVE strand coverage (critical: invert values to mirror below zero)
lines(-neg$coverage,  
      type = 'h', 
      col = 'darkorange3',
      lwd = 1.2)

# Add x-axis ticks and position labels
l <- length(pos$position)
axis(1, 
     at = floor(seq(from = 1, to = l, by = l / 10)), 
     labels = floor(seq(from = head(pos$position, n = 1), 
                        to = tail(pos$position, n = 1), 
                        by = (tail(pos$position, n = 1) - head(pos$position, n = 1))/10)))

# Add "+" and "-" symbols near the zero axis (adjust positions to match your example)
text(x = l * 0.05, y = 2, labels = "+", cex = 1.2, font = 2)  # "+" above zero
text(x = l * 0.05, y = -2, labels = "-", cex = 1.2, font = 2) # "-" below zero

dev.off()

What Changed & Why?

  1. Inverted Negative Coverage: lines(-neg$coverage) ensures the negative strand plots below the zero axis, completely separating it from the positive strand—this fixes the core overlap issue.
  2. Thicker Lines: lwd = 1.2 makes individual lines more distinct, especially if your data is dense.
  3. Tighter X-Axis: xaxs = 'i' removes unnecessary padding, so your data fills the plot area cleanly.
  4. Added Axis Symbols: The text() commands place the "+" and "-" markers—tweak the x/y values to match the exact positioning in your example.

Bonus: For Extremely Dense Data

If lines still overlap due to high data density, replace the type='h' line plots with filled polygons for even clearer separation:

# Replace the type='h' lines with these polygon fills
# Positive strand fill (semi-transparent for readability)
polygon(c(1:length(pos$coverage), length(pos$coverage), 1), 
        c(pos$coverage, 0, 0), 
        col = adjustcolor('deepskyblue2', alpha.f = 0.7), border = NA)
# Negative strand fill
polygon(c(1:length(neg$coverage), length(neg$coverage), 1), 
        c(-neg$coverage, 0, 0), 
        col = adjustcolor('darkorange3', alpha.f = 0.7), border = NA)

内容的提问来源于stack exchange,提问作者Achal Neupane

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:03:17