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")