Safina A.R
Safina A.R

Reputation: 19

Plotting multiple variable trends over years for 3 continents (trend time series plot in ggplot)

amr <- read.csv("amr_paraB_trends.csv")

so this table consists of 16 columns:

  1. Continents (America, Asia, Europe)

  2. Years (1965 1976 1981 1988 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021)

  3. Streptomycin

  4. Spectinomycin

  5. Kanamycin

  6. Ampicillin

  7. Trimethoprim

  8. Chloramphenicol

  9. Ciprofloxacin_IR

  10. Sulfisoxazole

  11. Tetracycline

  12. Nalidixic_acid

  13. Colistin

  14. Cefoxitin

  15. Ceftriaxone

  16. Lincomycin

    than i converted this table to long df:

> amr$Years <- format(amr$Years, format = "%Y")
>     library(ggplot2)
>     library(tidyr)
>     df_long <- pivot_longer(amr, 
>                             cols = c(Streptomycin,  Spectinomycin,  Kanamycin,  Ampicillin, Trimethoprim,   Chloramphenicol,    Ciprofloxacin_IR,   Sulfisoxazole,  Tetracycline,   Nalidixic_acid, Colistin,   Cefoxitin,  Ceftriaxone,    Lincomycin),
> 
>                             values_to = "Isolates",
>                             names_to = "Phenotypes")
>     

than I want to plot the trends of each phenotypes (Streptomycin, kanamycin and etc) over years for each continent.

>     ##plot
>     ggplot(df_long, aes(x = Years, y = Isolates, color = Phenotypes, linetype = Continents)) +
>       geom_line(size = 0.7) +
>       ggtitle("Trend of amr over years") + 
>       xlab("Year") +
>       ylab("No of isolates) +
>       #x_continuous(breaks = seq(1975, 2022, by = 1)) +
>       theme_minimal()

but this is not producing trend lines the graph is empty.

can anyone helps?

Upvotes: 0

Views: 44

Answers (1)

jrcalabrese
jrcalabrese

Reputation: 2321

From the code you have provided so far, the two main issues are that your ylab axis title is lacking an apostrophe (leading to an Error: unexpected symbol message) and that your x_continuous argument should be scale_x_continuous; you can specify limits, breaks, and labels to tailor the x-axis for what you're trying to make. I also used the lubridate package to transform Years.

library(tidyverse)
library(lubridate)
set.seed(123)

Continents <- sample(c("Asia", "America", "Europe"), 100, replace = T)
Years <- sample(c(1965, 1976, 1981, 1988, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021), 100, replace = T)
Streptomycin <- sample(1:100, 100, replace = T); Spectinomycin <- sample(1:100, 100, replace = T); Kanamycin <- sample(1:100, 100, replace = T)
Ampicillin <- sample(1:100, 100, replace = T); Trimethoprim <- sample(1:100, 100, replace = T); Chloramphenicol <- sample(1:100, 100, replace = T)
Ciprofloxacin_IR <- sample(1:100, 100, replace = T); Sulfisoxazole <- sample(1:100, 100, replace = T); Tetracycline <- sample(1:100, 100, replace = T)
Nalidixic_acid <- sample(1:100, 100, replace = T); Colistin <- sample(1:100, 100, replace = T); Cefoxitin <- sample(1:100, 100, replace = T)
Ceftriaxone <- sample(1:100, 100, replace = T); Lincomycin <- sample(1:100, 100, replace = T)

df <- data.frame(Continents, Years, Streptomycin, 
         Spectinomycin, Kanamycin, Ampicillin, 
         Trimethoprim, Chloramphenicol, Ciprofloxacin_IR, 
         Sulfisoxazole, Tetracycline, Nalidixic_acid, Colistin, 
         Cefoxitin, Ceftriaxone, Lincomycin)

df_long <- pivot_longer(df, cols = c(Streptomycin, Spectinomycin, 
                                     Kanamycin, Ampicillin, Trimethoprim, 
                                     Chloramphenicol,Ciprofloxacin_IR, Sulfisoxazole, 
                                     Tetracycline, Nalidixic_acid, Colistin, 
                                     Cefoxitin, Ceftriaxone, Lincomycin),
                        values_to = "Isolates",
                        names_to = "Phenotypes") %>% 
  mutate(Years = year(as.Date(as.character(Years), format = "%Y")))

ggplot(df_long, aes(x = Years, y = Isolates, color = Phenotypes, linetype = Continents)) +
  geom_line(size = 0.7) +
  ggtitle("Trend of amr over years") + 
  xlab("Year") +
  ylab("No of isolates") +
  scale_x_continuous("Years", 
                     labels = as.character(Years), 
                     limits = c(1975, 2022),
                     breaks = Years,
                     guide = guide_axis(check.overlap = T)) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 30))

enter image description here

Upvotes: 0

Related Questions