R: Reshape data to create a multiline-plot from different variables

Multi tool use
R: Reshape data to create a multiline-plot from different variables
I have to create a plot with different lines from different variables.
One the y-axis:
> a1
[1] 7 10 12 19 30 45 68 104 151 214 279 362 425 467 487 493 498 500
> a2
[1] 7 11 20 33 59 94 151 244 334 425 483 500
> a3
[1] 7 17 35 77 150 241 361 450 488 500
> a4
[1] 7 20 50 129 292 441 498 500
> a5
[1] 7 22 68 191 382 493 500
On the x-axis:
> b1
[1] 18
> b2
[1] 12
> b3
[1] 12
> b4
[1] 10
> b5
[1] 8
a1 and b1 should be one line, a2 and b2 another and so on.
How can I fit the data to the same scale?
How can I generate a plot in which all lines are included?
Right now, the single plots look like this:
The code used for the plots looks like this:
plot(a1, type="l",col="red",xlim=c(1,b1),main="Example1", xlab="x", ylab = "y")
plot(a2,type="l",col="blue",xlim=c(1,b2),main="Example2", xlab="x", ylab = "y")
The requestet dput():
> dput(a1)
c(6, 8, 12, 20, 34, 54, 80, 110, 159, 214, 281, 345, 402, 447, 479, 492, 494, 498, 500)
> dput(b1)
19L
> dput(a2)
c(6, 12, 24, 42, 76, 127, 209, 306, 375, 441, 476, 495, 499, 499, 500)
> dput(b2)
15L
As you can probably tell, I am very new to using R.
I just edited my original question, to give you an example on how my single plots look right now.
– Cold2Breath
Jul 2 at 14:21
Please post the output of
dput
on your data, rather than a printout. What code got you those plots? And are you looking to translate this into a ggplot
solution, since you included that tag?– camille
Jul 2 at 14:25
dput
ggplot
Just did that. I found some solutions using ggplot, I tried it, but I wasn´t able to put my data into a dataframe.
– Cold2Breath
Jul 2 at 14:39
I'm confused. What are
b1
and b2
? Those just seem to be single numbers– camille
Jul 2 at 15:13
b1
b2
2 Answers
2
This is really a problem of getting data in shape in order to plot it. I'm a little confused by what the data is exactly that you're working with, i.e. what b1
, b2
, etc. are. But with the a1
and a2
you posted (and whatever similar vectors you have), you should get everything into a data frame. The fact that these vectors are of different lengths doesn't matter because ggplot
expects long-shaped data anyway. That is, as far as I can tell, a1
and a2
contain different sets of measurements of the same sort of thing (stock values, size of an organism, probability of some outcome, etc); therefore, in the tidy-data paradigm, they should be different groups of the same variable.
b1
b2
a1
a2
ggplot
a1
a2
So I first make a data frame/tibble of each set of data, giving them an x
that's just integers from 1 to whatever is the length of its a
vector, then give it a group marker so I can keep each set of measurements straight. Then I bind that into a single data frame.
x
a
library(tidyverse)
df1 <- tibble(
a = c(6, 8, 12, 20, 34, 54, 80, 110, 159, 214, 281, 345, 402, 447, 479, 492, 494, 498, 500),
x = 1:length(a),
group = 1
)
df2 <- tibble(
a = c(6, 12, 24, 42, 76, 127, 209, 306, 375, 441, 476, 495, 499, 499, 500),
x = 1:length(a),
group = 2
)
df <- bind_rows(df1, df2)
df
#> # A tibble: 34 x 3
#> a x group
#> <dbl> <int> <dbl>
#> 1 6 1 1
#> 2 8 2 1
#> 3 12 3 1
#> 4 20 4 1
#> 5 34 5 1
#> 6 54 6 1
#> 7 80 7 1
#> 8 110 8 1
#> 9 159 9 1
#> 10 214 10 1
#> # ... with 24 more rows
The ggplot
way of doing things is to map variables onto aesthetics such as color. If you find yourself making multiple geoms that serve essentially the same purpose (like a geom_line
for each of a set of observations), you probably need to reshape your data.
ggplot
geom_line
ggplot(df, aes(x = x, y = a, color = as.factor(group), group = as.factor(group))) +
geom_line()
This will scale for however many sets of observations you have—create smaller data frames for each a*
, mark them as a group, and bind them all together.
a*
That worked perfectly, thank you very much for your help! :)
– Cold2Breath
Jul 3 at 13:59
As with many things ggplot
, there are a lot of ways to do this. I think the easiest would be to save all your vectors in one data.frame/tibble and then refer to the columns in individual geom_line
calls. In the example below I made each line a different color just to more easily distinguish between the two.
ggplot
geom_line
library(tidyverse)
data <- tibble(a1 = c(1, 2 ,3, 4, 5), b1 = c(20), a2 = c(5, 6, 7, 8, 9), b2 = c(30))
ggplot(data = data) +
geom_line(aes(x = a1, y = b1), color = "blue") +
geom_line(aes(x = a2, y = b2), color = "red")
Or with different length data:
library(tidyverse)
data_1 <- tibble(a1 = c(1, 2 ,3, 4, 5), b1 = c(20))
data_2 <- tibble(a2 = c(5, 6, 7, 8, 9, 10, 11, 12), b2 = c(30))
ggplot() +
geom_line(data = data_1, aes(x = a1, y = b1), color = "blue") +
geom_line(data = data_2, aes(x = a2, y = b2), color = "red")
I just tried your code, I got an error due to the different collumn lengths.
– Cold2Breath
Jul 2 at 14:40
The code as is works (the column lengths are not different). But, if you're data has different lengths, then you'd need to put them in different data sets. I'll edit it to show you.
– Ben G
Jul 2 at 15:06
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Hi! What kind of result are you looking for? If you plot one line with one axe costant, you'd end up with a simple straight line. Is that correct with you?
– Riccardo Lavelli
Jul 2 at 14:05