楼主: 明显饭桶
1552 2

[问答] ggplot如何做到像plot那样图例上方没有colour那个单词且能自定义颜色呢? [推广有奖]

  • 0关注
  • 3粉丝

本科生

40%

还不是VIP/贵宾

-

威望
0
论坛币
706 个
通用积分
5.7057
学术水平
3 点
热心指数
8 点
信用等级
3 点
经验
8237 点
帖子
36
精华
0
在线时间
134 小时
注册时间
2015-7-7
最后登录
2020-8-23

20论坛币
  1. library(ggplot2)
  2. x=seq(0,10,length=300)
  3. df <- data.frame(x = x,y = x/2,
  4.                  z1 = x/2+sqrt(x),
  5.                  z2 = x/2-sqrt(x)) #c("x","mean","mean+SD","mean-SD")
  6. ggplot(df, aes(x,y,colour = "mean")) +
  7.   geom_line(lwd = 1.5) +
  8.   geom_line(aes(x=x,y=z1,colour="mean+SD"), lty = 2, lwd = 1.6) +
  9.   geom_line(aes(x=x,y=z2,colour="mean-SD"), lty = 2, lwd = 1.6) +
  10.   scale_y_continuous(limits = c(-1,9)) +
  11.   scale_x_continuous(limits = c(0,11)) +
  12.   theme(legend.title=NULL) +
  13.   theme(legend.position=c(.1,0.9)) +
  14.   theme(legend.background=element_blank()) +
  15.   theme(legend.key=element_blank()) +
  16.   labs(x="time")
复制代码
Rplot21.png
  1. plot(x,x/2,type="l",ylim=c(-1,9), xlim=c(0,11),
  2.      lwd=2,ylab="",xlab="time")
  3. lines(x,x/2+sqrt(x),lty=2,lwd=2)
  4. lines(x,x/2-sqrt(x),lty=2,lwd=2)
  5. legend("topleft",c("mean","mean+SD","mean-SD"),
  6.        lwd=2,lty=c(1,2,2))
复制代码
Rplot23.png

最佳答案

zerofung 查看完整内容

theme(legend.title=NULL)改为theme(legend.title=element_blank()) 最后加一层scale_color_manual(values = c("red","blue","black"))
沙发
zerofung 学生认证  发表于 2017-3-14 15:14:42 |只看作者 |坛友微信交流群
theme(legend.title=NULL)改为theme(legend.title=element_blank())
最后加一层scale_color_manual(values = c("red","blue","black"))
  1. ggplot(df, aes(x,y,colour = "mean")) +
  2.   geom_line(lwd = 1.5) +
  3.   geom_line(aes(x=x,y=z1,colour="mean+SD"), lty = 2, lwd = 1.6) +
  4.   geom_line(aes(x=x,y=z2,colour="mean-SD"), lty = 2, lwd = 1.6) +
  5.   scale_y_continuous(limits = c(-1,9)) +
  6.   scale_x_continuous(limits = c(0,11)) +
  7.   theme(legend.title=element_blank()) +
  8.   theme(legend.position=c(.1,0.9)) +
  9.   theme(legend.background=element_blank()) +
  10.   theme(legend.key=element_blank()) +
  11.   labs(x="time") +
  12.   scale_color_manual(values = c("red","blue","black"))
复制代码
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
明显饭桶 + 1 + 1 + 1 感谢及时的答复

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

使用道具

藤椅
不懂不装懂 发表于 2017-3-14 20:29:38 |只看作者 |坛友微信交流群
楼上有一点小问题,就是添加线条的顺序和颜色的顺序不一致。最后一项需加一个参数。
  1. ggplot(df, aes(x,y,colour = "mean")) +
  2.       geom_line(lwd = 1.5) +
  3.       geom_line(aes(x=x,y=z1,colour="mean+SD"), lty = 2, lwd = 1.6) +
  4.       geom_line(aes(x=x,y=z2,colour="mean-SD"), lty = 2, lwd = 1.6) +
  5.       scale_y_continuous(limits = c(-1,9)) +
  6.       scale_x_continuous(limits = c(0,11)) +
  7.       theme(legend.title=element_blank()) +
  8.       theme(legend.position=c(.1,0.9)) +
  9.       theme(legend.background=element_blank()) +
  10.       theme(legend.key=element_blank()) +
  11.       labs(x="time") +
  12.       scale_color_manual(limits=c("mean","mean+SD","mean-SD"),values = c("red","blue","black"))  
  13.   
复制代码
已有 1 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
明显饭桶 + 5 + 1 + 1 + 1 精彩帖子

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

使用道具

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

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

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

GMT+8, 2024-4-19 22:06