【问题标题】:R plot with strings showing in the axis轴上显示字符串的 R 绘图
【发布时间】:2013-06-27 16:03:37
【问题描述】:

我正在根据宿舍房间号(4 位数)绘制住户人数。房间号应该是字符串。但是当我使用 as.character(RmNum) 时,轴仍然显示为数字。

meanResidents = c(3, 4, 3, 2, 4, 5)
rmNumber = c(2034, 3043, 4012, 2035, 2022, 3013)
plot(as.character(rmNumber), meanResidents, xlab = as.character(rmNumber))

我希望宿舍号码在轴上垂直显示。有人可以帮我吗?

【问题讨论】:

  • 请给我们你的代码和一个小数据集,我们可以用它来重现问题。
  • @Roland meanResidents = c(3, 4, 3, 2, 4, 5); rmNumber = c(2034, 3043, 4012, 2035, 2022, 3013)。
  • @Roland 我的代码是 plot(as.character(rmNumber), meanResidents, xlab = as.character(rmNumber)) 但 xaxis 显示的是数字而不是字符串。
  • 我也试过 plot(as.character(rmNumber), meanResidents, xlab ='n', xaxt = 'n'); axis(1, at=seq(1, length(rmNumber), 1), labels=as.character(rmNumber), las =2) 还是不行....

标签: r plot axis-labels vertical-text


【解决方案1】:

使用函数axis,您可以指定轴的位置,放置刻度线的位置(at)并选择labels。参数las=2表示垂直于轴的标签。

plot(meanResidents, axes=FALSE, xlab="dorms")
axis(2)
axis(1, at=seq_along(meanResidents),labels=as.character(rmNumber), las=2)
box()

【讨论】:

  • @user2498497 如果您对结果感到满意(并且您不希望得到另一个答案),最好通过单击此答案的复选标记来表明这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-24
  • 1970-01-01
相关资源
最近更新 更多