楼主: zhou1_20
2602 8

[问答] 用ggplot2怎么做这样的图 [推广有奖]

已卖:689份资源

副教授

20%

还不是VIP/贵宾

-

威望
0
论坛币
18999 个
通用积分
1029.7754
学术水平
146 点
热心指数
166 点
信用等级
135 点
经验
36357 点
帖子
541
精华
0
在线时间
887 小时
注册时间
2015-9-25
最后登录
2025-12-4

楼主
zhou1_20 发表于 2016-6-8 22:37:26 |AI写论文
20论坛币
Rplot.png
这是用R 自带的基本绘图工具做的,我想问下怎么用ggplot2做类似的图,用R 自带的基本绘图工具绘图代码如下:
  1. dose<-c(0,20,40,60,80,100)
  2. drugA<-c(10,15,35,45,65,95)
  3. drugB<-c(10,25,30,65,80,95)
  4. opar<-par(no.readonly=TRUE)
  5. par(lwd=2,cex=1.5,font.lab=2)
  6. plot(dose,drugA,type="b",pch=20,lty=1,col="red",ylim=c(0,105),axes=FALSE,
  7.      main="学习曲线(我 VS 别人)",xlab="学习阶段(小学到现在)",ylab="学习折线")
  8. axis(1, at = seq(0,100,20), labels = c("小学", "初中", "高中", "本科", "研究生", "毕业"),pos=0)
  9. lines(dose,drugB,type="b",pch=17,cex=0.8,lty=2,col="blue")
  10. axis(2, at = seq(0,105,15), pos = 0)
  11. abline(h=c(50),lwd=1.5,lty=2,col="gray")
  12. legend("topleft",inset=0.05,cex=0.5,title="图例",c("我","别人"),
  13.        lty=c(1,2),pch=c(20,17),col=c("red","blue"))
  14. par(opar)
复制代码


关键词:ggplot2 gplot plot GPL 怎么做 绘图工具

沙发
robbieyeah 发表于 2016-6-8 22:37:27
  1. df <- read.table(header=T, text='
  2. cond xval yval
  3.    A    1  2.0
  4.    A    2  2.5
  5.    A    3  2.3
  6.    B    1  3.0
  7.    B    2  2.0
  8.    B    4  3.1')


  9. library(ggplot2)

  10. # Plot with standard lines and points
  11. # group = cond tells it which points to connect with lines
  12. ggplot(df, aes(x=xval, y=yval, group = cond)) +
  13.   geom_line(aes(linetype=cond)) +
  14.   geom_point(aes(shape=cond),size=4)
复制代码

藤椅
zhou1_20 发表于 2016-6-9 10:19:14
robbieyeah 发表于 2016-6-9 01:25
坐标怎么设置成途中那样的‘小学,初中。。。。。。

板凳
hou0922 学生认证  发表于 2016-6-9 19:50:51
zhou1_20 发表于 2016-6-9 10:19
坐标怎么设置成途中那样的‘小学,初中。。。。。。
接楼上

  1. g <- ggplot(df, aes(x=x, y=y, group = cond)) +
  2.   geom_line(aes(linetype=cond)) +
  3.   geom_point(aes(shape=cond),size=4)

  4. g+scale_x_continuous(breaks = c(1,2,3,4),labels = c("小学","初中","高中","大学"))
复制代码



已有 1 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
zhou1_20 + 5 + 1 + 1 + 1 精彩帖子

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

报纸
zhou1_20 发表于 2016-6-9 22:20:18
Rplot01.png
首先谢谢大家的热心解答,这是我自己编的程序,
还有个问题就是如何修改图例的标题(subject???
  1. library(ggplot2)
  2. dose<-c(0,20,40,60,80,100)
  3. drugA<-c(10,15,35,45,65,95)
  4. drugB<-c(10,25,30,65,80,95)
  5. subject=c(rep('我',6),rep('别人',6))
  6. drug=c(drugA,drugB)
  7. dos=rep(dose,2)
  8. learn=data.frame(subject,drug,dos)
  9. ggplot(learn,aes(dos,drug,group=subject,linetype =subject ,colour = subject,
  10.                  shape = subject, fill = subject))+geom_point()+geom_line()+
  11.   labs(title='学习曲线(我 VS 别人)',x="学习阶段(小学到现在)", y="学习折线")+
  12.   theme(panel.background=element_rect(fill = 'white',colour = 'black'),
  13.         legend.background = element_rect(fill = 'white'))+
  14.   scale_x_continuous(breaks=dose, labels=c("小学", "初中", "高中", "本科", "研究生","毕业"))
复制代码



地板
robbieyeah 发表于 2016-6-9 22:30:31
在scale_x_continuous中加入guide = guide_legend(title = "图例")

7
zhou1_20 发表于 2016-6-9 23:05:19
robbieyeah 发表于 2016-6-9 22:30
在scale_x_continuous中加入guide = guide_legend(title = "图例")
不能运行

8
robbieyeah 发表于 2016-6-10 22:28:41
  1. library(ggplot2)
  2. dose<-c(0,20,40,60,80,100)
  3. drugA<-c(10,15,35,45,65,95)
  4. drugB<-c(10,25,30,65,80,95)
  5. subject=c(rep('我',6),rep('别人',6))
  6. drug=c(drugA,drugB)
  7. dos=rep(dose,2)
  8. learn=data.frame(subject,drug,dos)
  9. ggplot(learn,aes(dos,drug,group=subject ,colour = subject),
  10.        linetype =subject,
  11.        shape = subject,
  12.        fill = subject)+
  13.   geom_point()+
  14.   geom_line()+
  15.   labs(title='学习曲线(我 VS 别人)',x="学习阶段(小学到现在)", y="学习折线",
  16.        color='图例')+
  17.   theme(panel.background=element_rect(fill = 'white',colour = 'black'),
  18.         legend.background = element_rect(fill = 'white'))+
  19.   
  20.   scale_x_continuous(breaks=dose,
  21.                      labels=c("小学", "初中", "高中", "本科", "研究生","毕业"))
复制代码

9
zhou1_20 发表于 2016-6-10 23:16:47
robbieyeah 发表于 2016-6-10 22:28
谢谢,不过这样画,线型问题没解决,画出图显示两条线全是实线

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-29 15:03