1

i am currently working with a dataset which includes the value "MarketStart" for every observation. The values of this parameter reach from -24 (corresponds to january 2012) to 47 (corresponds to october 2017). The data collection was started in january 2014 which therefore corresponds to 0.

I am using ggplot2 to display the results but i am unable to label the x-axis in an acceptable manner, this is the code i am using right now:

df <- data.frame(Name,Price,ObsMonth,Producer)
plot.all <- 
  ggplot(data=df, aes(ObsMonth, Price, group = Name)) +
  geom_point(alpha=0.05) + 
  geom_line() +
  scale_x_continuous(expand = c(0, 0), limits = c(1, 36)) +
  scale_y_continuous(expand = c(0, 0), limits = c(0, 1200)) +             
  labs(x="ObsMonth", y="Price in Euro", title="title")

I limited the x-axis for now, but i'll need the numbers to be displayed as dates "Jan 2012, Feb 2012,..., Dez 2017" in the future corresponding to the numeric values explained above.

edit1: as requested here is part of the output provided by dput(df):

42L, 44L, 9L, 12L, 35L, 21L, 11L, 21L, 25L, 24L, 34L, 32L, 29L, 
41L, 36L, 33L, 30L, 43L, 44L, 31L, 39L, 35L, 27L, 42L, 23L, 28L, 
26L, 38L, 22L, 33L, 30L, 39L, 25L, 32L, 28L, 36L, 23L, 27L, 24L, 
34L, 29L, 26L, 22L, 21L, 45L, 35L, 37L, 31L, 43L, 44L, 38L, 39L, 
28L, 29L, 25L, 22L, 21L, 34L, 23L, 36L, 35L, 31L, 33L, 37L, 44L, 
26L, 30L, 27L, 24L, 27L, 25L, 29L, 28L, 26L, 30L, 31L, 31L, 30L, 
18L, 15L, 23L, 24L, 20L, 19L, 16L, 22L, 21L, 25L, 14L, 6L, 3L, 
4L, 7L, 9L, 11L, 12L, 17L, 16L, 14L, 5L, 10L, 2L, 8L, 1L, 15L, 
13L, 13L, 10L, 16L, 12L, 15L, 14L, 24L, 26L, 11L, 8L, 16L, 3L, 
6L, 13L, 12L, 29L, 19L, 4L, 1L, 9L, 17L, 25L, 28L, 7L, 18L, 10L, 
15L, 27L, 2L, 5L, 14L, 20L, 22L, 21L, 23L, 19L, 18L, 16L, 6L, 
3L, 2L, 1L, 8L, 5L, 4L, 11L, 7L, 14L, 15L, 9L, 13L, 12L, 10L, 
..... and the end part.....
, .Names = c("Name", 
"Preis", "Erhebungsmonat", "Hersteller"), row.names = c(NA, -6732L
), class = "data.frame")
lilputenga
  • 11
  • 3
  • The `lubridate` package would be helpful here. Could you update your question with the output of `dput(df)`? See [here](http://lubridate.tidyverse.org) for more on the package. – tyluRp Oct 29 '17 at 00:24
  • Thanks for your reply tyluRp. the output you requested is really long, i will edit it to the question and check out the package you suggested! – lilputenga Oct 29 '17 at 19:49
  • Try a smaller sample; `dput(head(df))` – tyluRp Oct 29 '17 at 21:08

1 Answers1

1

You could add a date column to your data set.

start <- as.Date("2012-01-01")
df$date <- seq.Date(from = start, length.out = dim(df)[1], by = "month")

And then change your plot accordingly

plot.all <-
ggplot(data=df, aes(date, Price, group = Name)) +
geom_point(alpha=0.05) +
geom_line() +
scale_x_continuous(expand = c(0, 0), limits = c(1, 36)) +
scale_y_continuous(expand = c(0, 0), limits = c(0, 1200)) +
labs(x="date", y="Price in Euro", title="title")
markus
  • 25,843
  • 5
  • 39
  • 58
  • Thank you for your quick reply. I tried working around with the method you suggested but i didn't get any useful results. I'm working with a dataset with over 7000 observations and i basically need to "replace" the mentioned numeric values just for displaying purposes in the plot. I could only get your method to work if my dataset included only 1 observation per month which is not the case. – lilputenga Oct 28 '17 at 20:38
  • You could use `replace`, see https://stackoverflow.com/questions/11811027/replace-function-examples#11811147 If your data is in a 'long' format where the entries of your `ObsMonth` column repeat `n` times you could use `rep(date, times = n)`. Can you provide some data (structure)? – markus Oct 28 '17 at 20:49
  • Thank you for another quick reply markus. I checked the link you provided and i think i could do a workaround with that method even tho i would have prefered to do it otherwise, i think this would still work. As for your request for data, i made 2 screenshots: Number of observations per month: https://prnt.sc/h39r8b Part of the structure: https://prnt.sc/h39rh2 "Erhebungsmonat corresponds to the "MarketStart" mentioned in the question. – lilputenga Oct 28 '17 at 21:20