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

R语言matplot绘图异常:显示字符而非点及X轴设置问题

解决matplot显示异常与X轴刻度设置问题

问题1:图中显示数字/字母而非点的解决方法

你遇到的显示问题是因为matplot默认会循环使用不同的绘图符号(pch参数),当列数较多时(比如你这里的20列),部分符号会被解析为数字或字母。解决方法很简单,显式指定统一的点符号即可:

比如用实心小圆点(pch=20)或者实心圆(pch=16),修改后的绘图代码如下:

## draw plot with uniform points
matplot(mat, type = "p", ylab = " ", pch = 20)

这样所有列的观测值都会显示为一致的点,不会出现数字或字母。

问题2:设置X轴为1到30且间隔为1

xlim参数只是设置X轴的显示范围,不会改变刻度的间隔。要实现1到30每个整数都显示刻度,需要手动自定义坐标轴:

  1. 在matplot中添加xaxt="n",告诉R不要绘制默认的X轴;
  2. 用axis()函数手动添加刻度,指定at=1:30来设置刻度位置,labels=1:30设置刻度标签。

完整代码如下:

## draw plot with custom X-axis
matplot(mat, type = "p", ylab = " ", pch = 20, xaxt = "n")
axis(1, at = 1:30, labels = 1:30)

这样X轴就会显示1到30的所有整数刻度,间隔为1。

完整整合代码

把两个修改整合在一起,完整的可运行代码如下:

## dummy data
dat <- rnorm(30, 1, 0.5)
## generate 20 sampled data
resamples <- lapply(1:20, function(i) sample(dat, replace = T))
## create matrix combining all sampled data together
mat <- t(do.call(rbind, resamples))

## draw plot with correct points and custom X-axis
matplot(mat, type = "p", ylab = " ", pch = 20, xaxt = "n")
axis(1, at = 1:30, labels = 1:30)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 08:53:15