Daniel Lewis Jones
Daniel Lewis Jones

Reputation: 13

GGplot not producing linear y axis for line graph

I'm very new to R, so this may be a simple solve.

I am trying to create a line graph that plots the average global immunisation rate of a vaccine against the year. I have created my data set, shown below:

Year <- c("1991","1992","1993","1994","1995","1996", "1997","1998","1999","2000","2001","2002","2003","2004","2005","2006","2007",
          "2008","2009","2010","2011","2012","2013","2014","2015","2016","2017","2018")

Average_Global_Immunisation_Rate <- c("1.453125","2.234375","3.322917", "3.906250","4.703125","6.411458","10.598958","15.348958","20.776042",                                    "25.265625","29.932292","35.755208","39.250000","42.343750","45.343750","48.177083","52.567708","58.270833",                         "69.161458","75.156250","79.677083","81.911458","84.968750","86.072917","87.151042","87.677083","87.265625","87.281250")

#create new data set from averages and year values

df.AGIR <- data.frame(Year,Average_Global_Immunisation_Rate)
View(df.AGIR)

Whilst my data shows a steady increase as the years progress, when I use GGplot to create the graph, the y axis is not linear, as seen below:

enter image description here

Would anyone be able to help me make the y axis linear, or explain why it defaults to the one shown? Thanks.

Upvotes: 1

Views: 339

Answers (1)

Ian Campbell
Ian Campbell

Reputation: 24770

Your data as presented is currently stored as a character vector:

class(Average_Global_Immunisation_Rate)
[1] "character"
class(Year)
[1] "character"

You need your data to be stored as numeric vectors:

Year <- as.numeric(Year)
Average_Global_Immunisation_Rate <- as.numeric(Average_Global_Immunisation_Rate)
df.AGIR <- data.frame(Year,Average_Global_Immunisation_Rate)
ggplot(df.AGIR, aes(x = Year, y = Average_Global_Immunisation_Rate)) +
   geom_point()

enter image description here

Next time, when you are assigning your data, do not use the " character to surround it. That indicates to R to create a character vector.

Upvotes: 1

Related Questions