LazyBrain
LazyBrain

Reputation: 104

ggplot2 not displaying values of the Y axis

I try to get the values showing of the Y axis but it seems like I can't figure out the issue, here's my code and the results of the plot besides the data:

library(ggplot2)

traffic.data <- structure(list(years = 2008:2017, units = structure(c(1L, 2L, 4L, 5L, 3L, 6L, 7L, 8L, 9L, 10L), .Label = c("12,866,461", "13,350,011", "15,106,666", "15,361,841", "15,669,918", "16,498,204", "17,296,885", "17,611,762", "18,239,288", "20,359,883"), class = "factor")), .Names = c("years", "units"), class = "data.frame", row.names = c(NA, -10L))

traffic.data
       years     units
#  1   2008 12,866,461
#  2   2009 13,350,011
#  3   2010 15,361,841
#  4   2011 15,669,918
#  5   2012 15,106,666
#  6   2013 16,498,204
#  7   2014 17,296,885
#  8   2015 17,611,762
#  9   2016 18,239,288
# 10  2017  20,359,883

ggplot(traffic.data, aes(x = years, y = as.numeric(units))) + 
   geom_point() +
   geom_line() +
   scale_x_continuous(breaks = seq(2008, 2017, 1)) + 
   scale_y_continuous(breaks = seq(10000000,30000000,1)) +
   labs(x = "years", y = "total traffic of passengers", 
        title = "evolution of traffic during the past 10 years")

When I ran this code I get the following line chart missing the values of Y axis : the R plot results

Upvotes: 0

Views: 9926

Answers (2)

Sal-laS
Sal-laS

Reputation: 11649

Problem 1:

scale_y_continuous(breaks = seq(10000000,30000000,1))

So, you are trying to increase from 10M to 30M by 1.

Problem 2:

  +   labs(x = "years", y = "total traffic of passengers", 
    +        title = "evolution of traffic during the past 10 years")

in this line your second + is not needed, and it makes problem.


it works well for me by this data

dt<-data.frame(years=c(2008:2018),
               units=sample(c(15000000:20000000),11,replace = T))
dt

and this code:

       ggplot(dt, aes(x=years, y=units))+
   geom_point(data=NULL)+
   geom_line(data=NULL) + 
   scale_y_continuous(breaks = seq(10000000,30000000,1000000)) + 
   labs(x = "years", y = "total traffic of passengers",
        title = "evolution of traffic during the past 10 years")

Here is the result:

enter image description here

Upvotes: 2

AntoniosK
AntoniosK

Reputation: 16121

Dataset

# example dataset
df = structure(list(years = 2008:2017, 
                    units = structure(c(1L, 2L, 4L, 5L, 3L, 6L, 7L, 8L, 9L, 10L), .Label = c("12,866,461", "13,350,011", "15,106,666", "15,361,841", "15,669,918", "16,498,204", "17,296,885", "17,611,762", "18,239,288", "20,359,883"), class = "factor")), 
              .Names = c("years", "units"), class = "data.frame", row.names = c(NA, -10L))

The problem

Your units column is factor and updating it into a number is tricky. Check this:

as.numeric(df$units)
# [1]  1  2  4  5  3  6  7  8  9 10

Those are the values you plot in y axis. This is why the distance between your points seems to be the same no matter what the actual numbers are and this is why those values are nor shown in y axis, because you specify scale_y_continuous(breaks = seq(10000000,30000000,1)) (i.e. to start from 3mil.).

Solution

# update column
df$units = as.numeric(gsub(",", "", as.character(df$units), fixed = TRUE))

library(ggplot2)

ggplot(df, aes(x=years, y=units)) +
  geom_point() +
  geom_line() + 
  scale_x_continuous(breaks = seq(2008,2017,1))+ 
  scale_y_continuous(breaks = seq(10000000,30000000,1000000)) +
  labs(x = "years", y = "total traffic of passengers", title = "evolution of traffic during the past 10 years")

enter image description here

Note that I've changed the step/distance between your y axis values to 1mil. Having a step = 1 will make the process slow and the plot unreadable.

You can use options(scipen = 999) before plotting, in order to avoid scientific notation of numbers.

Upvotes: 1

Related Questions