0

Weird issue: I am plotting some data, but the graph is not showing the lines.

Here is the code:

plot1 <- dat1 %>% 
  rename(country1 = "Region, subregion, country or area *", `1955` = "1950-1955", `1960` = "1955-1960", `1965` = "1960-1965", `1970` = "1965-1970", `1975` = "1970-1975", `1980` = "1975-1980", `1985` = "1980-1985", `1990` = "1985-1990", `1995` = "1990-1995", `2000` = "1995-2000", `2005` = "2000-2005", `2010` = "2005-2010", `2015` = "2010-2015", `2020` = "2015-2020") %>% 
  mutate(difference = `2020` - `1955`) %>% 
  mutate(hightfr = if_else(`2020` > 5, "high fertility", "fast decline")) %>% 
  filter(`Parent code` %in% c(911, 913)) %>% 
  filter(`2020` > 5 | `difference` < -3) %>% 
  pivot_longer(8:21, names_to = "year_range", values_to = "tfr") %>% 
  ggplot() +
  geom_line(mapping = aes(year_range, tfr, color = `country1`)) + 
  geom_point(mapping = aes(year_range, tfr, color = `country1`, shape = `hightfr`))
plot1

As you can see, the years are in ranges, which someone told me may be the source of the issue.

The issue is that the points show up, but not the lines. However, the lines do show up on the key.

Here is the plot.

Does anyone know of a way to get the lines to show up on the plot?

Thank you!

stefan
  • 90,330
  • 6
  • 25
  • 51
Scott Hebert
  • 133
  • 9
  • 1
    It would be easier to help you if you provide a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data. From your code I could only guess that `year_range` is a character in which case you could try with adding the group aesthetic, i.e. `geom_line(aes(..., group=country1))` . – stefan Sep 30 '21 at 16:05
  • @stefan Thank you. I believe the issue is actually an issue regarding the year range needing to be a single numeric value. I'm trying to figure this out now. – Scott Hebert Sep 30 '21 at 16:11

1 Answers1

0

I had to get the midpoint of the year ranges by using this code:

  mutate(year = as.numeric(str_sub(year_range, end = 4))) %>% 
  mutate(year2 = year + 2.5) %>% 

Thank you!

Scott Hebert
  • 133
  • 9