0

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?

Safina A.R
  • 19
  • 8
  • Can you make your post [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by providing your data using `dput()`? – jrcalabrese Dec 19 '22 at 18:51

1 Answers1

0

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

jrcalabrese
  • 2,184
  • 3
  • 10
  • 30
  • Thank you. But this is becoming to messy I would like to add a decade info like plots based in 10 years instead yearly . I tried to give breaks of 10 years but that not adding up values for 1 decade – Safina A.R Jan 23 '23 at 09:49
  • If you have an additional question about formatting of the x-axis, you can post a separate question on StackOverflow (with a reproducible example). – jrcalabrese Jan 23 '23 at 16:50