楼主: lydia5555
3514 4

[问答] 如何去除坐标左右框和曲线之间的空白距离? [推广有奖]

  • 0关注
  • 0粉丝

初中生

95%

还不是VIP/贵宾

-

威望
0
论坛币
15 个
通用积分
38.1025
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
95 点
帖子
10
精华
0
在线时间
19 小时
注册时间
2014-9-2
最后登录
2018-6-1

楼主
lydia5555 发表于 2016-11-5 11:20:44 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

如下图红箭头所示,左右框和曲线间有一段空白区域,我想去掉这个区域,曲线直接从边框起始,用什么参数?谢谢

QQ截图20161105110128.jpg
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:如何 左右

沙发
lydia5555 发表于 2016-11-5 11:28:28
再做一个简单的图plot(0:10,3:13,type = "l"),也还是这样,横坐标的0为什么不从最右端开始呢?

QQ截图20161105112729.jpg (11.87 KB)

1

1

藤椅
hugebear 发表于 2016-11-5 22:05:17
问题提得很清楚,赞。这种有违数学上常见的坐标轴一直也困扰着我(不过R的作者应该也有自己的考量)~
也许没有简单直接的解决方案,我的解决方法是一开始限制R自动给出坐标轴,稍后自己用lines加arrows手动画上坐标轴。
见下面的例子:
  1. par(mar = c(1, 1, 0, 0) + 0.1)
  2. x <- c(0, 1, 2, 3)
  3. p <- c(1/8, 3/8, 3/8, 1/8)
  4. plot(x, p, type = "n", axes = F, xlab = "", ylab = "",
  5.      xlim = c(-0.2, 3.2), ylim = c(-0.1, 1.2))
  6. arrows(x0 = -0.2, y0 = 0, x1 = 3.2, y1 = 0, length = 0.1, angle = 15)
  7. arrows(x0 = 0, y0 = 0, x1 = 0, y1 = 1.2, length = 0.1, angle = 15)

  8. lines(c(x[1], x[1]), c(0, p[1]), col = "blue", lwd = 3)
  9. lines(c(x[2], x[2]), c(0, p[2]), col = "blue", lwd = 3)
  10. lines(c(x[3], x[3]), c(0, p[3]), col = "blue", lwd = 3)
  11. lines(c(x[4], x[4]), c(0, p[4]), col = "blue", lwd = 3)

  12. text(x = 3.2, y = -0.03, labels = expression("x"))
  13. text(x = 0, y = -0.02, labels = 0)
  14. text(x = 1, y = -0.02, labels = 1)
  15. text(x = 2, y = -0.02, labels = 2)
  16. text(x = 3, y = -0.02, labels = 3)

  17. text(x = -0.1, y = 1.2, labels = expression("F(x)"))
  18. text(x = -0.1, y = 1/8, labels = expression(frac(1, 8)))
  19. text(x = -0.1, y = 3/8, labels = expression(frac(3, 8)))
  20. text(x = -0.1, y = 4/8, labels = expression(frac(4, 8)))
  21. text(x = -0.1, y = 7/8, labels = expression(frac(7, 8)))
  22. text(x = -0.1, y = 1, labels = expression(1))

  23. # Add tickmarks on x-axis and y-axis
  24. xcor <- c(0, 1, 2, 3)
  25. ycor <- c(1/8, 3/8, 4/8, 7/8, 1)

  26. for (xv in xcor) {
  27.   lines(c(xv, xv), c(0, 0.05), col = "black")
  28. }

  29. for (yv in ycor) {
  30.   lines(c(0, 0.05), c(yv, yv), col = "black")
  31. }

  32. # Start drawing cdf
  33. lines(c(-0.2, 0), c(0, 0), col = "orange", lwd = 3)
  34. lines(c(0, 1), c(1/8, 1/8), col = "orange", lwd = 3)
  35. lines(c(1, 2), c(4/8, 4/8), col = "orange", lwd = 3)
  36. lines(c(2, 3), c(7/8, 7/8), col = "orange", lwd = 3)
  37. lines(c(3, 3.2), c(1, 1), col = "orange", lwd = 3)

  38. points(c(0, 1, 2, 3), c(1/8, 4/8, 7/8, 1), pch = 19, col = "orange", cex = 1.5)
  39. points(c(0, 1, 2, 3), c(0, 1/8, 4/8, 7/8), pch = 1, col = "orange", cex = 1.5)

  40. points(c(0, 1, 2, 3), c(1/8, 3/8, 3/8, 1/8), pch = 4, col = "blue", cex = 1.5)
  41. legend(x = 0.5, y = 1, c(expression(paste("pmf of ", X)), expression(paste("cdf of ", X))),
  42.        lwd = c(3, 3), col = c("blue", "orange"))
复制代码


这段代码给出的图片输出如下:

代码可能会有点烦,不知道这个问题在最近流行的ggplot2, ggvis包里有没有得到更好的处理。权作抛砖引玉吧。
已有 1 人评分热心指数 收起 理由
求证1加1 + 1 热心帮助其他会员

总评分: 热心指数 + 1   查看全部评分

板凳
zhangyangsmith 发表于 2016-11-10 16:06:25
Try setting xaxs="i"?
According to R help:

xaxs

The style of axis interval calculation to be used for the x-axis. Possible values are "r", "i", "e", "s", "d". The styles are generally controlled by the range of data or xlim, if given.
Style "r" (regular) first extends the data range by 4 percent at each end and then finds an axis with pretty labels that fits within the extended range.
Style "i" (internal) just finds an axis with pretty labels that fits within the original data range.
Style "s" (standard) finds an axis with pretty labels within which the original data range fits.
Style "e" (extended) is like style "s", except that it is also ensures that there is room for plotting symbols within the bounding box.
Style "d" (direct) specifies that the current axis should be used on subsequent plots.
(Only "r" and "i" styles have been implemented in R.)

报纸
lydia5555 发表于 2016-12-1 15:33:16
非常感谢两位的解答!!!

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-20 11:19