码迷,mamicode.com
首页 > 其他好文 > 详细

《ggplot2-EGDA》 -2.1-to-2.5

时间:2017-10-01 15:14:14      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:density   and   object   lib   fill   number   variables   datatable   rar   

diamonds数据结构

 
  • ggplot2对数据框类型数据十分友好,colour\shape\size必须赋值等于常数数据框中的分类变量(因子类型数据)

技术分享

  • geom=point/smooth
dsmall <- diamonds[sample(nrow(diamonds), 100), ]

qplot(carat, price, data = diamonds)

##1.基本aesthetic attributes :colour\shape\size\alpha

qplot(carat, price, data = dsmall, colour = color) 
qplot(carat, price, data = dsmall, shape = cut) 
qplot(carat, price, data = diamonds, alpha = I(1/10)) 
###You can also manually set the aesthetics using I() 

#####2.geometric object
###2.1 smooth method到底用什么方法 可以加载不同的包 如MASS 按相应的回归formula输入即可
qplot(carat, price, data = dsmall, geom = c("point", "smooth")) #####默认小样本(小于1000)使用局部回归
qplot(carat, price, data = dsmall, geom = c("point", "smooth"),span = 0.2) #span调整平滑程度 
library(mgcv)
qplot(carat, price, data = dsmall, geom = c("point", "smooth"),method = "gam", formula = y ~ s(x, bs = "cs")) #####默认大样本(大于1000)的回归算法
qplot(carat, price, data = dsmall, geom = c("point", "smooth"),method = "lm") #最小二乘
  • Boxplots and jittered points(扰动点图)
  • geom=jitter/boxplot
  • how the values of the continuous variables vary with the levels of the categorical variable(一个连续变量、一个分类变量)
qplot(color, price / carat, data = diamonds, geom = "jitter",alpha = I(1 / 5)) #第一个参数是分类变量,第二个是连续变量
qplot(cut,price/carat,data=dsmall,geom=c(jitter,boxplot),alpha=I(0.5),colour=cut)##jitter boxplot 可以一起用
##boxplot中colour是外框线颜色,fill是填充颜色,size是线的粗细
  • geom=histogram\density\bar
  • histogram:binwidth(组距)、xlim=c(...,...)
  • density,adjust(取值0到1,平滑程度)
qplot(carat, data = diamonds, geom = "histogram", binwidth = 0.01,xlim = c(0,3))
##histogram默认Y轴为频数

qplot(carat, data = diamonds, geom = "histogram", fill = color) #使用fill参数可以很快得到堆积柱形图

qplot(color, data = diamonds, geom = "bar", weight = carat) +scale_y_continuous("carat") 
#y轴不再是频数,而是对给定的weight求和,+号后面的代码是变坐标轴名称count为carat
  • geom=line/path
year <- function(x) as.POSIXlt(x)$year + 1900  #as.POSIXlt(x)读取的日期(字符串) 转化为 “Date” 类型数据
qplot(unemploy / pop, uempmed, data = economics,geom = c("point", "path"))
qplot(unemploy / pop, uempmed, data = economics,geom = "path", colour =year(date)) + scale_size_area()  #按年份更换颜色

 

《ggplot2-EGDA》 -2.1-to-2.5

标签:density   and   object   lib   fill   number   variables   datatable   rar   

原文地址:http://www.cnblogs.com/winda921/p/7617181.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!