使用R plot命令重叠标题?

时间:2015-09-26 22:58:55

标签: r plot graph

目前,当我尝试绘制图形时,我无法使用R studio。 我想让Y轴只说:迭代的E(sigma)和X轴只说:列表大小。不幸的是,它是重叠的,人们无法阅读它。有没有办法来解决这个问题。我为自己的无知道歉,但为了避免使用Excel,我自己自学R,所以我真的是新手。谢谢你的帮助。这是R代码:

  N = c(100, 200, 300, 400, 500, 600, 700, 800, 900, 1000)
  Shell Sort = c(100, 200, 300, 400, 500, 600, 700, 800, 900, 1000 )
  M = c(100, 200, 300, 400, 500, 600, 700, 800, 900, 1000 )
  plot(N, M, type = "o", col = "green");par(new=TRUE)
  plot(N, Shell Sort, type = "o", col = "blue")
  legend('topleft', col = c("black", "red"), lty = 1, 
         legend = c("N", "Shell Sort"), bty='n', cex=.59)
  title(main="Comparisons - Speed", col.main="black", font.main=4)
  title(xlab="List size", col.lab=rgb(0,0.5,0))
  title(ylab="∑ of iterations", col.lab=rgb(0,0.5,0))

根据我从您的评论中读到的内容,我做到了这一点:

        N = c(100, 200, 300, 400, 500, 600, 700, 800, 900, 1000)
        InsertionSort = c(33, 80, 127, 177, 245, 318, 420, 532, 654, 815 )
        ShellSort = c(18, 48, 111, 156, 213, 283, 360, 451, 566, 684 )
        plot(N, InsertionSort, type = "o", col = "green", 
             xlab="List size", ylab="∑ of iterations", col.lab=rgb(0,0.5,0),
             main="Comparisons - Speed", col.main="black", font.main=4)
        par(new=TRUE)
        plot(N, ShellSort, type = "o", col = "blue", 
             xlab="", ylab="")

        legend('topleft', col = c("black", "red"), lty = 1, 
               legend = c("N", "Shell Sort"), bty='n', cex=.9)

现在y值正在重叠。我为第一次不清楚地解释自己而道歉。谢谢你的帮助。

这是图片 Graph

1 个答案:

答案 0 :(得分:3)

这不是RStudio问题。默认情况下,plot函数会根据绘图中x和y变量的名称将x和y轴标题添加到绘图中。您可以通过将它们更改为空字符串来删除它们,然后在您完成后再添加它们。或者,您可以直接在plot命令中添加它们。我根据我猜测你尝试做的事情,对代码做了一些更改。如果我猜错了,请告诉我。

N = seq(100,1000,100)
ShellSort = seq(100,1000,100)
M = seq(50,950,100)

plot(N, M, type = "o", col = "green", xlab="", ylab="")
#par(new=TRUE)
lines(N, ShellSort, type = "o", col = "blue")

legend('topleft', col = c("black", "red"), lty = 1, 
       legend = c("N", "Shell Sort"), bty='n', cex=.59)

title(main="Comparisons - Speed", col.main="black", font.main=4)
title(xlab="List size", col.lab=rgb(0,0.5,0))
title(ylab="∑ of iterations", col.lab=rgb(0,0.5,0))

下面的代码直接在plot命令中添加主轴和x轴和y轴标题。然后运行与上面相同的代码,但跳过对title的三次调用。

plot(N, M, type = "o", col = "green", 
     xlab="List size", ylab="∑ of iterations", col.lab=rgb(0,0.5,0),
     main="Comparisons - Speed", col.main="black", font.main=4)

这是由此产生的情节:

enter image description here