0
#create the data 
library(ggplot2)
data <- data.frame(
  Date = c("2000", "2002", "2003", "2006",
       "2007", "2008", "2000", "2002",
       "2003", "2011", "2012", "2014"),
  EUR = c(1.0155, 1.0309, 1.0335, 1.0324, 1.0294, 1.0252, 1.0322,
      1.0281, 1.027, 1.0128, 1.0121, 1.0115),
  Company=c(rep(c(1),6),rep(c(2),6))
)

#check the data
str(data)

# convert date column to Date as it is currently a **factor**
data$Date <- as.Date(as.character(data$Date))

#basic plot
ggplot(data,aes(Date, EUR, colour=Company, group=Company)) + geom_line()+ geom_point()

As you see in the graph, the space between 2003 and 2006 is the same as between 2011 and 2012. Is there a way to display the years that are scaled correctly/ aka proportionately!?

As you see in the graph, the space between 2003 and 2006 is the same as between 2011 and 2012. Is there a way to display the years that are scaled correctly/ aka proportionately!?

JustLearning
  • 180
  • 2
  • 12
  • 6
    The problem is that your `Date`column is not an object of class `Date`. You must do something like `data$Date <- as.Date(paste(as.character(data$Date), "1", "1", sep = "-"))`. – Rui Barradas Jan 27 '18 at 17:34

1 Answers1

2

Like Rui pointed out data$Date isn't actually a Date object. Using the function as.POSIXct() will properly convert your initial character values into POSIX times. Then scale_x_datetime() can help format your ggplot axis.

# use POSIXct() and specify format; see ?strptime() for help on formats
data$Date <- as.POSIXct(data$Date, format = "%Y")

# for plotting porpoises
data$Company <- as.factor(data$Company)

# now with scale_x_datetime to give sensical breaks and labels
ggplot(data,aes(Date, EUR, colour = Company)) +
    geom_line() + geom_point() +
    scale_x_datetime(date_breaks = "2 years", date_labels = "%Y")

If you want to have all of the point line up with the year ticks, then you need to specify your date at January 1st of yearX. You can use this code instead of the original as.POSIXct() line:

data$Date <- as.POSIXct(paste0("01-01-", data$Date), format = "%m-%d-%Y")

Careful not to run the as.POSIX() conversion twice (pick one) or you won't like what you get.

enter image description here

Nate
  • 10,361
  • 3
  • 33
  • 40
  • Thank you @Nate! This works great! However, when i place this plot inside Shiny app, all of a sudden i get an error " 'origin' must be supplied". As I understand, it refers to "origin" parameter of as.POSIXct. But when i added the parameter origin="1960-10-01" (alternatively I also tried origin="1970-01-01") , my years get converted into completely different numbers. (like 33-37). Any tips on this? I am confused why as.POSIXct doesn't request origin parameter when called from stand-alone r script, but works differntly inside of Shiny app. Thanks! :) – JustLearning Jan 29 '18 at 19:40
  • 1
    You're most welcome. It's very hard to say what is causing that error message without your full code. [Here](https://nathanday.shinyapps.io/test2/) is working example of the same code/data running in a Shiny app. If that doesn't help, make a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and simply ask about it is a new question. #SOhappytohelp – Nate Jan 30 '18 at 02:32
  • Thank you @Nat! That did help. I was able to get the same graph as you. However , i noticed that on your graph (and on mine, too) - the data points are shifted to the right , which doesn't look good. (to clarify, the data points corresponding to years are not next to the year tick, but is to the right.) Do you happen to know why? Thank you!!! – JustLearning Feb 01 '18 at 15:52
  • 1
    Yea the issue is that `as.POSIXct()` is use the system time to fill in the month and day so the offset you are seeing if probably becasue really its January 30,2018 in your date column now, you can check yourself via `data$Date`. I'll make an edit and show you how to fix this. – Nate Feb 01 '18 at 16:47