-1

I have hard time finding a solution for creating gradient color.
This is how it should look like(dont mind the blue bars)
enter image description here

Something similar to How to make gradient color filled timeseries plot in R, but a bit to advanced for me to reuse this example. I dont have any negative values and max is 80.I have tried the answer offered by nograpes, my PC was frozen for some 6-7 min and then I got message:
Error in rowSums(na) : 'Calloc' could not allocate memory (172440001 of 16 bytes)

This is only a subset of data with 841 rows (some containing NAs), and solution in previous answer could hardly work for me.

    df <- structure(list(date = structure(c(1497178800, 1497182400, 1497186000, 
1497189600, 1497193200, 1497196800, 1497200400, 1497204000, 1497207600, 
1497211200, 1497214800, 1497218400, 1497222000, 1497225600, 1497229200, 
1497232800, 1497236400, 1497240000, 1497243600, 1497247200, 1497250800, 
1497254400, 1497258000, 1497261600, 1497265200, 1497268800, 1497272400, 
1497276000, 1497279600, 1497283200, 1497286800, 1497290400, 1497294000, 
1497297600, 1497301200, 1497304800, 1497308400, 1497312000, 1497315600, 
1497319200, 1497322800, 1497326400, 1497330000, 1497333600, 1497337200, 
1497340800, 1497344400, 1497348000, 1497351600, 1497355200), class = c("POSIXct", 
"POSIXt"), tzone = "UTC"), dk_infpressure = c(22, 21.6, 21.2, 
20.9, 20.5, 20.1, 19.8, 19.4, 19, 18.6, 18.2, 17.9, 17.5, 17.1, 
16.8, 16.4, 16, 15.6, 15.2, 14.9, 14.5, 14.1, 13.8, 13.4, 13, 
12.5, 11.9, 11.4, 10.8, 10.3, 9.8, 9.2, 8.7, 8.1, 7.6, 7, 6.5, 
6, 5.4, 4.9, 4.3, 3.8, 3.2, 2.7, 2.2, 1.6, 1.1, 0.5, 0, 0)), .Names = c("date", 
"dk_infpressure"), row.names = c(NA, -50L), class = c("tbl_df", 
"tbl", "data.frame"))

Code to get basic plot:

 ggplot()+
geom_area(data=df, aes(x = date, y= dk_infpressure ) )+
scale_y_continuous(limits = c(0, 80))
m_c
  • 496
  • 2
  • 19
  • 2
    Do you want us to write the code for you? Because the answer is already there you only need to modify to work for datetime objects. – M-- Jun 10 '17 at 19:04
  • I have made changes to the question. Please tell me if I can do something more to make it more clear. – m_c Jun 11 '17 at 14:57

1 Answers1

1

Because geom_area can't take a gradient fill, it's a somewhat hard problem.

Here's a decidedly hacky but possibly sufficient option that makes a raster (but using geom_tile since x and y sizes differ) and covering the ragged edges with cropping and ggforce::geom_link (a version of geom_segment that can plot a gradient):

library(tidyverse)

df %>% 
    mutate(dk_infpressure = map(dk_infpressure, ~seq(0, .x, .05))) %>%    # make grid of points
    unnest() %>% 
    ggplot(aes(date, dk_infpressure, fill = dk_infpressure)) + 
    geom_tile(width = 3600, height = 0.05) + 
    # hide square tops
    ggforce::geom_link(aes(color = dk_infpressure, xend = lag(date), yend = lag(dk_infpressure)), 
                       data = df, size = 2.5, show.legend = FALSE) + 
    scale_x_datetime(expand = c(0, 0)) +    # hide overplotting of line
    scale_y_continuous(expand = c(0, 0))

alistaire
  • 42,459
  • 4
  • 77
  • 117
  • thanks @alistaire! I have a few questions: When I run this on my real data, it gives me error: `Error in seq.default(0, .x, 0.05) : 'to' must be a finite number`. How can I change color to yellow and red gradient? Can ylab be fixed at 80? – m_c Jun 11 '17 at 14:53
  • The error is probably because of an `NA` value in `dk_infpressure`. Since it won't plot anyway, you could just call `na.omit` or `tidyr::drop_na` on the whole data.frame before plotting. To change the gradient, use `scale_fill_gradient` and `scale_color_gradient` (or equivalents). – alistaire Jun 11 '17 at 22:54