Moving all data points of a 'group' in ggplot

Viewed 271

I'm having a hard time even articulating the question -

I think it'll be easier if I just showed my objective: Take this example:

require(tidyverse)
df <- data.frame(planet = rep(c("mars", "jupiter"), each = 10),
                 date = seq(as.Date("2020-01-01"), as.Date("2020-01-10"), length.out = 10) %>% rep(2),
                 someNumbers = c(1:10, 6:15)
                 )

df
    planet       date someNumbers
1     mars 2020-01-01           1
2     mars 2020-01-02           2
3     mars 2020-01-03           3
4     mars 2020-01-04           4
5     mars 2020-01-05           5
6     mars 2020-01-06           6
7     mars 2020-01-07           7
8     mars 2020-01-08           8
9     mars 2020-01-09           9
10    mars 2020-01-10          10
11 jupiter 2020-01-01           6
12 jupiter 2020-01-02           7
13 jupiter 2020-01-03           8
14 jupiter 2020-01-04           9
15 jupiter 2020-01-05          10
16 jupiter 2020-01-06          11
17 jupiter 2020-01-07          12
18 jupiter 2020-01-08          13
19 jupiter 2020-01-09          14
20 jupiter 2020-01-10          15

If I plot

ggplot(df, aes(date, someNumbers, group = planet)) + geom_bar(stat = "identity", position = "dodge", aes(fill = planet))

enter image description here


I can see that the two planets are following the same sequence in someNumbers. What I'd like to do is move all of the mars blue bars to the left by 4-5 days so that I can easily compare the sequence visually.

I can do this by:


df.mars <- subset(df, planet == "mars") 
df.jupiter <- subset(df, planet == "jupiter")

df.mars$date <- df.mars$date - 5 #subset mars and manually minus 5

bind_rows(df.mars, df.jupiter) %>% ggplot(aes(date, someNumbers, group = planet)) + geom_bar(stat = "identity", position = "dodge", aes(fill = planet))

enter image description here

However, I am actually changing the underlying values of date for mars.

Can I achieve the same thing without actually changing the underlying date values and just shift all the mars geom_bar() to the left or right by an arbitrary amount? Ideally, I would have two x-axis labels: one for mars one for jupiter.

Rant: This question came up when I was playing around with the covid19 data set. I was plotting bar plots grouped by countries with:

Y-axis = #cases
X-axis = Date

I wanted to move all my USA bars to the left by n Days so that I can make a statement along the lines of "USA trend is following that of Italy with n Days behind"

1 Answers

You could use a secondary axis.

bind_rows(df.mars, df.jupiter) %>% 
  ggplot(aes(date, someNumbers, fill = planet)) +
  geom_col(position = 'dodge') +
  scale_x_date('Mars date', sec.axis = sec_axis(~. - 5, 'Jupiter date'))

enter image description here

A prettier version using ggtext:

library(ggtext)
bind_rows(df.mars, df.jupiter) %>% 
  ggplot(aes(date, someNumbers, fill = planet)) +
  geom_col(position = position_dodge(preserve = 'single'), show.legend = FALSE) +
  scale_x_date(
    name = "<i style='color:firebrick'>Mars date</i>",
    sec.axis = sec_axis(~. - 5, "<i style='color:navy'>Jupiter date</i>")
  ) +
  scale_fill_manual(values = c(jupiter = 'navy', mars = 'firebrick')) +
  theme_minimal() +
  theme(axis.title.x = element_markdown(), axis.title.x.top = element_markdown())

enter image description here

Related