Вот возможное решение, в котором я использую второй вспомогательный data.frame вместе с geom_segment()
, чтобы нарисовать соединительную линию между двумя временными точками.Я также включил некоторые визуальные настройки, такие как цвета, тип точек, прозрачность и стрелки.Они могут быть удалены или изменены по мере необходимости.
# Auxiliary data.frame to draw segments.
adat = reshape2::dcast(data=Animals,
formula=Reason ~ Year,
value.var="Species")
adat
# Reason 2018 2019
# 1 Genuine 24 16
# 2 Misclassified 41 85
# 3 Taxonomic 2 7
# 4 Unclear 41 117
# Colors selected from
# https://yutannihilation.github.io/allYourFigureAreBelongToUs/ggthemes/economist_pal/
year_colors = c("2018"="#D7D29E", "2019"="#82C0E9")
p1 = ggplot() +
theme_economist() +
geom_point(data=Animals,
aes(y=Reason, x=Species, fill=Year),
size=6, shape=21, color="grey30") +
geom_segment(data=adat,
aes(y=Reason, yend=Reason, x=`2018`, xend=`2019`),
size=1.8, color="grey30",
lineend="butt", linejoin="mitre",
arrow=arrow(length = unit(0.02, "npc"), type="closed")) +
scale_fill_manual(values=year_colors)
p2 = ggplot() +
theme_economist() +
geom_segment(data=adat,
aes(y=Reason, yend=Reason, x=`2018`, xend=`2019`),
size=6, color="#82C0E9", alpha=0.6, lineend="butt") +
geom_point(data=Animals,
aes(y=Reason, x=Species, color=Year),
size=6) +
xlab("Species") +
scale_color_manual(values=c("2018"="#C10534", "2019"="#008BBC"))
ggsave("segment_plot_1.png", plot=p1, width=8, height=2.5, dpi=150)
ggsave("segment_plot_2.png", plot=p2, width=8, height=2.5, dpi=150)
# Basic version.
p3 = ggplot() +
geom_point(data=Animals, aes(y=Reason, x=Species, color=Year)) +
geom_segment(data=adat, aes(y=Reason, yend=Reason, x=`2018`, xend=`2019`))
ggsave("segment_plot_3.png", plot=p3, width=8, height=2.5, dpi=150)


