R语言之ggplot2画图篇
2014-11-19 22:31
369 查看
转载自:http://blog.csdn.net/lilanfeng1991/article/details/28665255
quick plot
数据集:diamonds
eg
[plain] view
plaincopy
library(ggplot2)
length(diamonds)
set.seed(1410)#设定种子数
dsmall<-diamonds[sample(nrow(diamonds),100),]#随机产生样本数
qplot(carat,price,data=diamonds)#画散点图
[plain] view
plaincopy
qplot(log(carat),log(price),data=diamonds)
color、size、shape
[plain] view
plaincopy
qplot(carat,price,data=dsmall,colour=color)
[plain] view
plaincopy
qplot(carat,price,data=dsmall,shape=cut)
[plain] view
plaincopy
<span style="font-family: Arial, Helvetica, sans-serif;">qplot(carat,price,data=dsmall,alpha=I(1/20))</span>
设置透明度
取值可以为:point、smooth、boxplot、path、line
对于连续型变量:geom可取histogram、freqpoly、density
对于离散型变量:bar
[plain] view
plaincopy
qplot(carat,price,data=dsmall,geom=c("point","smooth"))
[plain] view
plaincopy
qplot(carat,price,data=dsmall,geom=c("point","smooth"),span=0.2)
[plain] view
plaincopy
qplot(carat,price,data=dsmall,geom=c("point","smooth"),span=1)
[plain] view
plaincopy
mpg
head(mpg)
p<-ggplot(data=mpg,mapping=aes(x=cty,y=hwy))
p+geom_point()
summary(p)
summary(p+geom_point())
[plain] view
plaincopy
#将年份映射到颜色属性
p<-ggplot(mpg,aes(x=cty,y=hwy,colour=factor(year)))
p+geom_point()
[plain] view
plaincopy
p+geom_point()+stat_smooth()
[plain] view
plaincopy
p<-ggplot(mpg,aes(x=ctymy=hwy))
p+geom_point(aes(colour=factor(year)))+stat_smooth()
[plain] view
plaincopy
#方法一
p<-ggplot(mpg,aes(x=cty,y=hwy))
p+geom_point(aes(colour=factor(year)))+stat_smooth()
#方法二
d<-ggplot()+
geom_point(data=mpg,aes(x=cty,y=hwy,colour=factor(year)))+
stat_smooth(data=mpg,aes(x=cty,y=hwy))#此时除了底层画布外,有两个图层,分别定义了geom和stat
plaincopy
p<-ggplot(mpg,aes(x=cty,y=hwy))
p+geom_point(aes(colour=factor(year)))+stat_smooth()+scale_color_manual(values=c("blue","red"))
[plain] view
plaincopy
p+geom_point(aes(colour=factor(year),size=displ))+
stat_smooth()+
scale_color_manual(values=c("blue2","red4"))
[plain] view
plaincopy
p+geom_point(aes(colour=factor(year),size=displ),alpha=0.5,position="jitter")+
stat_smooth()+
scale_color_manual(values=c("blue2","red4"))+
scale_size_continuous(range=c(4,10))
[plain] view
plaincopy
p+geom_point(aes(colour=factor(year),size=displ),alpha=0.5,position="jitter")+
stat_smooth()+
scale_color_manual(values=c("blue2","red4"))+
scale_size_continuous(range=c(4,10))+
coord_cartesian(xlim=c(15,25),ylim=c(15,40))
[plain] view
plaincopy
p+geom_point(aes(colour=class,size=displ),alpha=0.5,position="jitter")+
stat_smooth()+
scale_size_continuous(range=c(4,10))+
facet_wrap(~year,ncol=1)
[plain] view
plaincopy
p<-ggplot(mpg,aes(x=cty,y=hwy))
p+geom_point(aes(colour=class,size=displ),alpha=0.5,position="jitter")+
stat_smooth()+
scale_size_continuous(range=c(4,10))+
facet_wrap(~year,ncol=1)+
opts(title='汽车油耗与型号')+
labs(x="每加仑高速公路行驶距离",y="每加仑城市公路行驶距离")+
guides(size=guide_legend(title="排量"),colour=guide_legend(titile="车型",override.aes=list(size=5)))
[plain] view
plaincopy
p<-ggplot(mpg,aes(x=hwy))
p+geom_histogram()
直方图的几何对象中内置有默认的统计变换
[plain] view
plaincopy
p+geom_histogram(aes(fill=factor(year),y=..density..),alpha=0.3,colour="black")+
stat_density(geom="line",position="identity",size=1.5,aes(colour=factor(year)))+
facet_wrap(~year,ncol=1)
[plain] view
plaincopy
p<-ggplot(mpg,aes(x=class))
p+geom_bar()
[plain] view
plaincopy
class2<-mpg$class
class2<-reorder(class2,class2,length)
mpg$class2<-class2
p<-ggplot(mpg,aes(x=class2))
p+geom_bar(aes(fill=class2))
[plain] view
plaincopy
p<-ggplot(mpg,aes(class2,fill=factor(year)))
p+geom_bar(position="identity",alpha=0.5)
并立方式
[plain] view
plaincopy
p+geom_bar(position="dodge")
叠加方式
[plain] view
plaincopy
p+geom_bar(position="stack")
相对比例
[plain] view
plaincopy
p+geom_bar(position="fill")
分面显示
[plain] view
plaincopy
p+geom_bar(aes(fill=class2))+facet_wrap(~year)
[plain] view
plaincopy
p<-ggplot(mpg,aes(x=factor(1),fill=factor(class)))+geom_bar(width=1)
p+coord_polar(theta="y")
[plain] view
plaincopy
p<-ggplot(mpg,aes(class,hwy,fill=class))
p+geom_boxplot()
[plain] view
plaincopy
p+geom_violin(alpha=0.3,width=0.9)+geom_jitter(shape=21)
增加扰动(jitter)
增加透明度(alpha)
二维直方图(stat_bin2d)
密度图(stat_density2d)
[plain] view
plaincopy
p<-ggplot(diamonds,aes(carat,price))
p+stat_bin2d(bins=60)
[plain] view
plaincopy
p+stat_density2d(aes(fill=..level..),geom="polygon")+
coord_cartesian(xlim=c(0,1.5),ylim=c(0,6000))+
scale_fill_continuous(high="red2",low="blue4")
[plain] view
plaincopy
#随机生成100次风向,并洪到16个敬意内
dir<-cut_interval(runif(100,0,360),n=16)
#随机生成100次风速,并划分成4种强度
mag<-cut_interval(rgamma(100,15),4)
sample<-data.frame(dir=dir,mag=mag)
#将风向映射到x轴,频数映射到y轴,风速大小映射到填充色,生成条形图后再转为极坐标形式即可
p<-ggplot(sample,aes(x=dir,y=..count..,fill=mag))
p+geom_bar()+coord_polar()
1. qplot
quick plot数据集:diamonds
(1)基本用法
eg[plain] view
plaincopy
library(ggplot2)
length(diamonds)
set.seed(1410)#设定种子数
dsmall<-diamonds[sample(nrow(diamonds),100),]#随机产生样本数
qplot(carat,price,data=diamonds)#画散点图
[plain] view
plaincopy
qplot(log(carat),log(price),data=diamonds)
color、size、shape
[plain] view
plaincopy
qplot(carat,price,data=dsmall,colour=color)
[plain] view
plaincopy
qplot(carat,price,data=dsmall,shape=cut)
[plain] view
plaincopy
<span style="font-family: Arial, Helvetica, sans-serif;">qplot(carat,price,data=dsmall,alpha=I(1/20))</span>
设置透明度
(2)geom
取值可以为:point、smooth、boxplot、path、line对于连续型变量:geom可取histogram、freqpoly、density
对于离散型变量:bar
[plain] view
plaincopy
qplot(carat,price,data=dsmall,geom=c("point","smooth"))
[plain] view
plaincopy
qplot(carat,price,data=dsmall,geom=c("point","smooth"),span=0.2)
[plain] view
plaincopy
qplot(carat,price,data=dsmall,geom=c("point","smooth"),span=1)
2.ggplot2
数据(Data)和映射(Mapping) | 将数据中的变量映射到图形属性,映射控制了二者之间的关系 |
标度(Scale) | 标度负责控制映射后图形属性的显示方式,具体形式上看是图例和坐标刻度 |
几何对象(Geometric) | 几何对象代表我们在图中实际看到的图形元素,如点、线、多边形等 |
统计变换(Statistics) | 对原始数据进行某种计算,如对二元菜点图加上一条回归线 |
坐标系统(Coordinate) | 坐标系统控制坐标轴并影响所有图形元素,坐标轴可以进行变换以满足不同的需要 |
图层(Layer) | 数据、映射、几何对象、统计变换等构成一个图层,图层可以允许用户一步步的构建图形,方便单独对图层进行修改 |
分面(Facet) | 条件绘图,将数据按某种方式分组,然后分别绘图。分布就是控制分组绘图的方式和排列形式 |
(1)geom_point
[plain] viewplaincopy
mpg
head(mpg)
p<-ggplot(data=mpg,mapping=aes(x=cty,y=hwy))
p+geom_point()
summary(p)
summary(p+geom_point())
[plain] view
plaincopy
#将年份映射到颜色属性
p<-ggplot(mpg,aes(x=cty,y=hwy,colour=factor(year)))
p+geom_point()
(2)增加平滑线
[plain] viewplaincopy
p+geom_point()+stat_smooth()
[plain] view
plaincopy
p<-ggplot(mpg,aes(x=ctymy=hwy))
p+geom_point(aes(colour=factor(year)))+stat_smooth()
(3)两种等价的绘图方式
[plain] viewplaincopy
#方法一
p<-ggplot(mpg,aes(x=cty,y=hwy))
p+geom_point(aes(colour=factor(year)))+stat_smooth()
#方法二
d<-ggplot()+
geom_point(data=mpg,aes(x=cty,y=hwy,colour=factor(year)))+
stat_smooth(data=mpg,aes(x=cty,y=hwy))#此时除了底层画布外,有两个图层,分别定义了geom和stat
(4)用标度来修改颜色取值
[plain] viewplaincopy
p<-ggplot(mpg,aes(x=cty,y=hwy))
p+geom_point(aes(colour=factor(year)))+stat_smooth()+scale_color_manual(values=c("blue","red"))
(5)将排量映射到散点大小
[plain] viewplaincopy
p+geom_point(aes(colour=factor(year),size=displ))+
stat_smooth()+
scale_color_manual(values=c("blue2","red4"))
[plain] view
plaincopy
p+geom_point(aes(colour=factor(year),size=displ),alpha=0.5,position="jitter")+
stat_smooth()+
scale_color_manual(values=c("blue2","red4"))+
scale_size_continuous(range=c(4,10))
(6)用坐标控制图形显示的范围
[plain] viewplaincopy
p+geom_point(aes(colour=factor(year),size=displ),alpha=0.5,position="jitter")+
stat_smooth()+
scale_color_manual(values=c("blue2","red4"))+
scale_size_continuous(range=c(4,10))+
coord_cartesian(xlim=c(15,25),ylim=c(15,40))
(7)利用facet分别显示不同年份的数据
[plain] viewplaincopy
p+geom_point(aes(colour=class,size=displ),alpha=0.5,position="jitter")+
stat_smooth()+
scale_size_continuous(range=c(4,10))+
facet_wrap(~year,ncol=1)
(8)增加图名并精细修改图例
[plain] viewplaincopy
p<-ggplot(mpg,aes(x=cty,y=hwy))
p+geom_point(aes(colour=class,size=displ),alpha=0.5,position="jitter")+
stat_smooth()+
scale_size_continuous(range=c(4,10))+
facet_wrap(~year,ncol=1)+
opts(title='汽车油耗与型号')+
labs(x="每加仑高速公路行驶距离",y="每加仑城市公路行驶距离")+
guides(size=guide_legend(title="排量"),colour=guide_legend(titile="车型",override.aes=list(size=5)))
(9)直方图
[plain] viewplaincopy
p<-ggplot(mpg,aes(x=hwy))
p+geom_histogram()
直方图的几何对象中内置有默认的统计变换
[plain] view
plaincopy
p+geom_histogram(aes(fill=factor(year),y=..density..),alpha=0.3,colour="black")+
stat_density(geom="line",position="identity",size=1.5,aes(colour=factor(year)))+
facet_wrap(~year,ncol=1)
(10)条形图
[plain] viewplaincopy
p<-ggplot(mpg,aes(x=class))
p+geom_bar()
[plain] view
plaincopy
class2<-mpg$class
class2<-reorder(class2,class2,length)
mpg$class2<-class2
p<-ggplot(mpg,aes(x=class2))
p+geom_bar(aes(fill=class2))
[plain] view
plaincopy
p<-ggplot(mpg,aes(class2,fill=factor(year)))
p+geom_bar(position="identity",alpha=0.5)
并立方式
[plain] view
plaincopy
p+geom_bar(position="dodge")
叠加方式
[plain] view
plaincopy
p+geom_bar(position="stack")
相对比例
[plain] view
plaincopy
p+geom_bar(position="fill")
分面显示
[plain] view
plaincopy
p+geom_bar(aes(fill=class2))+facet_wrap(~year)
(11)饼图
[plain] viewplaincopy
p<-ggplot(mpg,aes(x=factor(1),fill=factor(class)))+geom_bar(width=1)
p+coord_polar(theta="y")
(12)箱线图
[plain] viewplaincopy
p<-ggplot(mpg,aes(class,hwy,fill=class))
p+geom_boxplot()
[plain] view
plaincopy
p+geom_violin(alpha=0.3,width=0.9)+geom_jitter(shape=21)
3.观察密集散点的方法
增加扰动(jitter)增加透明度(alpha)
二维直方图(stat_bin2d)
密度图(stat_density2d)
[plain] view
plaincopy
p<-ggplot(diamonds,aes(carat,price))
p+stat_bin2d(bins=60)
[plain] view
plaincopy
p+stat_density2d(aes(fill=..level..),geom="polygon")+
coord_cartesian(xlim=c(0,1.5),ylim=c(0,6000))+
scale_fill_continuous(high="red2",low="blue4")
4.风向风速玫瑰图
[plain] viewplaincopy
#随机生成100次风向,并洪到16个敬意内
dir<-cut_interval(runif(100,0,360),n=16)
#随机生成100次风速,并划分成4种强度
mag<-cut_interval(rgamma(100,15),4)
sample<-data.frame(dir=dir,mag=mag)
#将风向映射到x轴,频数映射到y轴,风速大小映射到填充色,生成条形图后再转为极坐标形式即可
p<-ggplot(sample,aes(x=dir,y=..count..,fill=mag))
p+geom_bar()+coord_polar()
相关文章推荐
- R语言利用ggplot画图时调整坐标轴字体大小
- [R语言]ggplot2画图保存方法
- R语言ggplot2画图
- R语言画图功能使用手册
- R语言_ggplot2 分面相关设置(facet)
- 利用R语言画图时添加希腊符号标注
- R语言画图中输出上或者下标的方法
- R语言画图
- R语言图层—利用ggplot画蝙蝠侠logo及分层
- R语言ggplot2之图例的设置
- R语言的多维可视化方法(ggplot二维图表现多维)
- R语言可视化---介绍一些强大的画图和数据处理包及函数(ggplot,melt())
- R画图之ggplot2-->柱状图
- [置顶] 【R语言 可视化】R语言画图增加次要刻度线
- R语言-画图(下)
- R语言学习——画图
- R语言ggplot包之数据分布汇总
- R语言学习记录-ggplot2练习-经济学人统计图临摹(1)
- R语言画图大全(实战3,6,11)
- R语言︱SNA-社会关系网络—igraph包(社群划分、画图)(三)