1

scale_date() set of functions accept scale position on top or bottom (for horizontal scales) OR right or left (for vertical scales). However it lacks scale_continuous(sec.axis) which allows for secondary axis (say to put time range both at top and bottom).

Here is the code producing dates just at bottom of the chart:

library(data.table)
library(ggplot2)
set.seed(123)

foo <- data.table(day = rep(seq(as.Date("2018-01-01"), as.Date("2018-01-30"), by="days"), 3),
                       var = c(rep("v1", 30), rep("v2", 30), rep("v3", 30)),
                       value = round(rnorm(90)*90))

ggplot2::ggplot(data = foo, ggplot2::aes(x = day, y = value, color = var)) + 
  ggplot2::geom_line() +
  ggplot2::ggtitle("Demo example",
                   subtitle = "random values") +
  ggplot2::scale_x_date(date_labels = "%Y%m%d",
                        breaks = seq(from = min(foo$day), to = max(foo$day), length.out = 5),
                        position = "bottom") +
  ggplot2::facet_grid(var ~ ., scales = "free") +  
  ggplot2::theme(legend.position = "top",
                 legend.title = ggplot2::element_blank(),
                 legend.direction = "horizontal",
                 plot.title = ggplot2::element_text(hjust = 0.5),
                 plot.subtitle = ggplot2::element_text(hjust = 0.5))

Output as is follows: scale_date at bottom I have many time series graphs which I am plotting in stacked way. This make the scroll necessary for a user to see time scale (not good UX). So I want to have time scale both at top and bottom. How can I achieve this?

abhiieor
  • 3,132
  • 4
  • 30
  • 47
  • 1
    It's more likely that we will be able to help you if you provide [a complete minimal reproducible example](http://stackoverflow.com/help/mcve) to go along with your question. Something we can easily pick up and work from. Something we can use to show you how it might be possible to answer your question. – Eric Fail Jan 22 '18 at 13:18
  • See [this issue](https://github.com/tidyverse/ggplot2/issues/2244), and the accepted answer in the duplicate. – Axeman Jan 22 '18 at 14:20

1 Answers1

0

For the sake of completeness here is the code (courtesy @axeman):

library(data.table)
library(ggplot2)
set.seed(123)

foo <- data.table(day = rep(seq(as.Date("2018-01-01"), as.Date("2018-01-30"), by="days"), 3),
                       var = c(rep("v1", 30), rep("v2", 30), rep("v3", 30)),
                       value = round(rnorm(90)*90))

ggplot(data = foo[, day1 := as.numeric(day)], aes(x = day1, y = value, color = var)) + 
  geom_line() +
  ggtitle("Demo example",
                   subtitle = "random values") +
  scale_x_continuous(sec.axis = dup_axis(), breaks = seq(from = min(foo$day1),
                                                to = max(foo$day1), length.out = 5),
                              labels = function(breaks) {lubridate::as_date(breaks)}) +
  facet_grid(var ~ ., scales = "free") +  
   theme(legend.position = "top",
                  legend.title = element_blank(),
                  legend.direction = "horizontal",
                  plot.title = element_text(hjust = 0.5),
                  plot.subtitle = element_text(hjust = 0.5))
abhiieor
  • 3,132
  • 4
  • 30
  • 47