楼主: verbatim
3382 10

[问答] help on legend() in R [推广有奖]

  • 1关注
  • 3粉丝

博士生

5%

还不是VIP/贵宾

-

威望
0
论坛币
513 个
通用积分
2.0505
学术水平
0 点
热心指数
2 点
信用等级
0 点
经验
3404 点
帖子
139
精华
0
在线时间
143 小时
注册时间
2006-12-1
最后登录
2020-1-30

相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
Could anybody help on how to overlay all three different color of points(pch=c(16,16,16), col=c(1,2,3)) with one line (lty=4) in the legend() in R? Thanks a lot!
二维码

扫码加我 拉你入群

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

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

关键词:Legend egen Help elp Leg different anybody legend points color

回帖推荐

zhangyangsmith 发表于11楼  查看完整内容

Finally you explain your wish CLEARLY. If you accept using low level functions to customize your legend you could do pretty much what you want. It should not be hard if you understand the example I have shown above. Output: I completely ignore the plot as I was not able to imagine a plot that needs such legend... Will you show your plot, perhaps with dummy data? The way to determine t ...
沙发
kaifengedu 发表于 2013-6-9 09:33:19 |只看作者 |坛友微信交流群
  1. x <- seq(0, 2, 0.01)
  2. y1 <- x
  3. y2 <- 2 * x
  4. y3 <- 3 * x
  5. plot(x, y1, pch = 16, col = 1, lty = 4)
  6. lines(x, y2, pch = 16, col = 2, lty = 4)
  7. lines(x, y3, pch = 16, col = 3, lty = 4)
  8. legend('bottomright', legend = c('y1', 'y2', 'y3'), pch = c(16, 16, 16),
  9.        col = c(1, 2, 3), lty = c(4, 4, 4))
复制代码

已有 2 人评分论坛币 学术水平 热心指数 收起 理由
ryusukekenji + 1 + 1 热心帮助其他会员
qoiqpwqr + 10 + 1 热心帮助其他会员

总评分: 论坛币 + 10  学术水平 + 1  热心指数 + 2   查看全部评分

使用道具

藤椅
verbatim 发表于 2013-6-9 12:43:50 |只看作者 |坛友微信交流群
Thanks but sorry for not clear. I attached the legend I want in a PDF file with this message, besides this, I also would like to change all three black dots in the legend in the attachment to three different colors (col=c(1,2,3)).

Presentation1.pdf

3.22 KB

使用道具

板凳
qoiqpwqr 发表于 2013-6-9 21:02:16 |只看作者 |坛友微信交流群
我觉得一个点就足够了
  1. x <- 1:10
  2. y1 <- x
  3. y2 <- x + 0.5
  4. y3 <- x + 1
  5. plot(x, y1, ylim = c(1, 11), col = "blue", type = "o")
  6. lines(x, y2, col = "red", type = "o")
  7. lines(x, y3, col = "green", type = "o")
  8. legend("topleft", legend = c("y1", "y2", "y3"), pch = 1, col = c("blue", "red", "green"), lty = 1)
复制代码

使用道具

报纸
verbatim 发表于 2013-6-9 22:52:05 |只看作者 |坛友微信交流群
Thanks. I knew your way. but my data need that kind of legend I want, the data didn't have three lines, only one line link all data shown in three different colors. I know it is difficult to create that legend I want.

使用道具

地板
zhangyangsmith 发表于 2013-6-13 04:24:35 |只看作者 |坛友微信交流群
verbatim 发表于 2013-6-9 22:52
Thanks. I knew your way. but my data need that kind of legend I want, the data didn't have three lin ...
Not sure what exactly you want. They ask for virtual money to see your example and I am unwilling to do so...
Anyway, I suppose you have 3 types of points and 1 type of line which you would like to show. And those make 4 rows in the legend. Following @qoiqpwqr's example:
  1. plot(x, y1, ylim = c(1, 11), col = "blue")
  2. points(x, y2, col = "red")
  3. points(x, y3, col = "green")
  4. lines(x, y1)
  5. lines(x, y2)
  6. lines(x, y3)
  7. legend("topleft", legend = c("y1", "y2", "y3", "line"), pch = c(1, 1, 1, NA),
  8. col = c("blue", "red", "green", "black"), lty = c(NA, NA, NA, 1))
复制代码

使用道具

7
verbatim 发表于 2013-6-19 11:55:51 |只看作者 |坛友微信交流群
Thank you but sorry. this is not what I want, please see PDF file I attached previously.

使用道具

