0

I am sure it is easy to solve this question.

I am trying to fill the area below the line I plot from "mydata" using ggplot2.

here the example:

a = seq(10, 100, 5)
b = -(runif(19, 0, 50))
mydata <- data.frame(a, b)

ggplot(data = mydata, aes(x = a, y = b)) +
  geom_area()

geom_bar fills above the line, considering the area the one from 0 and the values (negative). I want to fill the other area and I think I should delimit the area I want to colour, but I do not know how.

The script above is just an easy example. My real script is

ggplot(data = ctd, aes(x = Longitude, y = Depth)) +
  geom_raster(aes(fill = NO3_uM)) +
  scale_fill_gradientn(colours = rev(my_colours)) +
  geom_contour(aes(z = NO3_uM), binwidth = 2, colour = "black", alpha = 0.2) +
  #geom_contour(aes(z = NO3_uM), breaks = 20, colour = "black") +

  geom_point(data = ctd, aes(x = Longitude, y = Depth),
             colour = 'black', size = 3, alpha = 1, shape = 15) +
  geom_area(data = trsect, aes(x = Longitude, y = Depth), fill = "black")+
  ylim(-320,0)

my result is:

enter image description here enter image description here

I want to colour the other area of the "mountain"

Strobila
  • 317
  • 3
  • 15

2 Answers2

1

You can try to make a vector to define a new area.

qq = rep(-60, length(b))
ggplot(data=mydata, aes(x = a, y = b))+
 geom_area(data=data.frame(qq), aes(y=qq), fill='red', alpha=0.5) +
 geom_area(aes(y=b), col='black') 

Which produces

enter image description here

You can play with fill and the theme to have a different set of options (It is not quite clear what's your desired output).

ggplot(data=mydata, aes(x = a, y = b))+
   geom_area(data=data.frame(qq), aes(y=qq),fill='red', alpha=0.5) +
   geom_area(aes(y=b), fill='white') + theme_void()

Which produces

enter image description here

Matias Andina
  • 4,029
  • 4
  • 26
  • 58
  • This is a nice option, but unfortunately, it is not convenient in my case, because this area is not the only data I have to plot, and this way will cover everything. – Strobila Apr 29 '18 at 13:59
  • My question is similar to this one: https://stackoverflow.com/questions/44656444/fill-area-with-geom-area-under-negative-values?noredirect=1&lq=1 – Strobila Apr 29 '18 at 14:16
  • @LucaStirnimann We can definitely work it out but if `this area is not the only data I have to plot`, it would be really really nice to have a more defined idea of what is the expected output. The link shows a graph that almost the answer posted here (you would only need to add the axis and tweak a few things). Please provide at least a drawing of the expected plot – Matias Andina Apr 30 '18 at 13:21
1

You can use geom_ribbon for this. geom_area is a special case of geom_ribbon where ymin is set to 0. Instead, set ymin to the minimum of your y-values, i.e. ymin = min(b). Or if you wanted some extra space, you could do ymin = min(b) - 2 or something like that.

library(tidyverse)

a = seq(10, 100, 5)
b = -(runif(19, 0, 50))
mydata <- data.frame(a, b)

ggplot(mydata) +
    geom_ribbon(aes(x = a, ymin = min(b), ymax = b))

Created on 2018-05-04 by the reprex package (v0.2.0).

camille
  • 16,432
  • 18
  • 38
  • 60