加载ggplot2: library(‘ggplot2’) 1.条形图 基本命令:qplot(region, data = example1, geom = ‘bar’) 将标题居中放置:theme_update(plot.title = element_text(hjust = 0.5)) qplot(region, data=example1, geom=‘bar’, main=‘标题’, ylab=‘count’) 2.直方图 基本命令:qplot(Price, data=example1, geom=‘histogram’) 调整宽度: qplot(Price, data=example1, geom=‘histogram’, binwidth=5) qplot(Price, data=example1, geom=‘histogram’, binwidth=5, main=‘标题’, ylab=‘frequency’) 调整坐标轴范围: qplot(Price, data=example1, geom=‘histogram’, binwidth=5, main=‘标题’, ylab=‘frequency’, xlim=c(0,8), ylim=c(0,5)) 填充颜色: qplot(Price, data=example1, geom=‘histogram’, binwidth=5, main=‘标题’, ylab=‘frequency’, fill=I(‘blue’)) 添加边框颜色: qplot(Price, data=example1, geom=‘histogram’, binwidth=5, main=‘标题’, ylab=‘frequency’, fill=I(‘sky blue’), col=I(‘red’)) 调整透明度: qplot(Price, data=example1, geom=‘histogram’, binwidth=5, main=‘标题’, ylab=‘frequency’, fill=I(‘blue’), alpha=I(0.8)) 3.箱线图 qplot(x=“1”, y=Price, data=example1, geom=‘boxplot’) qplot(x=“1”, y=Price, data=example1, geom=‘boxplot’, xlab=‘Price’) qplot(x=region, y=Price, data=example1, geom=‘boxplot’) qplot(x=region, y=Price, data=example1, geom=‘boxplot’, fill=region) qplot(x=region, y=Price, data=example1, geom=‘boxplot’, fill=region, outlier.shape=NA) qplot(x=region, y=Price, data=example1, geom=c(‘boxplot’,‘jitter’), fill=region, outlier.shape=NA) 4.散点图 qplot(x=Kilogram, y=Price, data=example1, geom=‘point’) qplot(x=Kilogram, y=Price, data=example1, geom=‘point’, ylab=‘Price’) 调整散点大小: qplot(x=Kilogram, y=Price, data=example1, geom=‘point’, ylab=‘Price’,size=I(3)) 调整散点形状: qplot(x=Kilogram, y=Price, data=example1, geom=‘point’, ylab=‘Price’, shape=I(‘triangle’)) qplot(x=Kilogram, y=Price, data=example1, geom=‘point’, ylab=‘Price’, col=Region) qplot(x=Kilogram, y=Price, data=example1, geom=‘point’, ylab=‘Price’, col=Region, shape=region) 5.分组画图 qplot(x=Kilogram, y=Price, data=example1, geom=‘point’, ylab=‘Price’, facets=Region~Sex) qplot(x=Kilogram, y=Price, data=example1, geom=‘point’, ylab=‘Price’, facets=Region~.) qplot(x=Kilogram, y=Price, data=example1, geom=‘point’, ylab=‘Price’, facets=.~Sex) qplot(x=Kilogram, y=Price, data=example1, geom=‘point’, ylab=‘Price’, facets=.~Sex, col=region) qplot(x=Sex, y=Price, data=example1, geom=‘boxplot’, fill=Sex) qplot(x=Sex, y=Price, data=example1, geom=‘boxplot’, fill=Sex, facets=.~Region) qplot(x=Sex, y=Price, data=example1, geom=‘boxplot’, fill=Sex, facets=.~Region, main=‘Age by Sex and Region’)
|