如何格式化 R 中线图的 x 轴以便它可以容纳多个文本标签?

发布于 2025-01-06 19:08:23 字数 730 浏览 1 评论 0原文

Name    UP  Downs
A   10  -3
B   2   -4
C   1   -1
D   4   -1
E   5   0
F   0   -1
G   6   -5
H   0   -1
I   7   -1
J   0   -1
K   0   -11
L   3   -1
M   0   -13
N   2   -1
O   0   -1
P   1   -1
Q   0   0
R   1   -1
S   0   0
T   12  -1
.....
.........
..........
(goes all the way to 2500 rows) 

这是我的 R 代码的样子...

nba <- read.csv("test.csv")
plot(nba$UP, type="b", lwd=1, xaxt="n", ylim=c(-22, 22), col="red", xlab="Name", 
    ylab="Change", main="My plot")
axis(1, at=1:length(nba$Name), labels=nba$Name)
lines(rain$Downs, col="green", type="b", lwd=1)

该代码运行良好...但是绘制时的 x 轴显示很少的选定“名称”,而不是全部。问题是...有没有办法显示 x 轴中的所有文本(垂直且字体较小)以及当我将文件另存为图像时有没有办法...我可以放大并查看 x 轴标签。

Name    UP  Downs
A   10  -3
B   2   -4
C   1   -1
D   4   -1
E   5   0
F   0   -1
G   6   -5
H   0   -1
I   7   -1
J   0   -1
K   0   -11
L   3   -1
M   0   -13
N   2   -1
O   0   -1
P   1   -1
Q   0   0
R   1   -1
S   0   0
T   12  -1
.....
.........
..........
(goes all the way to 2500 rows) 

Here is what my R code looks like...

nba <- read.csv("test.csv")
plot(nba$UP, type="b", lwd=1, xaxt="n", ylim=c(-22, 22), col="red", xlab="Name", 
    ylab="Change", main="My plot")
axis(1, at=1:length(nba$Name), labels=nba$Name)
lines(rain$Downs, col="green", type="b", lwd=1)

This code works well...but the xaxis when plotted shows few selected "Name" and not all of them. The question is ...Is there a way to show all the text in the xaxis (either vertical with smaller font) and is there a way when i save the file as an image...i can zoom in and view the xaxis labels.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

临风闻羌笛 2025-01-13 19:08:23

如果您对真正想要绘制和标记的内容更有选择性,您可能会发现分析绘图会更容易。

例如,标记具有许多观测值的图的一种策略是仅标记异常值。以下示例说明了如何仅标记数据中的最高分和最低分。

创建一些示例数据并绘图。

set.seed(1)
n <- 100
nba <- data.frame(
    obs = 1:n,   
    Name = paste("label", 1:n, sep="_"), 
    UP = sample(0:15, n, replace=TRUE), 
    Downs = sample(-15:0, n, replace=TRUE)
)

plot(nba$obs, nba$UP, type="b", lwd=1, ylim=c(-22, 22), col="red", 
    xlab="Name", ylab="Change", main="My plot")

找到 10 个最高和最低值,并使用 text 添加标签:

nups <- 10
ups <- tail(nba[order(nba$UP), ], nups)
with(ups, text(obs, UP+1, Name, srt=90, adj=0, cex=0.75))

ndowns <- 10
downs <- head(nba[order(nba$UP), ], nups)
with(downs, text(obs, UP-1, Name, srt=90, adj=1, cex=0.75))

在此处输入图像描述

You may find it easier to analyse your plot if you are more selective about what it is you really want to plot and label.

For example, one strategy in labelling plots with many observations is to label only the outliers. Here is an example of how you might go about labelling only the highest and lowest scores in your data.

Create some sample data and plot.

set.seed(1)
n <- 100
nba <- data.frame(
    obs = 1:n,   
    Name = paste("label", 1:n, sep="_"), 
    UP = sample(0:15, n, replace=TRUE), 
    Downs = sample(-15:0, n, replace=TRUE)
)

plot(nba$obs, nba$UP, type="b", lwd=1, ylim=c(-22, 22), col="red", 
    xlab="Name", ylab="Change", main="My plot")

Find the 10 highest and lowest values and use text to add the label:

nups <- 10
ups <- tail(nba[order(nba$UP), ], nups)
with(ups, text(obs, UP+1, Name, srt=90, adj=0, cex=0.75))

ndowns <- 10
downs <- head(nba[order(nba$UP), ], nups)
with(downs, text(obs, UP-1, Name, srt=90, adj=1, cex=0.75))

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文