在R中自定义图形

时间:2012-04-07 19:39:42

标签: r graph charts

  1. 我有一段图表的代码: barplot(as.vector(t(mat1[1,3:ncol(mat1)])),las=2) 我想改变,以便 x -axis被行 y = 2取代;有效地将 x -axis向上移动2个单位,如下图所示。 enter image description here

    我需要从2开始的条形图,以便:

    • 值为3的条形从y = 2行开始,上升到y = 3结束。
    • 值为0的栏从y = 2行开始,下降从y = 0结束
  2. 如何制作mat1我的 x -axis类别的列名?

2 个答案:

答案 0 :(得分:7)

Barplot总是以0开始其条形。从每个y值减去2(或者我做的5)。将ylim设置为范围(y值 - 5)。您需要使用yaxt =“n”来禁止绘制y轴。轴的xpd参数允许标签范围扩展到实际值的范围之下。

 set.seed(231)
 tN <- table(Ni <- stats::rpois(100, lambda=5))
 tNshift <- tN-5
 barplot(tNshift, space = 1.5, yaxt="n", xaxt="n", ylim=range(tNshift))
 abline(0,0)
 axis(2, at= c(-5, pretty(tNshift)), labels=c(0, pretty(tNshift)+5), xpd=TRUE)

enter image description here

答案 1 :(得分:1)

这是来自?barplot的第一个示例,稍加修改,添加了abline(x,y)

require(grDevices) # for colours
tN <- table(Ni <- stats::rpois(100, lambda=5))

barplot(tN, space = 1.5, axisnames=FALSE)
abline(5,0)

enter image description here

很抱歉,如果这没有回答你的具体任务,但我没有任何样本数据可供使用,所以我采用了?barplot示例。