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

R语言直方图均值与分位数竖线不显示问题求助

修复R直方图竖线不显示问题

我的R代码可部分运行,但无法在直方图上显示以下竖线:

  • 绿色的均值竖线
  • 蓝色的x_right <- quantile(x, 0.95)对应的95分位数竖线
  • 红色的x1_right <- quantile(x1, 0.05)对应的5分位数竖线

希望实现目标竖线效果,原代码如下:

V_max=10
V_min=0
H=1
n=1

x_lim_min=-1
x_lim_max=13


C=c(0,0.01,0.1,1)


par(mfrow=c(length(C)+1,1), mar = c(2,0,2,0),oma = c(1,5,0,0))


V <- function( C, H, n ){
  1 / (1 + (C / H)^n) 
}


base_plot_fun <- function(x, x1, y, y1) {
  mean_x <- mean(x)
  mean_x1 <- mean(x1)
  
  # initialize the plot
  plot(
    0,
    0,
    ylim = range(c(y$counts, y1$counts)),
    xlim = range(c(y$breaks, y1$breaks)),
    xlab = "x",
    ylab = "counts",
    type = "n"
  )
  
  
  corners <- par('usr') # get the corners of the plots
  
  x_left <- corners[1]
  x_right <- quantile(x, 0.95) # at 95 percentile, change as needed
  
  x1_left <- x_right
  x1_right <- quantile(x1, 0.05) # at 5th percentile, change if needed
  
  rect(
    xleft = x_left,
    xright = x_right,
    ybottom = corners[3],
    ytop = corners[4],
    col = 'lightblue',
    density = 100
  )
  rect(
    xleft = x1_left,
    xright = x1_right,
    ybottom = corners[3],
    ytop = corners[4],
    col = 'pink',
    density = 100
  )
}



x <- rnorm(100,V_min,1)
x1 <- rnorm(100,V_max,1)
y <- hist(x,plot=FALSE,breaks = 20)
y1 <- hist(x1, plot=FALSE,breaks=20)

base_plot_fun(x = x, x1 = x1, y = y, y1 = y1)
plot(y, col='gray48',add=T,xlim=c(x_lim_min,x_lim_max))
plot(y1, col='gray48',add=T,xlim=c(x_lim_min,x_lim_max))


mylist <- list()
for(i in 1:length(C)){
  V_C <- V_max*V(C[i],H,n)
  x3 <- rnorm(100,V_C,1)
  mylist[i] <- mean(x3)
  y3 <- hist(x3, plot=FALSE,breaks=20)
  
  plot(y3, col='gray48',xlim=c(x_lim_min,x_lim_max))
}



abline(v=mean_x3,col='forestgreen',lwd=3)


# histograms, not plotted
y <- hist(x,plot=FALSE,breaks = 20)
y1 <- hist(x1, plot=FALSE,breaks=20)


base_plot_fun(x = x, x1 = x1, y = y, y1 = y1)

corners <- par('usr') # get the corners of the plots

mean_x <- mean(x)
mean_x1 <- mean(x1)

x_left <- corners[1]
x_right <- quantile(x, 0.95) # at 95 percentile, change as needed

x1_left <- x_right
x1_right <- quantile(x1, 0.05) # at 5th percentile, change if needed

plot(y, col='lightgray',add=TRUE)
plot(y1, col='gray48',add=TRUE)


abline(v=mean_x,col='forestgreen',lwd=3)
abline(v=mean_x1,col='forestgreen',lwd=3)


abline(v=x_right,col='steelblue',lwd=3)
abline(v=x1_right,col='firebrick',lwd=3)

修改后的代码

V_max=10
V_min=0
H=1
n=1

x_lim_min=-1
x_lim_max=13

C=c(0,0.01,0.1,1)

# 设置多图布局
par(mfrow=c(length(C)+1,1), mar = c(2,0,2,0), oma = c(1,5,0,0))

V <- function(C, H, n ){
  1 / (1 + (C / H)^n) 
}

base_plot_fun <- function(x, x1, y, y1) {
  # 初始化空图,统一x轴范围
  plot(
    0,
    0,
    ylim = range(c(y$counts, y1$counts)),
    xlim = c(x_lim_min, x_lim_max),
    xlab = "x",
    ylab = "counts",
    type = "n"
  )
  
  corners <- par('usr')
  x_right <- quantile(x, 0.95)
  x1_right <- quantile(x1, 0.05)
  
  # 绘制背景矩形
  rect(
    xleft = corners[1],
    xright = x_right,
    ybottom = corners[3],
    ytop = corners[4],
    col = 'lightblue',
    density = 100
  )
  rect(
    xleft = x_right,
    xright = x1_right,
    ybottom = corners[3],
    ytop = corners[4],
    col = 'pink',
    density = 100
  )
  
  # 返回需要的统计量,方便后续添加竖线
  return(list(mean_x = mean(x), mean_x1 = mean(x1), 
              x_right = x_right, x1_right = x1_right))
}

# 绘制第一个子图:x和x1的直方图
x <- rnorm(100,V_min,1)
x1 <- rnorm(100,V_max,1)
y <- hist(x, plot=FALSE, breaks=20)
y1 <- hist(x1, plot=FALSE, breaks=20)

stats <- base_plot_fun(x = x, x1 = x1, y = y, y1 = y1)
plot(y, col='gray48', add=TRUE)
plot(y1, col='gray48', add=TRUE)
# 添加竖线
abline(v=stats$mean_x, col='forestgreen', lwd=3)
abline(v=stats$mean_x1, col='forestgreen', lwd=3)
abline(v=stats$x_right, col='steelblue', lwd=3)
abline(v=stats$x1_right, col='firebrick', lwd=3)

# 循环绘制C对应的子图
mylist <- list()
for(i in 1:length(C)){
  V_C <- V_max*V(C[i], H, n)
  x3 <- rnorm(100, V_C, 1)
  mylist[[i]] <- mean(x3)
  y3 <- hist(x3, plot=FALSE, breaks=20)
  
  # 初始化空图,统一x轴范围
  plot(0, 0, ylim = range(y3$counts), xlim = c(x_lim_min, x_lim_max),
       xlab = "x", ylab = "counts", type = "n")
  plot(y3, col='gray48', add=TRUE)
  # 添加当前组的均值竖线
  abline(v=mylist[[i]], col='forestgreen', lwd=3)
}

关键修改说明

  1. 统一坐标范围:所有绘图的x轴都使用x_lim_min和x_lim_max,避免竖线因坐标范围不一致被截断
  2. 调整绘图流程:每个子图先初始化空图,再叠加直方图,最后立即添加对应竖线,避免后续绘图覆盖
  3. 变量作用域修正:在循环内直接计算并使用当前组的均值,解决mean_x3未定义的问题
  4. 函数返回统计量:让base_plot_fun返回需要的均值、分位数,方便后续调用添加竖线
  5. 移除重复代码:删除原代码中重复的直方图计算和绘图部分,避免干扰

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 16:20:28