R语言基础绘图——散点图

R语言基础绘图——散点图

应用R语言绘制简单的散点图

数据

attachments-2017-07-m5negXGA596c725e74fd

采用plot函数绘制散点图

setwd("d:/huitu")
data=read.table(".txt",header=T,sep="\t")

plot(data$x0,data$y1,main="picture",xlab="year",ylab="weight",col="red",pch=1,cex=3)
points(data$x0,data$y2,col="blue",pch=3)
points(data$x0,data$y3,col="green",pch=5)
legend("topright", c("y1", "y2","y3"),pch = c(1,3,5), bg ="white")


参数
①    main自定义标题 ; sub:副标题
②    xlab、 ylabX、Y轴标题
③    col自定义颜色
④    cex散点大小; pch:点类型参数;lwd:点边框粗细参数
⑤    xlimylim: 坐标轴区间参数
⑥    legend图例参数进行设置
⑦    points():在原有图形上添加点

attachments-2017-07-7bKNUNsq596c72e0b250

采用ggplot2绘制简单的散点图
install.packages(“ggplot2”)
library(ggplot2)
pressure   #内置数据含temperature、pressure两列

ggplot(pressure,aes(x=temperature,y=pressure))+geom_point()

attachments-2017-07-l9ytk18F596c74263781ggplot(pressure,aes(x=temperature,y=pressure))+geom_point(shape=15,size=5,color=7)

attachments-2017-07-jzPHccOm596c746692fe

参数
①    geom_point()函数绘制散点图
②    size控制点的大小
③    color控制点的颜色
④    shape:控制点的形状可选的点的形状可参考下图

attachments-2017-07-qBgGYRsl596c744fee0f
采用ggplot2绘制分组的散点图
library(ggplot2)
library(gcookbook)
heightweight  #内置数据含 sex ageYear ageMonth heightIn weightLb 五列

#通过将变量sex映射给colour或shape可以按照变量sex对数据点进行分组
ggplot(heightweight,aes(x=ageYear, y=heightIn,colour=sex))+geom_point() #颜色

attachments-2017-07-f4X2CcXc596c751ee6a2

ggplot(heightweight,aes(x=ageYear, y=heightIn,shape=sex))+geom_point()  #形状

attachments-2017-07-hUcp7A7c596c75325168

ggplot(heightweight,aes(x=ageYear,y=heightIn,shape=sex,colour=weightLb))+geom_point() #颜色+形状注意shape不能取连续值

attachments-2017-07-wGsfopoJ596c754ce35b

ggplot(heightweight,aes(x=ageYear,y=heightIn,shape=sex))+geom_point()+scale_shape_manual(values=c(3,6))
#对默认给定的点的形状进行修改颜色同理scale_color_manual(values=c(*,*))

attachments-2017-07-T6xAhMMU596c7570774d

采用ggplot2绘制散点图

library(ggplot2)
setwd("d:/huitu")


data=read.table("data.txt",header=T,sep="\t")

#对数据进行简单的处理引入type变量将y1,y2,y3分别赋予1,2,3
vd = rbind(data.frame(v=data$x0, y=data$y1, type=as.factor(1)), data.frame(v=data$x0, y=data$y2,type=as.factor(2)),
data.frame(v=data$x0, y=data$y3, type=as.factor(3)))

ggplot(vd,aes(x=v,y=y,shape=type,color=type))+geom_point()+labs(title ="picture")+xlab("year")+ylab("weight")+scale_shape_manual(values=c(1,2,7))+scale_color_manual(values=c(1:3))+theme(plot.title=element_text(hjust=0.5))

参数
①    labs(title ="标题")给散点图添加标题
②    xlab("**")、ylab("**")添加x、y轴标题
③    color、shape可将分组变量赋值给颜色或形状属性实现分组散点图的绘制(默认给定颜色、形状)
④    scale_shape_manual(values=c(***))手动更改函数自定义点的形状
        scale_color_manual(values=c(***))手动更改函数自定义点的颜色
⑤    theme(plot.title=element_text(hjust=0.5))标题居中

attachments-2017-07-RmdSkuh5596c7354c355

  • 发表于 2017-07-17 16:22
  • 阅读 ( 13336 )
  • 分类:编程语言

0 条评论

请先 登录 后评论
不写代码的码农
爽儿

学生

25 篇文章

作家榜 »

  1. 祝让飞 118 文章
  2. 柚子 91 文章
  3. 刘永鑫 64 文章
  4. admin 57 文章
  5. 生信分析流 55 文章
  6. SXR 44 文章
  7. 张海伦 31 文章
  8. 爽儿 25 文章