简体   繁体   中英

How to include min and max value pf a particular variable in r ggplot

在此处输入图像描述

I have tried many things but was not able to include the min and maximum value of GDP in each countries line graph.

There already are maximum and minimum values of GDP in your plot. Or are you just trying to recreate this plot? If so, the following code gets you very close:

library(gapminder)
library(tidyverse)

gapminder %>%
  filter(country %in% c("Canada", "Japan", "Jordan", "United States")) %>%
  filter(year %in% c(1962, 1982, 2002)) %>%
  ggplot(aes(factor(year), gdpPercap, color = country, group = country)) +
  geom_line(size = 2, alpha = 0.8) +
  geom_point(size = 5, alpha = 0.8) +
  geom_text(data = . %>% filter(year == 2002), hjust = 0, nudge_x = 0.1,
            size = 5, key_glyph = draw_key_blank,
            aes(label = scales::dollar(round(gdpPercap)))) +
  geom_text(data = . %>% filter(year == 1962), hjust = 1, nudge_x = -0.1,
            size = 5, key_glyph = draw_key_blank,
            aes(label = scales::dollar(round(gdpPercap)))) +
  scale_color_brewer(palette = "Set2") +
  labs(title = "A comparison of GDP", 
       subtitle = "GDP per capita change 1962 - 2002",
       caption = "Source: Gapminder dataset") +
  theme_void(base_size = 16) +
  theme(panel.grid.major.y = element_line(size = 0.05),
        axis.text.x = element_text(),
        plot.margin = margin(20, 20, 20, 20),
        plot.title = element_text(face = 2),
        plot.subtitle =  element_text(face = 2, margin = margin(10, 0, 50, 0)),
        plot.caption =  element_text(face = 2, margin = margin(30, 30, 30, 30)))

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM