Franza
Franza

Reputation: 45

Change color background in ggplot2 - R by specific Date on x axis

I want to change the color of the background in my scatterplot based on a specific date (on X axis). Ma dates range from 23th june 2017 to 6th december 2017. I want the background green from 23th june to 31th august, and all the rest red.

I've tried with this script here Change background color panel based on year in ggplot R but it doesn't work (I've never used ggplot2 before honestly). The date variable is POSIXct format. This is the script I used with the error that R gives me:

> ggplot() + geom_point() + 
geom_rect(aes(xmin = as.Date("2017-06-23"),xmax = as.Date("2017-08-31"),ymin = 0, ymax = Inf),
          fill="green", 
          alpha = .2)+
geom_rect(aes(xmin = as.Date("2017-09-01"),xmax = as.Date("2017-12-06"),ymin = 0, ymax = Inf),
          fill="red", 
          alpha = .2)
Errore: Invalid input: time_trans works with objects of class POSIXct only

What is wrong (or missed) with this script?

If it can be useful this is the str() of my dataset data

str(data)
'data.frame':   420 obs. of  2 variables:
 $ UTC.Date        : POSIXct, format: "2017-07-01" "2017-08-01" "2017-09-01" "2017-10-01" ...
 $ Mean.elevation  : num  1353 1098 905 747 1082 ...

Here there are some data if you want to try (first 30 lines of my dataset):

UTC.Date           Mean.elevation
1  2017-07-01      452.88224
2  2017-08-01      499.03211
3  2017-09-01      600.52692
4  2017-10-01      554.38923
5  2017-11-01      424.03798
6  2017-07-02      697.89243
7  2017-08-02      404.75938
8  2017-09-02      104.60064
9  2017-10-02     2194.45778
10 2017-11-02      314.21575
11 2017-12-02      464.44365
12 2017-07-03      876.20422
13 2017-08-03      308.53507
14 2017-09-03      377.45005
15 2017-10-03      805.73900
16 2017-11-03      405.05043
17 2017-07-04      939.72697
18 2017-08-04      508.95055
19 2017-09-04      763.68243
20 2017-10-04       64.56294
21 2017-11-04      783.69125
22 2017-07-05      505.33392
23 2017-08-05     1164.36239
24 2017-09-05     1534.99598
25 2017-10-05       12.05559
26 2017-11-05     1209.14527
27 2017-07-06      167.01947
28 2017-08-06      451.23450
29 2017-09-06      989.66036
30 2017-10-06       54.97960

Upvotes: 1

Views: 2756

Answers (2)

StupidWolf
StupidWolf

Reputation: 46908

Your input for xmin,xmax in geom_rect has to be the same type that in your data frame, right now you have POSIXct in your data frame and Date in your geom_rect. One solution is, provide the geom_rect with POSIX format data:

# your data frame based on first 5 values
df = data.frame(
UTC.Date = as.POSIXct(c("2017-07-01","2017-08-01","2017-09-01","2017-10-01","2017-11-01")),
Mean.elevation=c(1353,1098,905,747,1082))

RECT = data.frame(
       xmin=as.POSIXct(c("2017-06-23","2017-09-01")),
       xmax=as.POSIXct(c("2017-08-31","2017-12-06")),
       ymin=0,
       ymax=Inf,
       fill=c("green","red")
)

ggplot(df,aes(x=UTC.Date,y=Mean.elevation)) + geom_point()+
geom_rect(data=RECT,inherit.aes=FALSE,aes(xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax),
fill=RECT$fill,alpha=0.2)

Or convert your original data frame Time to Date:

df$UTC.Date = as.Date(df$UTC.Date)
ggplot(df,aes(x=UTC.Date,y=Mean.elevation)) + geom_point() + 
geom_rect(aes(xmin = as.Date("2017-06-23"),xmax = as.Date("2017-08-31"),ymin = 0, ymax = Inf),
          fill="green", 
          alpha = .2)+
geom_rect(aes(xmin = as.Date("2017-09-01"),xmax = as.Date("2017-12-06"),ymin = 0, ymax = Inf),
          fill="red", 
          alpha = .2)

The first solution gives something like:

enter image description here

Upvotes: 3

Jeroen Colin
Jeroen Colin

Reputation: 345

I don't believe there is something wrong with the part that needs to put colors on the year axis. The code below works on my system.

ggplot() + geom_point() + 
  geom_rect(aes(xmin = as.Date("2017-06-23"),xmax = as.Date("2017-08-31"),ymin = 0, ymax = Inf),
            fill="green", 
            alpha = .2)+
  geom_rect(aes(xmin = as.Date("2017-09-01"),xmax = as.Date("2017-12-06"),ymin = 0, ymax = Inf),
            fill="red", 
            alpha = .2)

I believe you get the error from plotting you actual data (which is not included in the code you provided.) Could you check if the answer on this question helps you out?

Upvotes: 0

Related Questions