我有以下数据集:
tax <- tribble(
~ Country, ~ `1970`, ~ `1979`,
"Sweden", 46.9, 57.4,
"Netherlands", 44.0, 55.8,
"Norway", 43.5, 52.2,
"Britain", 40.7, 39.0,
"France", 39.0, 43.4,
"Germany", 37.5, 42.9,
"Belgium", 35.2, 43.2,
"Canada", 34.9, 35.8,
"Finland", 34.9, 38.2,
"Italy", 30.4, 35.7,
"United States", 30.3, 32.5,
"Greece", 26.8, 30.6,
"Switzerland", 26.5, 33.2,
"Spain", 22.5, 27.1,
"Japan", 20.7, 26.6
)
我正在尝试创建一个斜率图(x 轴为 1970 年和 1979 年),y 轴为年份值,并为国家/地区创建标签。然而,我遇到了一个问题,即某些值不是唯一的,因此它们相互重叠。这不是我想要的行为,我希望每个点在轴上都有唯一的位置。我正在尝试重新创建一个情节作为练习。到目前为止,我拥有的代码:(没有更深入地完善绘图或添加标签,因为我似乎无法弄清楚如何使点不重叠。)我尝试过抖动它们,但它没有没有按预期工作。
tax |>
pivot_longer(
cols=c("1970", "1979"),
names_to="Year",
values_to="Tax"
) |>
ggplot() +
geom_line(
aes(x=Year, y=Tax, group=Country)
) +
geom_point(
aes(x=Year, y=Tax, group=Country)
)
我可能会忽略其他选项,但我认为你最好的选择是使用排斥或改变尺度 - 使用两者可能效果最好。
我认为没有一个完美解决你的问题,但是很多出版物(纽约时报等)都会在 Photoshop 或其他工具中修改可视化效果,以获得你共享的图像类型...
您可以创建一个标签并使用 geom_text_repel 参数让文本按照您喜欢的方式布局。绘图输出的大小很重要,因此您需要使用绘图面板中的 export 按钮来设置绘图的大小并继续重新运行,直到将其格式化为您喜欢的格式。例如,下面的第一张图片来自我的 reprex,但第二张图片是我导出的宽度为 800、高度为 1200 的图。
library(tidyverse)
library(glue)
library(ggrepel)
tax <- tribble(
~ Country, ~ `1970`, ~ `1979`,
"Sweden", 46.9, 57.4,
"Netherlands", 44.0, 55.8,
"Norway", 43.5, 52.2,
"Britain", 40.7, 39.0,
"France", 39.0, 43.4,
"Germany", 37.5, 42.9,
"Belgium", 35.2, 43.2,
"Canada", 34.9, 35.8,
"Finland", 34.9, 38.2,
"Italy", 30.4, 35.7,
"United States", 30.3, 32.5,
"Greece", 26.8, 30.6,
"Switzerland", 26.5, 33.2,
"Spain", 22.5, 27.1,
"Japan", 20.7, 26.6
) %>%
pivot_longer(
cols=c("1970", "1979"),
names_to="Year",
values_to="Tax"
) |>
mutate(text = if_else(Year == "1970", glue("{Country} {Tax}"), glue("{Tax} {Country}")))
tax |> ggplot(aes(x=Year, y=Tax, group=Country)) +
geom_line() +
ggrepel::geom_text_repel(
data = tax |> filter(Year == "1970"),
aes(label = text),
nudge_x = -.2,
force = 2
) +
ggrepel::geom_text_repel(
data = tax |> filter(Year == "1979"),
aes(label = text),
nudge_x = .2,
force = 2
) +
theme_minimal() +
theme(panel.grid = element_blank())
创建于 2024-05-01,使用 reprex v2.0.2
您可以尝试使用变换来缩放 y 轴,使其足以分离。这通常适用于非常接近的值,但我认为在相同值的情况下,它不会解决您的问题。也就是说,这对于某些点接近但不完全相同的情况可能会有所帮助。
library(tidyverse)
library(glue)
library(ggrepel)
tax <- tribble(
~ Country, ~ `1970`, ~ `1979`,
"Sweden", 46.9, 57.4,
"Netherlands", 44.0, 55.8,
"Norway", 43.5, 52.2,
"Britain", 40.7, 39.0,
"France", 39.0, 43.4,
"Germany", 37.5, 42.9,
"Belgium", 35.2, 43.2,
"Canada", 34.9, 35.8,
"Finland", 34.9, 38.2,
"Italy", 30.4, 35.7,
"United States", 30.3, 32.5,
"Greece", 26.8, 30.6,
"Switzerland", 26.5, 33.2,
"Spain", 22.5, 27.1,
"Japan", 20.7, 26.6
) %>%
pivot_longer(
cols=c("1970", "1979"),
names_to="Year",
values_to="Tax"
) |>
mutate(text = if_else(Year == "1970", glue("{Country} {Tax}"), glue("{Tax} {Country}")))
tax |> ggplot(aes(x=Year, y=Tax, group=Country)) +
geom_line() +
geom_text_repel(
data = tax |> filter(Year == "1970"),
aes(label = text),
nudge_x = -.2,
force = 2
) +
geom_text_repel(
data = tax |> filter(Year == "1979"),
aes(label = text),
nudge_x = .2,
force = 2
) +
theme_minimal() +
theme(panel.grid = element_blank())
tax |> ggplot(aes(x=Year, y=Tax, group=Country)) +
geom_line() +
geom_text(
data = tax |> filter(Year == "1970"),
aes(label = text),
nudge_x = -.2
) +
geom_text(
data = tax |> filter(Year == "1979"),
aes(label = text),
nudge_x = .2
) +
scale_y_continuous(trans = scales::log2_trans()) +
theme_minimal() +
theme(panel.grid = element_blank())
创建于 2024-05-01,使用 reprex v2.0.2