'How to apply slope plot R code to another data

I have dataframe which represents sales by model within 2 different years. 'change' column stands for absolute change by models from 2020 to 2021 while 'chng.percent' measures this change in percentages. However, I am struggling to apply the given Code of slope plot to my data.

df <- data.frame (model  = c("A", "A", "B","B"),
                  year = c(2020,2021,2020,2021),
                  sale =c(105,190,110,180),
                  chang = c(85,NA,70,NA),
                  chng.percent = c(80.9,NA, 63.6,NA))

Expected outcome (Like this) enter image description here



Solution 1:[1]

you can try something like this :

df <- data.frame(model  = c("A", "B"),
                 sale_2020 =c(105,110),
                 sale_2021 =c(190,180),
                  chang = c(85,70),
                  chng.percent = c(80.9, 63.6))

df %>%
  ggplot() +
  geom_segment(aes(x = 1, xend = 2, 
                   y = sale_2020, 
                   yend = sale_2021,
                   group = model,
                   col = model), 
               size = 1.2) +
  # set the colors
  scale_color_manual(values = c("#468189", "#9DBEBB"), guide = "none")  +
  # remove all axis stuff
  theme_classic() + 
  theme(axis.line = element_blank(),
        axis.text = element_blank(),
        axis.title = element_blank(),
        axis.ticks = element_blank()) +
  geom_text(aes(x = x, y = y, label = label),
            data = data.frame(x = 1:2, 
                              y = 10 + max(df$sale_2021),
                              label = c("2020", "2021")),
            col = "grey30",
            size = 6) +
  # add vertical lines that act as axis for 2020
  geom_segment(x = 1, xend = 1, 
               y = min(df$sale_2020) -10,
               yend = max(df$sale_2020) + 81,
               col = "grey70", size = 1.5) +
  # add vertical lines that act as axis for 2021
  geom_segment(x = 2, xend = 2, 
               y = min(df$sale_2021) - 80,
               yend = max(df$sale_2021) + 1,
               col = "grey70", size = 1.5) +
  # add the success rate next to each point on 2021 axis
geom_text(aes(x = 2 + 0.08, 
              y = sale_2021, 
              label = paste0(round(sale_2021, 1))),
          col = "grey30") +
  # add the success rate next to each point on 2021 axis
  geom_text(aes(x = 1 - 0.08, 
                y = sale_2020, 
                label = paste0(round(sale_2020, 1))),
            col = "grey30") +
  # add the success rate next to each point on 2020 axis
  geom_text(aes(x = 2 - 0.5, 
                y = c(156, 135), 
                label = paste0(round(chng.percent, 1), "%")),
            col = "grey30") 

Solution 2:[2]

Here's a way to do it all within ggplot using your existing data:

ggplot(df, aes(year, sale, color = model)) +
  geom_line(arrow = arrow(type = "closed", angle = 20),
            key_glyph = draw_key_point) +
  geom_vline(aes(xintercept = year)) +
  geom_text(aes(label = sale, hjust = ifelse(year == 2020, 1.3, -0.3)), 
            color = "black", 
            size = 6) +
  geom_text(aes(x = min(df$year) + 0.25, y = 105,
           label = paste0("+", chang[1], "; ", chng.percent[1], "%"),
           color = "A"), size = 5) +
  geom_text(aes(x = max(df$year) - 0.25, y = 150,
           label = paste0("+", chang[3], "; ", chng.percent[3], "%"),
           color = "B"), size = 5) +
  theme_void(base_size = 16) +
  coord_cartesian(clip = "off") +
  scale_x_continuous(breaks = c(2020, 2021)) +
  guides(color = guide_legend(override.aes = list(size = 5))) +
  scale_color_brewer(palette = "Set1") +
  theme(plot.margin = margin(30, 30, 30, 30),
        aspect.ratio = 1.5,
        axis.text.x = element_text(size = 20))

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Claire Kermorvant
Solution 2