1

I have a created heatmap where I am trying to format the x axis labels as %d/%m (dd/mm). I needed to transform the original date as factor to be discrete. The tried the following code but it is giving me an error:

ggplot(data,aes(x=as.factor(DATE) ,y=as.factor(PRODUCT),fill=escale)) +
     geom_tile(colour="white",size=0.25) + 
     scale_x_discrete(position = "top",label=format(DATE,"%d/%y")) 

How can I write the correct code to format the x labels so it will be like "18/12" for example (dd/mm).

M--
  • 18,939
  • 7
  • 44
  • 76
  • 3
    When you set `x` as `factor`, it is not a date anymore, and `format` just won't work – HubertL Dec 18 '19 at 18:53
  • 3
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. you will probably want to avoid `as.factor` on the x value and use something like `scale_x_date()` instead depending on how your date is actually stored. – MrFlick Dec 18 '19 at 18:53
  • Hi MrFlick, scale_x_date() would show me weekends, something I need to filter therefore I converted in discrete scale to get the weekend out of the axis. – Jonathan Livingston Seagull Dec 18 '19 at 18:56
  • If you need to not show certain dates, you can do that, but factors are probably the least convenient way to do it – camille Dec 18 '19 at 19:13
  • thank you camille, i thought so but i also wondered there could be a way to use factors and format it as well. – Jonathan Livingston Seagull Dec 18 '19 at 19:23

1 Answers1

0

As noted in the comments, this is easier to do without converting to factor, though you need to play around with breaks. Using scales::date_format in scale_x_date:

library(ggplot2)
library(scales)

df <- data.frame(
  expand.grid(date = as.Date(c("2019-11-01", "2019-11-20")),
              y = letters[1:2]), 
  value = 1:4)

ggplot(df, aes(date, y, fill = value)) +
  geom_tile() +
  scale_x_date(breaks = df$date, 
               labels = date_format(format = "%d/%m"))

Created on 2019-12-18 by the reprex package (v0.3.0)

It also sounds like you may also get some mileage out of scales::date_breaks for your specific use case, but it's hard to say without more details.

MSR
  • 2,144
  • 1
  • 9
  • 21
  • hello, this could be a solution however I have a problem, the weekeds are showing as blank spaces because it is a continuous scale. – Jonathan Livingston Seagull Dec 18 '19 at 20:38
  • As suggested above, you can filter out weekend values before plotting. See e.g. [here](https://stackoverflow.com/questions/26441700/how-to-determine-if-date-is-a-weekend-or-not-not-using-lubridate) for some ideas on how to determine if a date is a weekend date. Then use e.g. `dplyr::filter` to remove. – MSR Dec 18 '19 at 20:50
  • I did filter the weekends (there is no data for it on the original dataset) however the x axis keeps showing blank spaces on it unless I turn it into a discrete value. – Jonathan Livingston Seagull Dec 18 '19 at 21:05