您的位置:首页 > 其它

[置顶] 【R语言 数据探索】实战 Diamonds数据集探索

2017-07-01 23:48 274 查看
1.主要内容

查看数据
diamonds

截取子集
set.seed(123)
diamonds <- diamonds[sample(nrow(diamonds), 1000),]

查看概要
summary()、str()

探索
价格和克拉的关系:geom_point(),映射颜色和形状

价格分布:geom_histogram(),映射填充、position="fill"/"dodge"

透明度分布:geom_bar()

价格概率分布:geom_density(),映射颜色、填充

不同切工下的价格分布:geom_boxplot(),映射填充

坐标变换:scale_y_log10()

加上坐标轴标签和标题:labs(x="", y="", title="")


2.数据集



rm(list=ls())
gc()

options(scipen = 200)

# 加载包和数据集
library(ggplot2)
library(gcookbook)
diamonds <- diamonds

# 截取子集
set.seed(123)
# 从全部行中采样出1000行
diamonds <- diamonds[sample(nrow(diamonds), 1000),]

# 查看数据框的一些概要信息
summary(diamonds)
str(diamonds)

# 查看数据框的前几行或最后几行
head(diamonds)
tail(diamonds)

# 价格和克拉的关系
ggplot(diamonds) + geom_point(aes(x=carat, y=price))
# 加入color和cut的影响
ggplot(diamonds) + geom_point(aes(x=carat, y=price, color=color, shape=cut))

# 价格分布
ggplot(diamonds) + geom_histogram(aes(x=price))
# 加入cut的影响
ggplot(diamonds) + geom_histogram(aes(x=price, fill=cut))
# 分组直方图
ggplot(diamonds) + geom_histogram(aes(x=price, fill=cut), position="dodge")
# 百分比直方图
ggplot(diamonds) + geom_histogram(aes(x=price, fill=cut), position="fill")

# 纯净度分布
ggplot(diamonds) + geom_bar(aes(x=clarity))
# 加入color的影响
ggplot(diamonds) + geom_bar(aes(x=clarity, fill=color))

# 价格的概率分布
ggplot(diamonds) + geom_density(aes(x=price))
# 加入cut的影响
ggplot(diamonds) + geom_density(aes(x=price, color=cut))
# 加入color的影响
ggplot(diamonds) + geom_density(aes(x=price, color=color))

# 不同切工下价格的分布
ggplot(diamonds) + geom_boxplot(aes(x=cut, y=price))
# 加入color的影响
ggplot(diamonds) + geom_boxplot(aes(x=cut, y=price, fill=color))

# 坐标变换
ggplot(diamonds) + geom_point(aes(x=carat, y=price, color=color, shape=cut)) + scale_y_log10()

# 加上标题和坐标轴标签
ggplot(diamonds) + geom_point(aes(x=carat, y=price, color=color, shape=cut)) + scale_y_log10() + labs(x='克拉', y='价格', title='克拉和价格之间的关系') + theme(text=element_text(family='Microsoft YaHei'))

?theme
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: