Anas Elomari
Anas Elomari

Reputation: 1

Multigroup line chart in R

I wish to do a simple line plot with R on a data.frame that looks something like this:

What I'm trying to do

There should be 4 lines, with each line representing an origin, and the x axis representing the years and the y the number of observations per that year, but I cannot seem to find a correct approach.

I have tried the following:

metadata %>%
+   filter(!is.na(origin)) %>%
+   ggplot(aes(year, colour = origin)) +
+   geom_point(size = 5, alpha = 0.3) +
+   geom_line(stat = "count", aes(group = 1)) +
+   theme_minimal()

Can someone help me figure out what I'm doing wrong?

I also tried doing a multiple line plot where the x is a numeric variable and the y is the count of the previous variable,

Upvotes: 0

Views: 49

Answers (1)

Johan Rosa
Johan Rosa

Reputation: 3152

I would just count year and origin before the plot.

library(ggplot2)
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

data <- data.frame(
  year = sample(2014:2024, 200, replace = TRUE),
  origin = sample(c("CWED", "LIMS", "CASD", "CWED"), 200, replace = TRUE)
)

head(data)
#>   year origin
#> 1 2022   CASD
#> 2 2021   CWED
#> 3 2023   CWED
#> 4 2022   CWED
#> 5 2015   CWED
#> 6 2020   CASD

data |>
  count(year, origin) |> 
  ggplot(aes(x = year, y = n, color = origin)) +
  geom_line() +
  theme_minimal()

Created on 2024-03-07 with reprex v2.0.2

Upvotes: 0

Related Questions