-1

I am trying to use ggplot2 to plot multiple lines in one R plot but I have a problem and I am not able to. This is my first question here. I am learning how to use R studio and its package so I don't know it very well so please be patient. This is the code I wrote:

library(ggplot2) 
x <- 1:10
y1 <- dati.m$With.no.educational.qualifications
y2 <- dati.m$Compulsory.education..1st.cycle
y3 <- dati.m$Compulsory.education..2nd.cycle
y4 <- dati.m$Compulsory.education..3rd.cycle
y5 <- dati.m$Upper.secondary.education
y6 <- dati.m$Higher.education
df <- dati.m(x, y1, y2, y3, y4, y5, y6)
ggplot(df, aes(x)) +
  geom_line(aes(y=y1),
            colour="red") +
  geom_line(aes(y=y2),
            colour="green") +
  geom_line(aes(y=y3),
            colour="blue") +
  geom_line(aes(y=y4),
            colour="yellow") +
  geom_line(aes(y=y5),
            colour="orange") +
  geom_line(aes(y=y6),
            colour="black")

but when I execute it R, I get an error:

Error: Aesthetics must be either length 1 or the same as the data (17): y, x

lmo
  • 37,904
  • 9
  • 56
  • 69
  • 1
    Please provide a reproducible example. You can see examples here: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – lizzie Jan 27 '16 at 23:21

1 Answers1

2

I replaced df <- dati.m(x, y1, y2, y3, y4, y5, y6) by df <- data.frame(x, y1, y2, y3, y4, y5, y6)

If you use this code:

library(ggplot2) 
x <- 1:10
y1 <- 1:10
y2 <- 2:11
y3 <- 3:12
y4 <- 4:13
y5 <- 5:14
y6 <- 6:15
df <- data.frame(x, y1, y2, y3, y4, y5, y6)
ggplot(df, aes(x)) +
        geom_line(aes(y=y1),
                  colour="red") +
        geom_line(aes(y=y2),
                  colour="green") +
        geom_line(aes(y=y3),
                  colour="blue") +
        geom_line(aes(y=y4),
                  colour="yellow") +
        geom_line(aes(y=y5),
                  colour="orange") +
        geom_line(aes(y=y6),
                  colour="black")

you will get this :

enter image description here

HubertL
  • 19,246
  • 3
  • 32
  • 51
  • First of all you thank you for the answer! Maybe i made a mistake searching the right example code for what i was thinking to do or i was not good enough explaining my self. I try to explain better. I wrote a script that download data from Quandl about level of schooling in portugal divided in years number of people and level oh schooling, then i wrote a code that draw a plot for every school level obtaining the following plots: link to the album with image: http://imgur.com/a/BVwqH now, i would like to do a single plot with all the data together, a plot with all the different lines – Marco Scapolo Jan 28 '16 at 20:46
  • Because your code can plot several lines (as shown in my answer), this means the problem is not from the code. So it's probably from your data. If you plot 10 x, you also need to plot 10 y – HubertL Jan 28 '16 at 21:01
  • So what do you recommend to do ? Should i post my entire script so you can open it or read it and tell what am i doing wrong ? Never mind, i want to say thank you again for your time and your answer!! – Marco Scapolo Jan 28 '16 at 21:22
  • I think you should try to simplify your data **and** your code to the minimum that reproduces the error you don't understand – HubertL Jan 28 '16 at 21:26
  • i just corrected the code replacing what you suggested me and i changed the x data values because i realized it was incorrect, now when i run the script r studio say me "geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?" what does it mean ? how can i fix it ? – Marco Scapolo Jan 28 '16 at 23:49