ggplot using geom_line with multiple lines

Hello,
I'm plotting using ggplot and geom_line. The data is a data frame. It has this structure:

> str(dg100)
'data.frame':	78 obs. of  3 variables:
 $ date_g1: Factor w/ 116 levels "jan-mar 2010",..: 91 92 93 94 95 96 97 98 99 100 ...
 $ y       : num  6.7 6.74 6.47 6.36 6.51 ...
 $ type    : chr  "rate1" "rate1" "rate1" "rate1...

I had three rates (rate1, rate2, rate3) and in order to plot using the same data frame, I just used rbind and append the columns. Type, obviously, specify if the rate is 1, 2, or 3.

ggplot(data=dg100,aes(x=date_g1,y=y,col=type))+geom_line(group=1)


As you can see, the lines are not plotting in the right way. in fact, I have no idea why I don't have three colours showing the three rates. Also, I have doubts about the x-axis.

I declared the x-axis, date_g1, as factor. Can you guide me with this error? I know how to handle multiple columns and plots using one data frame, but no with geom_line.

Thanks for your time and interest.

Hi, could you edit your question to provide a reproducible example, called a reprex, please? It's hard to troubleshoot without data. They don't have to be your actual data, just representative. You can even start with iris or another standard data set that you can transform into something similar to your dg100

I found the answer. It was the factors. I declared them in the wrong way.
With the following code I get the results I wanted it.

myplot+scale_x_discrete(name="",limits=dg100$date_g1)

I will always check from now on if the x-axis variable is well declared.
This thread can be declared as SOLVED.

1 Like

Great! Will you please mark it as the solution for the benefit of future visitors? (No false modesty!)

Ok, technocrat.
I will say that most of x-axis errors are related with the factor attributes.
Thanks again for your replies, community.

1 Like

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