关于数据diamonds的一些作图

library(ggplot2)
data(diamonds)
# 基础直方图
ggplot(diamonds, aes(x = price)) +
  geom_histogram(bins = 50, fill = "#69b3a2", color = "white") +
  labs(
    title = "钻石价格分布直方图",
    x = "价格(美元)",
    y = "频数"
  ) +
  theme_minimal()

 #添加对数变换(解决右偏分布)
ggplot(diamonds, aes(x = price)) +
  geom_histogram(fill = "steelblue", bins = 50) +
  scale_x_log10(labels = scales::dollar) +  # 对价格取对数
  labs(title = "价格分布(对数尺度)")

# 计算颜色和切工组合的平均价格
library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
stack_data <- diamonds %>%
  group_by(color, cut) %>%
  summarise(mean_price = mean(price), .groups = "drop")%>%
mutate(mean_price = round(mean_price,1)) #保留一位小数

ggplot(stack_data, aes(x = color, y = mean_price, fill = cut)) +
  geom_col(position = "stack") +
  geom_text(aes(label=mean_price),position=position_stack(0.5),size=3)+
  scale_fill_brewer(palette = "Paired") +
  labs(
    title = "颜色与切工组合的平均价格堆积图",
    x = "颜色等级",
    y = "平均价格(美元)"
  )

ggplot(diamonds, aes(x = color, y = carat, fill = color)) +
  geom_violin(alpha = 0.7, trim = FALSE) +
  geom_boxplot(width = 0.1, fill = "white", outlier.alpha = 0.3) +
  labs(
    title = "不同颜色等级的克拉分布",
    x = "颜色等级(D→J)",
    y = "克拉"
  ) +
  scale_fill_brewer(palette = "Spectral") +
  coord_flip()  # 横向显示

# 使用分面和颜色展示三维关系
ggplot(diamonds, aes(x = depth, y = price, color = carat)) +
  geom_point(alpha = 0.3) +
  facet_wrap(~ cut, scales = "free_y") +  # 按切工分面
  scale_color_viridis_c(option = "plasma") +  # 颜色表示克拉
  labs(
    title = "深度、价格与克拉的关系(按切工分面)",
    x = "深度(%)",
    y = "价格(美元)",
    color = "克拉"
  )

# 散点图(带透明度处理大数据点)
ggplot(diamonds, aes(x = carat, y = price)) +
  geom_point(alpha = 0.1, color = "darkorange") +  # 透明度避免重叠
  geom_smooth(method = "lm", color = "red") +       # 添加线性趋势线
  labs(
    title = "克拉重量与价格关系",
    x = "克拉",
    y = "价格(美元)"
  )
`geom_smooth()` using formula = 'y ~ x'

# 按切工分面(展示分组差异)
ggplot(diamonds, aes(x = carat, y = price, color = cut)) +
  geom_point(alpha = 0.2) +
  facet_wrap(~ cut) +  # 按切工分面
  scale_y_log10() +    # Y轴取对数
  labs(title = "克拉 vs 价格(按切工分组)")

ggplot(diamonds, aes(x = cut, y = price, fill = cut)) +
  geom_boxplot(alpha = 0.7, outlier.alpha = 0.3) +  # 隐藏部分离群点
  scale_y_log10(labels = scales::dollar) +          # Y轴取对数
  labs(
    title = "不同切工等级的价格分布",
    x = "切工等级",
    y = "价格(对数尺度)"
  ) +
  scale_fill_brewer(palette = "Set3") +  # 使用调色板
  theme(legend.position = "none")        # 隐藏图例

# 计算颜色和净度的平均价格
library(dplyr)
heatmap_data <- diamonds %>%
  group_by(color, clarity) %>%
  summarise(mean_price = mean(price), .groups = "drop")

# 绘制热力图
ggplot(heatmap_data, aes(x = color, y = clarity, fill = mean_price)) +
  geom_tile(color = "white") +
  scale_fill_viridis_c(option = "magma", labels = scales::dollar) +  # 颜色方案
  labs(
    title = "颜色与净度的平均价格热力图",
    x = "颜色等级(D 最佳 → J 最差)",
    y = "净度等级",
    fill = "平均价格"
  ) +
  theme_minimal()

ggplot(diamonds, aes(x = carat, fill = color)) +
  geom_density(alpha = 0.5) +
  labs(
    title = "克拉重量分布(按颜色等级分组)",
    x = "克拉",
    y = "密度"
  ) +
  scale_fill_brewer(palette = "Spectral")  # 彩虹色系

install.packages("treemapify")
Warning: package 'treemapify' is in use and will not be installed
library(treemapify)

# 计算各净度等级的总价值
treemap_data <- diamonds %>%
  group_by(clarity) %>%
  summarise(total_value = sum(price), .groups = "drop")

ggplot(treemap_data, aes(area = total_value, fill = clarity, label = clarity)) +
  geom_treemap() +
  geom_treemap_text(color = "white", place = "centre") +
  scale_fill_viridis_d() +
  labs(title = "各净度等级的总价值占比")

install.packages("ggridges")
Warning: package 'ggridges' is in use and will not be installed
library(ggridges)

ggplot(diamonds, aes(x = carat, y = cut, fill = cut)) +
  geom_density_ridges(alpha = 0.7, scale = 1.2) +
  scale_fill_brewer(palette = "Set2") +
  labs(
    title = "不同切工等级的克拉密度分布",
    x = "克拉",
    y = "切工等级"
  )
Picking joint bandwidth of 0.0647