I created this topic a few weeks ago :topic. I wanted to display multiples curves labelled 1,2,... (available in a dataframe) with ggplot2 on the same graph with the adequate legend and a very fine solution was proposed using the function "randomColor". Unfortunately, it turns out I cannot use colors on my graphs for editing reasons, so I was told to plot lines instead. So, is there a way to do exactly the same graph but with different types of lines (dotted, dashed, solid,...) with the legend generated automatically again ?
economics_long is a data frame with four variables. The code above maps x to date, y to value01, and linetype to the variable called variable.
For your data, you might need to map linetype to p, but I'm not sure.
It will be much easier to give you concrete suggestions if you can share reproducible code that we can run. As part of that, it would be nice to have some sample data with the same structure you're working with. (Your code goes in that direction, but it's not explicit which libraries you're relying on or where the traj function comes from.)
It's often easiest to share data using R's dput() function, which creates a "recipe" to make an exact copy of an R object, such as a data frame. For instance dput(head(YOUR_DATA), 100) would create code that, when run by others, will reproduce the first 100 rows of your data.
Ok. Actually, thanks to your last response, I see that the object "variable" is of type character and my object p is a float. So I just needed to convert p into a character and that worked just fine as below. I would just need in the legend the label "p" and not "as.character(p)" if you can help me with that.
data.frame(p = seq(0.1, 0.9, 0.2)) %>%
mutate(Xt = map(p, ~traj(N,Q0,T,.x)$X)) %>%
unnest(Xt) %>%
mutate(t = rep(c(-1:N+1), times = length(unique(p)))) %>%
ggplot(aes(t, Xt))+ ylim(0, Q0+1) +geom_line(aes(linetype = as.character(p))) +labs(title="Trajectoire en fonction de p" , y = "Xt")