Reputation: 13
I have this code for the purpose of making a figure
milliseconds <- c(0,50,100,150,200,250,300)
force <- c(20,75,120,260,400,500,600)
data <- data.frame(miliseconds,force)
I need to shade 0 to 50 milliseconds under the curve.
So far I have
RFD <-ggplot(data = data, mapping = aes(x = miliseconds, y = force)) +
geom_line(color="black", size=2) +
geom_area(mapping = aes(x = ifelse(x>0 & x<50 , x, 0)),
geom_params = list(fill = "red", alpha = 0.5)) +
scale_y_continuous(limits = c(0, max(data$force)))
geom_point(size=4, color="black") +
theme_classic() +
ggtitle("Rate of Force Developement")
RFD +scale_x_continuous(name = "Milliseconds",
breaks = seq(0, 300, 50),
limits=c(0, 300)) +
scale_y_continuous(name = "Force")
##########
but Im getting an error that I cant ggproto object together. any suggestions?
Upvotes: 1
Views: 74
Reputation: 4524
After removing typos, I think the main problem is that there are no data within the limits ( > 0 & < 50
) you set.
But you also should apply this filter to data
in geom_area
with the same mapping as in ggplot
. I have used slightly different filters for getting at least some data. If you put fill
and alpha
just outside of the aesthetics it will work.
You do not have to use df$force
in scale_y_continuous
.
PS:
linewidth
instead of size
in geom_line
.+
geom_params
library(tidyverse)
milliseconds <- c(0,50,100,150,200,250,300)
force <- c(20,75,120,260,400,500,600)
df <- data.frame(milliseconds,force)
RFD <- ggplot(data = df, mapping = aes(x = milliseconds, y = force)) +
geom_line(color="black", linewidth=2) +
geom_area(data = df |> filter(milliseconds > 0 & milliseconds < 150),
mapping = aes(x = milliseconds, y = force),
fill = 'red', alpha = .5) +
scale_y_continuous(name = 'Force', limits = c(0, max(force))) +
scale_x_continuous(name = "Milliseconds",
breaks = seq(0, 300, 50),
limits=c(0, 300)) +
geom_point(size=4, color="black") +
theme_classic() + theme(legend.position = 'none') +
ggtitle("Rate of Force Developement")
RFD
Upvotes: 1