8
zhangyangsmith 发表于 2013-6-19 20:36:11 |只看作者 |坛友微信交流群
verbatim 发表于 2013-6-19 11:55
Thank you but sorry. this is not what I want, please see PDF file I attached previously.
All right, finally I could see your example, without actual plot. If you are so picky on the details you have to do it by hand. It is possible that there is a package hiding somewhere which includes a secret function that can do it. But I personally find it much quicker to write some scripts as following:

  1. x <- 1:10

  2. y1 <- x

  3. y2 <- x + 0.5

  4. y3 <- x + 1

  5. clrs <- c("blue", "red", "green")

  6. plot(NA, NA, xlim = c(0, 11), ylim = c(1, 11), xlab = "x", ylab = "y")

  7. lgd <- paste("y", c(1:3), sep = "")

  8. for (i in 1:length(clrs))

  9. {

  10.   points(x, eval(parse(text = paste("y", i, sep = ""))),
  11.          type = "o", pch = 16, col = clrs[i])

  12.   text(par()$usr[1] + par()$cxy[1],
  13.      par()$usr[4] - par()$cxy[2]*i,
  14.      adj = c(0, 0.5), lgd[i])

  15.   lines(par()$usr[1] + (nchar(lgd[1]) + 1)*par()$cxy[1] + c(0:2*par()$cxy[1]),
  16.       rep(par()$usr[4], 3) - par()$cxy[2]*i,
  17.       type = "o", pch = 16, col = clrs[i])

  18. }
复制代码
And output:

customizedLgnd.png

There a 1-character width space between the text and the left margin of the plot and between the text and the symbol. The line spacing is 1.5 times character height. The space between top margin of the plot and the legend is half of a character height. And the distance between points in the legend is 1-character width.

Anyway I agree with qioqpwqr, single point in the legend seems enough. Hard to imagine cases that you need definitely this type of legend.
已有 1 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
qoiqpwqr + 40 + 2 + 2 + 2 精彩帖子

总评分: 论坛币 + 40  学术水平 + 2  热心指数 + 2  信用等级 + 2   查看全部评分

使用道具

9
verbatim 发表于 2013-6-22 08:27:45 |只看作者 |坛友微信交流群
First of all, Thank you very much! very big progress! but still need one more help.

Actually, for the lengend, what I need is only one lengend with three different colors of points instead of three legends, I mean, for example, for y1 in the legend in your example, I need colors of points are green, red and blue on one line, instead of three legends with different colors and color of points are all same for each legend. There is only one line connecting three groups of points with three different colors of green, red, blue in the plot. But i don't need generate plot, I already finished generating plot. I just need legend. In the example in my PDF file, I need change three black points to green, red, blue for that one legend only, no second and third legend please.  

I think this is hard, and don't know how to generate this legend with R. but I am a little bit excited we are closing.

使用道具

10
zhangyangsmith 发表于 2013-6-23 05:37:34 |只看作者 |坛友微信交流群
verbatim 发表于 2013-6-22 08:27
First of all, Thank you very much! very big progress! but still need one more help.

Actually, fo ...
Finally you explain your wish CLEARLY. If you accept using low level functions to customize your legend you could do pretty much what you want. It should not be hard if you understand the example I have shown above.
  1. lgd <- "Your legend"

  2. plot(NA, NA, xlim = c(0, 10), ylim = c(0, 10),
  3.      xlab = "x", ylab = "y")

  4. text(par()$usr[1] + par()$cxy[1],
  5.      par()$usr[4] - par()$cxy[2],
  6.      adj = c(0, 0.5), lgd)

  7. lines(par()$usr[1] + strwidth(lgd) + c(2, 4)*par()$cxy[1],
  8.       rep(par()$usr[4] - par()$cxy[2], 2), col = "#000000")

  9. for (i in 1:3)
  10.   points(par()$usr[1] + strwidth(lgd) + (i + 1)*par()$cxy[1],
  11.          par()$usr[4] - par()$cxy[2],
  12.          pch = 16, col = sprintf("#%06X", 256^i - 256^(i - 1)))
复制代码
Output:


clr_lgd.png

I completely ignore the plot as I was not able to imagine a plot that needs such legend... Will you show your plot, perhaps with dummy data?
The way to determine the location of the symbols has been changed. It is more precise to use the strwidth function than nchar()*par()$cxy[1] according to R's documentation.
已有 1 人评分热心指数 收起 理由
qoiqpwqr + 1 热心帮助其他会员

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

使用道具

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

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-28 17:33