Multiple line graph

Hi

I have problem how to make multiple linear graph. Sorry I don’t anything to show you, but I haven’t found where to start.
My data has 60 obs and 16 variables. First variable is fatherline (1-60), and others are two years periods as 1988-1989, 1999-1991 and so on until last one is 2016-2017. These years holds information about fatherlines contribution levels on that two years period.
What I want: linear graph where years are x-axis. Y-axis is contribution level so 30 is maximum on my data. One line is one fatherline and shows how strong contribution is every year so it will be easy to see what happens to that fatherline as time passes. I would love have all fatherlines on one chart too.

Does this make any sense to anybody?

Having 60 lines on one plot is likely to be confusing but here is an example. I colored the lines using fatherline as a continuous variable and I made all the lines parallel because that was easy.

library("ggplot2")
library(tidyr)
DF <- data.frame(fatherline = 1:60,
                 Y1988_Y1989 = seq(0.25, 15, 0.25),
                 Y1990_Y1991 = seq(1.25, 16, 0.25),
                 Y1992_Y1993 = seq(2.25, 17, 0.25))
head(DF)
#>   fatherline Y1988_Y1989 Y1990_Y1991 Y1992_Y1993
#> 1          1        0.25        1.25        2.25
#> 2          2        0.50        1.50        2.50
#> 3          3        0.75        1.75        2.75
#> 4          4        1.00        2.00        3.00
#> 5          5        1.25        2.25        3.25
#> 6          6        1.50        2.50        3.50

DFtall <- pivot_longer(DF, Y1988_Y1989:Y1992_Y1993, "Year")
head(DFtall)
#> # A tibble: 6 x 3
#>   fatherline Year        value
#>        <int> <chr>       <dbl>
#> 1          1 Y1988_Y1989  0.25
#> 2          1 Y1990_Y1991  1.25
#> 3          1 Y1992_Y1993  2.25
#> 4          2 Y1988_Y1989  0.5 
#> 5          2 Y1990_Y1991  1.5 
#> 6          2 Y1992_Y1993  2.5
ggplot(DFtall, aes(Year, value, group = fatherline, color = fatherline)) + geom_line()

Created on 2020-01-21 by the reprex package (v0.3.0)

Hi!

Thanks a lot!
I tried this but got error

DFtall <- pivot_longer(mergkok, 1988-1989:2016-2017, "Year")
Error: No common type for isalinja <factor<3f214>> and 1988-1889 .
Backtrace:

  1. tidyr::pivot_longer(mergkok, 1988 - 1989:2016 - 2017, "Year")
  2. vctrs:::vec_ptype2.factor.default(...)
  3. vctrs::stop_incompatible_type(x, y, x_arg = x_arg, y_arg = y_arg)
  4. vctrs:::stop_incompatible(...)
  5. vctrs:::stop_vctrs(...)

isalinja = fatherline

Does it matter that I have some NA-values?

The NA values should not be a problem. The first thing I would try is to change the names of your columns to something less likely to cause trouble. The name 1988-1989 looks like a mathematical expression. If you change the names to the pattern Y1988_1989, you may solve the problem. Alternatively, you can put the column names between back tics

DFtall <- pivot_longer(mergkok, `1988-1989`:`2016-2017`, "Year")

Thank you so much!
Back tics were the solution.
Also you was absolutely right that chart is too confusing to read. I need to think how to break my data. I have few big values, but most numbers are small so charts bottom is just rainbow.

Thanks!

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