Reputation: 317
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:
I want to colour the other area of the "mountain"
Upvotes: 0
Views: 1316
Reputation: 16862
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).
Upvotes: 1
Reputation: 4230
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
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
Upvotes: 1