您的位置:首页 > 其它

R语言ggplot2画图

2016-03-19 13:19 246 查看
#1、基本要素

#数据(Data)和映射(Mapping)

#几何对象(Geometric)

#标尺(Scale)

#统计变换(Statistics)

#坐标系统(Coordinante)

#图层(Layer)

#分面(Facet)

#主题(Theme)

#2、数据和映射

require(ggplot2)

data(diamonds)

set.seed(42)

#在一个数据框里进行抽样1000行数据

small<-diamonds[sample(nrow(diamonds),1000),]

head(small)

length(small)#给出的时small的列数而不是行数

p<-ggplot(data=small,mapping=aes(x=carat,y=price))

p+geom_point()

#用一个类别因子指定图形类别分组用shape=

p <- ggplot(data=small, mapping=aes(x=carat, y=price, shape=cut))

p+geom_point()

#映射颜色colour

p <- ggplot(data=small, mapping=aes(x=carat, y=price, shape=cut, colour=color))

p+geom_point()

#3、几何对像

#ggplot2支持图层,我通常把不同的图层中共用的映射提供给ggplot函数

#而某一几何对象才需要的映射参数提供给geom_xxx函数。

ggplot(small)+geom_histogram(aes(x=price))

#填充颜色

ggplot(small)+geom_histogram(aes(x=price, fill=cut))

#side-by-side地画直方图

ggplot(small)+geom_histogram(aes(x=price, fill=cut), position="dodge")

ggplot(small)+geom_histogram(aes(x=price, fill=cut), position="fill")

ggplot(small)+geom_bar(aes(x=clarity))

#通过stat参数,可以让geom_bar按指定高度画图

ggplot()+geom_bar(aes(x=c(LETTERS[1:3]),y=1:3), stat="identity")

ggplot(small)+geom_density(aes(x=price, colour=cut))

ggplot(small)+geom_density(aes(x=price,fill=clarity))

#colour参数指定的是曲线的颜色,而fill是往曲线下面填充颜色。

ggplot(small)+geom_boxplot(aes(x=cut, y=price,fill=color))

geom_abline geom_area

geom_bar
geom_bin2d

geom_blank geom_boxplot

geom_contour geom_crossbar

geom_density geom_density2d

geom_dotplot geom_errorbar

geom_errorbarh geom_freqpoly

geom_hex geom_histogram

geom_hline
geom_jitter

geom_line geom_linerange

geom_map geom_path

geom_point geom_pointrange

geom_polygon geom_quantile

geom_raster geom_rect

geom_ribbon geom_rug

geom_segment geom_smooth

geom_step geom_text

geom_tile geom_violin

geom_vline

#4、标尺--坐标变化

ggplot(small)+geom_point(aes(x=carat, y=price, shape=cut, colour=color))+scale_y_log10()+scale_colour_manual(values=rainbow(7))

#5、统计变换

#统计变换对原始数据进行某种计算,然后在图上表示出来,例如对散点图上加一条回归线。

ggplot(small, aes(x=carat, y=price))+geom_point()+scale_y_log10()+stat_smooth()

#aes所提供的参数,就通过ggplot提供,而不是提供给geom_point,因为ggplot里的参数,

#相当于全局变量,geom_point()和stat_smooth()都知道x,y的映射,如果只提供给geom_point(),

#则相当于是局部变量,geom_point知道这种映射,而stat_smooth不知道,当然你再给stat_smooth也提供x,y的映射,不过共用的映射,还是提供给ggplot好。

stat_abline stat_contour stat_identity stat_summary

stat_bin stat_density stat_qq stat_summary2d

stat_bin2d stat_density2d stat_quantile stat_summary_hex

stat_bindot stat_ecdf stat_smooth stat_unique

stat_binhex stat_function stat_spoke stat_vline

stat_boxplot stat_hline stat_sum stat_ydensity

#6、坐标系统

#坐标系统控制坐标轴,可以进行变换,例如XY轴翻转,笛卡尔坐标和极坐标转换

ggplot(small)+geom_bar(aes(x=cut, fill=cut))+coord_flip()

ggplot(small)+geom_bar(aes(x=factor(1), fill=cut))+coord_polar(theta="y")

ggplot(small)+geom_bar(aes(x=factor(1), fill=cut))+coord_polar()

ggplot(small)+geom_bar(aes(x=clarity, fill=cut))+coord_polar()

#7、图层

#柱状图+误差图为实例,展示ggplot2非常灵活的图层

#8、分面

#分面可以让我们按照某种给定的条件,对数据进行分组,然后分别画图

ggplot(small, aes(x=carat, y=price))+geom_point(aes(colour=cut))+scale_y_log10() +facet_wrap(~cut)+stat_smooth()

#9、主题

#ggtitle(), xlab()和ylab()等

#改变字体,字体大小,坐标轴,背景等各种元素,这需要通过theme()函数来完成。

theme_economist theme_economist_white

theme_wsj theme_excel

theme_few theme_foundation

theme_igray theme_solarized

theme_stata theme_tufte

#10、二维密度

ggplot(diamonds, aes(carat, price))+ stat_density2d(aes(fill = ..level..), geom="polygon")+ scale_fill_continuous(high='darkred',low='darkgreen')
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: