Line chart with multiple columns

Hello!

I am trying to plot a line chart, where each line would be a category that is in a different column (i.e. one line will be 'amnorte', another 'amlatina', and finally 'europa'). They should represent the count of occurrences in each year ('AN_BASE'), as values '1' would be counted, and '0' should not be counted.

Please find below a reprex:

new_df_regionalismo<-data.frame(
  stringsAsFactors = FALSE,
           AN_BASE = c(2003,2003,2003,2003,2004,
                       2004,2004,2004,2004,2004,2005,2005,2005,2005,2005),
           amnorte = c("0","0","0","0","1","0",
                       "0","0","0","0","1","0","0","0","0"),
          amlatina = c("0","1","0","1","0","1",
                       "0","0","1","0","0","0","0","0","0"),
            europa = c("0","1","0","0","0","0",
                       "0","0","0","0","0","0","0","1","0")
)

Thank you for your help

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)
library(stringr)
library(tidyr)

# original values of "0" and "1" converted to logical
d <- structure(list(AN_BASE = c(2003, 2003, 2003, 2003, 2004, 2004, 
                                 2004, 2004, 2004, 2004, 2005, 2005, 2005, 2005, 2005), 
                     amnorte = c(FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, 
                                 TRUE, FALSE, FALSE, FALSE, FALSE), 
                     amlatina = c(FALSE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, 
                                  FALSE, FALSE, FALSE), 
                     europa = c(FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, 
                                FALSE)), row.names = c(NA, -15L), class = "data.frame")

d_count <- d %>%
  pivot_longer(cols = -AN_BASE, names_to = "region", values_to = "count") %>%
  group_by(AN_BASE, region) %>%
  summarise(count = sum(count), .groups = "drop")

ggplot(d_count, aes(x = AN_BASE, y = count, fill = region)) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(x = "Year", y = "Count of TRUE values", fill = "Region") +
  theme_minimal()

Created on 2023-11-18 with reprex v2.0.2

1 Like

I appreciate your help! It is working well now

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.