This is my code:
library(ggplot2)
df <- data.frame(x = 0:10, y = 0:10)
curve <- tibble( z_1 = seq(0, 20, length.out = 1500),x_1 = sin(z_1),
z_2 = sin(x_1) 2, z_3 = sin(x_1) 5)
base <- ggplot(df, aes(x, y)) geom_blank()
base geom_path(data = curve, aes(x = x_1,y = z_1), color ='red')
geom_path(data = curve, aes(x = x_1 1,y = z_1), color ='red')
geom_path(data = curve, aes(x = x_1 2,y = z_1), color ='red')
geom_path(data = curve, aes(x = x_1 3,y = z_1), color ='red')
I want the output to be something like this. But I would like to have the curves produced by geom_path
in x = 1, x = 2, x = 3, x = 4.
How can I do this? Should I change de data.frame?
Many thanks
CodePudding user response:
library(ggplot2)
library(dplyr)
library(purrr)
# For each different x-offset from 1-5
# Make a new tibble with a constant y value, and sin x value
# These get automatically merged together because of `map_dfr`
curve <- map_dfr(1:5, function(offset){
tibble(
y = seq(0, 20, length.out = 1500),
x = sin(y) offset,
offset = offset
)
})
# The only trick here is that we use group=offset to "restart" the
# function plot for each of the tibbles with different offsets created above
ggplot(curve, aes(x=x, y=y, group=offset))
geom_path(color='red')