0

I don't know how can I plot in better way.

I have

df1 <- data.frame(x=c(1,3,5), y=c(2,4,6))

df2 <- data.frame(x=c(2,6,10,12), y=c(1,4,7,15)

Those data frames have x as time, y as its own value.

  1. I have data-frames with different amount of elements
  2. I want to combine this data by x (time), but I need one method of two to show them on one plot: a) to show df1.y on x axis of a plot to see distribution df2 by df1, so these two data frames should be connected by the time (x) but shown each on one of two axis, or b) to show three axis, and for df1.y the y axis should be at the right side of a plot.

enter image description here enter image description here

Alexander.Iljushkin
  • 4,519
  • 7
  • 29
  • 46

1 Answers1

1

For a better terminology, I will rename your example variables according to your sample plots.

df1 <- data.frame(time=c(1,3,5), memory=c(2,4,6))
df2 <- data.frame(time=c(2,6,10,12), threads=c(1,4,7,15))

Your first plot:

From your description, I assume that you want to do the following: For each available time value get the value of df1$memory and df2$threads. However, that value may not always be available. One suitable approach is to fill up missing values by linear interpolation. This may be done using the approx-function:

merged.time <- sort(unique(c(df1$time, df2$time))
merged.data <- data.frame(time = merged.time,
                          memory = approx(df1$time, df1$memory, xout=merged.time)$y
                          threads = approx(df2$time, df2$threads, xout=merged.time)$y
               )

Note that appprox(...)$y just extracts the interpolated data.

Plotting may now be done using standard plotting commands (or, as your tags suggest, using ggplot2:

ggplot(data=merged.data, aes(x=memory, y=threads)) + geom_line()

Your second plot

... is not possible with ggplot2. That is for numerous reasons, for example see here.

Community
  • 1
  • 1
Thilo
  • 8,827
  • 2
  • 35
  • 56