multiple curves with ggplot2

Hello,

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 ?

Thanks for the help.

Yes, just as ggplot2 has a colour aesthetic, there is one for linetype. Here is documentation

Thanks for your answer. The graph below is exactly what I need but what is this "variable" object ?

# Mapping line type from a grouping variable
ggplot(economics_long, aes(date, value01)) +
  geom_line(aes(linetype = variable))

Here's what my data look like . I would like a curve for each value of p.

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))))

image

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.

economics_long

# A tibble: 2,870 x 4
   date       variable value  value01
   <date>     <chr>    <dbl>    <dbl>
 1 1967-07-01 pce       507. 0       
 2 1967-08-01 pce       510. 0.000265
 3 1967-09-01 pce       516. 0.000762
 4 1967-10-01 pce       512. 0.000471
 5 1967-11-01 pce       517. 0.000916
 6 1967-12-01 pce       525. 0.00157 
 7 1968-01-01 pce       531. 0.00207 
 8 1968-02-01 pce       534. 0.00230 
 9 1968-03-01 pce       544. 0.00322 
10 1968-04-01 pce       544  0.00319 
# … with 2,860 more rows

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")

probably :

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))),
p = as.character(p)) %>%
  ggplot(aes(t, Xt))+ ylim(0, Q0+1) +geom_line(aes(linetype = p)) +labs(title="Trajectoire en fonction de p" , y = "Xt")

Indeed, thank you to all.
Sorry for not giving my code, I felt like It was not necessary to give me a proper suitable answer.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.