Skip to content

Commit

Permalink
将图例置于绘图区域,必须设置 legend.position = "inside" 以适应 ggplot2 3.5.0 的 guide…
Browse files Browse the repository at this point in the history
…s 系统
  • Loading branch information
XiangyunHuang committed Jan 17, 2024
1 parent 7141037 commit 36d7ba0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
2 changes: 2 additions & 0 deletions gaussian-processes-regression.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,7 @@ ggplot(data = rongelap_sf)+
theme_bw() +
labs(x = "横坐标(米)", y = "纵坐标(米)", colour = "辐射强度") +
theme(
legend.position = "inside",
legend.position.inside = c(0.75, 0.1),
legend.direction = "horizontal",
legend.background = element_blank()
Expand Down Expand Up @@ -850,6 +851,7 @@ p3 <- ggplot() +
theme_bw() +
labs(x = "横坐标(米)", y = "纵坐标(米)", fill = "辐射强度") +
theme(
legend.position = "inside",
legend.position.inside = c(0.75, 0.1),
legend.direction = "horizontal",
legend.background = element_blank()
Expand Down
14 changes: 6 additions & 8 deletions visualization-advanced.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@ Stacked density plots: if you want to create a stacked density plot, you probabl
ggplot(data = province_sex_ratio, aes(x = `性别比(女=100)`, y = after_stat(density))) +
geom_density(aes(fill = `区域`), position = "stack", alpha = 0.5) +
theme_classic() +
theme(legend.position.inside = c(0.9, 0.8))
theme(legend.position = "inside", legend.position.inside = c(0.9, 0.8))
ggplot(data = province_sex_ratio, aes(x = `性别比(女=100)`, y = after_stat(density * n))) +
geom_density(aes(fill = `区域`), position = "stack", alpha = 0.5) +
theme_classic() +
theme(legend.position.inside = c(0.9, 0.8))
theme(legend.position = "inside", legend.position.inside = c(0.9, 0.8))
```

什么原因导致 @fig-density-stack 中两个子图看起来没什么差别呢?而换一组数据,就可以看出明显的差别。
Expand All @@ -171,14 +171,14 @@ ggplot(data = province_sex_ratio, aes(x = `性别比(女=100)`, y = after_st
ggplot(diamonds, aes(x = carat, y = after_stat(density), fill = cut)) +
geom_density(position = "stack") +
theme_classic() +
theme(legend.position.inside = c(0.8, 0.8))
theme(legend.position = "inside", legend.position.inside = c(0.8, 0.8))
ggplot(diamonds, aes(x = carat, y = after_stat(density * n), fill = cut)) +
geom_density(position = "stack") +
scale_y_continuous(
breaks = c(25000, 50000, 75000),
labels = c("25K", "50K", "75K")) +
theme_classic() +
theme(legend.position.inside = c(0.8, 0.8))
theme(legend.position = "inside", legend.position.inside = c(0.8, 0.8))
```

#### 联合密度图 {#sec-density-2d-joint}
Expand Down Expand Up @@ -463,8 +463,7 @@ ggplot(
scale_size(labels = label_number(scale_cut = cut_short_scale())) +
scale_color_viridis_c(option = "C") +
geom_text_repel(
aes(label = `地区`),
size = 3, max.overlaps = 50,
aes(label = `地区`), size = 3, max.overlaps = 50,
segment.colour = "gray", seed = 2022, show.legend = FALSE
) +
coord_cartesian(xlim = c(30, 60), ylim = c(0, 10.5), expand = FALSE) +
Expand Down Expand Up @@ -512,8 +511,7 @@ ggplot(df, aes(year, rank, color = country)) +
geom_text(data = df[df$year == max(df$year), ],
aes(x = year + .1, label = country), size = 5, hjust = 0) +
geom_bump(linewidth = 2, smooth = 8) +
scale_x_continuous(limits = c(2017.6, 2020.4),
breaks = seq(2018, 2020, 1)) +
scale_x_continuous(limits = c(2017.6, 2020.4), breaks = seq(2018, 2020, 1)) +
theme_minimal(base_size = 14) +
theme(legend.position = "none",
panel.grid.major = element_blank()) +
Expand Down
17 changes: 11 additions & 6 deletions visualization-basic.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp)) +
labels = label_dollar(), minor_breaks = mb, limits = c(100, 110000)
) +
theme_classic() +
theme(legend.position.inside = c(0.875, 0.3)) +
theme(legend.position = "inside", legend.position.inside = c(0.875, 0.3)) +
labs(x = "人均 GDP", y = "预期寿命", fill = "区域")
```

Expand Down Expand Up @@ -458,8 +458,13 @@ ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp)) +
scale_x_log10(
labels = label_dollar(), minor_breaks = mb, limits = c(100, 110000)
) +
theme_tufte(base_family = "Noto Serif CJK SC") +
theme(legend.position.inside = c(0.875, 0.3)) +
theme_tufte(base_family = "sans") +
theme(
legend.position = "inside",
legend.position.inside = c(0.875, 0.3),
legend.title = element_text(family = "Noto Sans CJK SC"),
legend.text = element_text(family = "Noto Sans CJK SC"),
axis.title = element_text(family = "Noto Sans CJK SC")) +
labs(x = "人均 GDP", y = "预期寿命", fill = "区域")
```

Expand Down Expand Up @@ -503,7 +508,7 @@ ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp)) +
) +
scale_size(range = c(2, 12)) +
theme_tufte(base_family = "Noto Serif CJK SC") +
theme(legend.position.inside = c(0.9, 0.3)) +
theme(legend.position = "inside", legend.position.inside = c(0.9, 0.3)) +
labs(x = "人均 GDP", y = "预期寿命", fill = "区域")
```

Expand Down Expand Up @@ -538,7 +543,7 @@ ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp)) +
) +
scale_size(range = c(2, 12)) +
theme_tufte(base_family = "Noto Serif CJK SC") +
theme(legend.position.inside = c(0.9, 0.3)) +
theme(legend.position = "inside", legend.position.inside = c(0.9, 0.3)) +
labs(x = "人均 GDP", y = "预期寿命", fill = "区域")
```

Expand Down Expand Up @@ -595,7 +600,7 @@ ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp)) +
scale_x_log10(labels = label_log(), limits = c(100, 110000)) +
facet_wrap(facets = ~income_level, ncol = 3) +
theme_classic() +
theme(legend.position.inside = c(0.9, 0.2)) +
theme(legend.position = "inside", legend.position.inside = c(0.9, 0.2)) +
labs(x = "人均 GDP", y = "预期寿命", fill = "区域")
```

Expand Down

0 comments on commit 36d7ba0

Please sign in to comment.