• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

R语言ggplot画图入门

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

工具:R+ggplot2工具包

ggplot画图比较简单就直接上代码了

 

library(ggplot2) #加载工具包

mtcars #使用R的数据集

 

plot(mtcars$mpg,mtcars$drat) #将mpg和drat按照(x,y)做散点图

plot(mtcars$mpg,mtcars$drat,type = ‘b’) #将mpg和drat按照(x,y)做线图, 

其中,type有以下赋值:

"p" for points,

线"l" for lines,

点线"b" for both,

点线图去掉点"c" for the lines part alone of "b",

覆盖式的电线"o" for both ‘overplotted’,

类似直方图"h" for ‘histogram’ like (or ‘high-density’) vertical lines,

楼梯状"s" for stair steps,

楼梯状"S" for other steps, see ‘Details’ below,

不显示"n" for no plotting.

 

qplot(mtcars$mpg,mtcars$drat)

ggplot(mtcars,aes(mpg,drat)) + geom_point() #散点图

 

用R中自带数据集Pressure

plot(pressure$temperature,pressure$pressure,type = 'b')

qplot(pressure$temperature,pressure$pressure,geom = c("line","point"))

ggplot(pressure,aes(temperature,pressure)) + geom_point() +geom_line() 

 

mtcars

p<-ggplot(mtcars,aes(mpg,drat,colour = factor(carb))) + geom_point(aes(size =gear))

p + annotate("text",x = 20,y = 4,label = "hjhjhjh") + geom_hline(yintercept = 4) + geom_vline(xintercept = 20)

# 按照gear大小分类,按照carb进行颜色区分

#做直线x=20,y=4,并在(20,4)处标记“hjhjhjh”

 

p + facet_grid(vs ~ .) #以VS为分类变量

p + facet_grid(. ~ vs) #以VS为分类变量

p + facet_grid(vs ~ am, scales = "free") #以vs和am为分类变量

 

p + coord_flip() #将X,Y相互颠倒 

p1 <- p + ylim(3.5,5) + xlim(15,30) #限定坐标轴的范围

p2 <- p1 + scale_x_continuous(breaks = c(15,20,25,30),labels = c("A","B","C","D"))

p2 + scale_y_continuous(breaks = c(3.5,4.0,4.5,5.0),labels = c("A","B","C","D"))

#将X和Y轴的轴标签改为A,B,C,D

暂时写到这里,如有错误欢迎指正。

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap