R中的时间序列连接散点图(如附图)

Time series connected scatter plot in R (as image attached)

我想绘制这些数据 as connected temporal scatter kind of plot in R. Any hint/code would be greatly helpful. I can do with single but not with time series one.

看看您是否可以将此应用于您的数据。

我假设出于图形表示的目的,将年份视为 'time' 数据并不重要。

library(tidyr)
library(dplyr)
library(stringr)
library(ggplot2)
library(ggrepel)

pop <- tibble(city = c("Delhi", "Madurai", "Cape Town", "Jo Burg"),
              `1990` = c(10, 12, 5, 6),
              `2000` = c(13, 17, 7, 8),
              `2015` = c(20, 24, 7.5, 9),
              km2_1990 = c(12, 15, 2, 4),
              km2_2000 = c(13, 14, 3, 4),
              km2_2015 = c(15, 16, 5, 6))


cont <- tibble(city = c("Delhi", "Madurai", "Cape Town", "Jo Burg"),
               cont = c("Asia", "Asia", "Africa", "Africa"))


tib <- 
  pop %>% 
  pivot_longer(cols = `1990`:km2_2015, names_to = "year", values_to = "val") %>% 
  mutate(type = case_when(str_detect(year, "km") ~ "area",
                          TRUE ~ "pop"),
         year = as.numeric(str_remove(year, "km2_"))) %>% 
  pivot_wider(names_from = type, values_from = val) %>% 
  left_join(cont) %>% 
  group_by(year, cont) %>% 
  summarise(area = sum(area),
            pop = sum(pop)) %>% 
  group_by(cont) %>% 
  mutate(lab_cont = case_when(pop == max(pop)~cont,
                              TRUE ~ ""))



ggplot(tib, aes(area, pop, label = lab_cont))+
  geom_line(aes(group = cont), colour = "red")+
  geom_point(fill = "red", colour = "red", shape = 22, size = 4)+
  geom_text_repel(aes(label = year))+
  geom_text_repel(nudge_x = 1, size = 5)

这为您提供了下图,您可以根据需要调整参数以美化外观以满足您的目的:

像这样转换您的数据

然后,使用 ggplot2 包绘图。

mydata %>%
  ggplot(aes(x=bsurf, y=pop, group=city, color=city, dlabel=year)) + geom_point(,size = 2)+
  geom_line(size=1)+ geom_text(label=mydata$year, vjust = 1.2, nudge_y = 0.5)+
  ggtitle("Dummy Title", subtitle = "Dummy") + xlab("Population") + ylab("Total Built-up Surface")