美文网首页
R探索性数据分析cut_number, cut_width, b

R探索性数据分析cut_number, cut_width, b

作者: liyin_d64b | 来源:发表于2018-10-23 18:00 被阅读0次
ggplot(data = diamonds,
       mapping = aes(color = cut_number(carat, 5), x = price)) +
  geom_freqpoly() +
  ylab("Carat")
image
ggplot(data = diamonds,
       mapping = aes(color = cut_width(carat, 0.5, boundary = 0), x = price)) +
  geom_freqpoly() +
  ylab("Carat")
image

Exercise 7.5.3.1.2

Visualize the distribution of carat, partitioned by price.

Plotted with a box plot with 10 bins with an equal number of observations, and the width determined by the number of observations.

ggplot(diamonds, aes(x = cut_number(price, 10), y = carat)) +
  geom_boxplot() +
  coord_flip() +
  xlab("Price")
image

Plotted with a box plot with 10 equal-width bins of 2,000. The argument boundary = 0 ensures that first bin is 0–2,000.

ggplot(diamonds, aes(x = cut_width(price, 2000, boundary = 0), y = carat)) +
  geom_boxplot(varwidth = TRUE) +
  coord_flip() +
  xlab("Price")
image
install.packages("hexbin")
library(hexbin)
ggplot(data = diamonds) + 
  geom_hex(mapping = aes(x = carat, y = price))
ggplot(data = diamonds) + 
  geom_bin2d(mapping = aes(x = carat, y = price))
ggplot(data = diamonds, mapping = aes(x = carat, y = price)) + 
  geom_boxplot(mapping = aes(group = cut_width(carat, 0.5)),varwidth = TRUE)

ggplot(data = smaller, mapping = aes(x = carat, y = price)) + 
  geom_boxplot(mapping = aes(group = cut_number(carat, 20))) #20是需要的组数

ggplot(data = diamonds,
       mapping = aes(color = cut_number(carat, 5), x = price)) +
  geom_freqpoly() + 
  ylab("Carat")

diamonds %>%
  count(cut_width(carat,0.15,boundary = 0.2)) #数字分割,左边第一个为0.2,间隔0.15



cut_number #把数字分为多少组
cut_width #把数字以多大分为一组
#按照价格划分克拉
ggplot(diamonds,aes(y=carat,x=cut_number(price,10)))+
  geom_boxplot(mapping = aes(color=cut_number(price,10)))+
  coord_flip()+
  xlab("Price")
       


ggplot(diamonds, aes(x = cut_width(price, 2000, boundary = 0), y = carat)) +
  geom_boxplot(varwidth = TRUE,mapping = aes(color=cut_width(price,2000,boundary = 0))) +
  coord_flip() +
  xlab("Price")

相关文章

网友评论

      本文标题:R探索性数据分析cut_number, cut_width, b

      本文链接:https://www.haomeiwen.com/subject/bpwazftx.html