Rizzle
Rizzle

Reputation: 117

How to display the counts (with $) at both ends of each line?

I want add the counts at the end of each line with a $. I did the plot in ggplot.

ggplot(aes(x = year, y = gdpPercap, color = country)) + 
  geom_line(size = 2) + geom_point(size = 4, alpha = 0.7) + theme_bw() + 
  
  
  labs(title = "A Comparrasson of GDP", 
 subtitle = "GDP per capita change, 1962 - 2022", 
 caption = "Source: Gapminder Data Set") + 
 theme(axis.ticks = element_blank(), axis.text.y = element_blank(),
    panel.grid.minor = element_blank(), panel.border = element_blank(), panel.grid.major.x = element_blank(), axis.title = element_blank()) +
  scale_colour_discrete(name = "Canada", type=c("light green", "orange", "sky blue", "pink")) + xlim(1960, 2003) 

enter image description here

Upvotes: 1

Views: 92

Answers (1)

jared_mamrot
jared_mamrot

Reputation: 26695

There are lots of potential solutions to this problem; here is one option:

library(tidyverse)
library(gapminder)

gapminder %>%
  filter(country %in% c("Canada", "United States",
                        "Australia", "New Zealand")) %>%
  ggplot(aes(x = year, y = gdpPercap, color = country)) + 
  geom_line(size = 2) + 
  geom_point(size = 4, alpha = 0.7) + 
  theme_bw() +
  labs(title = "Comparing GDP",
       subtitle = "GDP per capita change, 1962 - 2022", 
       caption = "Source: Gapminder Data Set (https://www.gapminder.org/data/) CC-BY") + 
  theme(axis.ticks = element_blank(), 
        axis.text.y = element_blank(), 
        panel.grid.minor = element_blank(), 
        panel.border = element_blank(),
        panel.grid.major.x = element_blank(), 
        axis.title = element_blank(),
        legend.position = c(0.1, 0.75)) + 
  scale_colour_discrete(name = "Country", 
                        type=c("light green", "orange", 
                               "sky blue", "pink")) + 
  xlim(1960, 2016) +
  geom_text(data = . %>% filter(year == max(year)),
            aes(x = 2009,
                y = gdpPercap,
                label = scales::dollar(gdpPercap)),
          inherit.aes = FALSE,
          hjust = 0, size = 4, vjust = 0.3) +
  coord_cartesian(clip = "off")
#> Warning: Removed 8 row(s) containing missing values (geom_path).
#> Warning: Removed 8 rows containing missing values (geom_point).

Created on 2022-09-30 by the reprex package (v2.0.1)


Colouring the points is straightforward (add color = country to aes()) however if you label the points at 1962 using the same approach you get overlapping labels (the points are too close together) and they are unreadable. A great solution for this problem is to use the ggrepel package, e.g.

library(tidyverse)
library(gapminder)
library(scales)
library(ggrepel)

gapminder %>%
  filter(country %in% c("Canada", "United States",
                        "Australia", "New Zealand")) %>%
  filter(year %in% 1962:2022) %>%
  ggplot(aes(x = year, y = gdpPercap, color = country)) + 
  geom_line(size = 2) + 
  geom_point(size = 4, alpha = 0.7) + 
  theme_bw() +
  labs(title = "Comparing GDP",
       subtitle = "GDP per capita change, 1962 - 2022", 
       caption = "Source: Gapminder Data Set (https://www.gapminder.org/data/) CC-BY") + 
  theme(axis.ticks = element_blank(), 
        axis.text.y = element_blank(), 
        panel.grid.minor = element_blank(), 
        panel.border = element_blank(),
        panel.grid.major.x = element_blank(), 
        axis.title = element_blank(),
        legend.position = c(0.1, 0.75)) + 
  scale_colour_discrete(name = "Country", 
                        type=c("light green", "orange", 
                               "sky blue", "pink")) + 
  xlim(1950, 2016) +
  geom_text(data = . %>% filter(year == max(year)),
            aes(x = 2009,
                y = gdpPercap,
                label = dollar(gdpPercap),
                color = country),
            inherit.aes = FALSE,
            hjust = 0, size = 4, vjust = 0.3) +
  coord_cartesian(clip = "off") +
  geom_text_repel(data = . %>% filter(year == min(year)),
            aes(x = 1962,
                y = gdpPercap,
                label = dollar(gdpPercap),
                color = country),
            inherit.aes = FALSE,
            hjust = 1, size = 4,
            min.segment.length = 0, 
            force = 10, nudge_x = -20)

Created on 2022-09-30 by the reprex package (v2.0.1)


Final update: you can round labels to 0 decimal places using round() and change the axis breaks using scale_x_continuous():

library(tidyverse)
library(gapminder)
library(scales)
#> 
#> Attaching package: 'scales'
#> The following object is masked from 'package:purrr':
#> 
#>     discard
#> The following object is masked from 'package:readr':
#> 
#>     col_factor
library(ggrepel)
gapminder %>%
  filter(country %in% c("Canada", "United States",
                        "Australia", "New Zealand")) %>%
  filter(year %in% 1962:2022) %>%
  ggplot(aes(x = year, y = gdpPercap, color = country)) + 
  geom_line(size = 2) + 
  geom_point(size = 4, alpha = 0.7) + 
  theme_bw() +
  labs(title = "Comparing GDP",
       subtitle = "GDP per capita change, 1962 - 2022", 
       caption = "Source: Gapminder Data Set (https://www.gapminder.org/data/) CC-BY") + 
  theme(axis.ticks = element_blank(), 
        axis.text.y = element_blank(), 
        panel.grid.minor = element_blank(), 
        panel.border = element_blank(),
        panel.grid.major.x = element_blank(), 
        axis.title = element_blank(),
        legend.position = c(0.1, 0.75)) + 
  scale_colour_discrete(name = "Country", 
                        type=c("light green", "orange", 
                               "sky blue", "pink")) + 
  geom_text(data = . %>% filter(year == max(year)),
            aes(x = 2009,
                y = gdpPercap,
                label = dollar(round(gdpPercap, 0)),
                color = country),
            inherit.aes = FALSE,
            hjust = 0, size = 4, vjust = 0.3) +
  coord_cartesian(clip = "off") +
  geom_text_repel(data = . %>% filter(year == min(year)),
            aes(x = 1962,
                y = gdpPercap,
                label = dollar(round(gdpPercap, 0)),
                color = country),
            inherit.aes = FALSE,
            hjust = 1, size = 4,
            min.segment.length = 0, 
            force = 10, nudge_x = -20) +
  scale_x_continuous(breaks = seq(from = 1962,
                                  to = 2022,
                                  by = 20),
                     limits = c(1950, 2016))

Created on 2022-09-30 by the reprex package (v2.0.1)

Upvotes: 3

Related Questions