1

I'm trying to plot multiple dataframe with smooth line using ggplot() or stat_smooth() with a png as a background.

I have three dataframes

data1 <- data.frame(Max.x, Max.y)  
data2 <- data.frame(Med.x, Med.y)  
data3 <- data.frame(Min.x, Min.y)  

and a picture(png) file

mypng <- readPNG("picture.png")

I found that with stat_smooth(span=0.4) it will create a smooth line that goes through all the points in the dataframe.

Now I'm trying to plot the three smooth lines on mypng

I've tried couple things.

ggplot(data1, aes(x=Max.x, y=Max.y))+scale_x_continuous(limits = c(126,129))+scale_y_continuous(limits = c(37.5, 38.5))+ annotation_raster(mypng, xmin=126, xmax=129, ymin=37.5, ymax=38.5)+stat_smooth(span=0.4) 

Same goes for data2 and data3. This plots each line on a png but I can't find a way to get all three to lines on a same png.
Also, note that the plot has to have set x,y max and min b/c my data falls between those coordinates.

Another thing I tried:

ggplot()+annotation_raster(mypng, xmin=126, xmax=129, ymin=37.5, ymax=38.5)+stat_smooth(data=data1, aes(x=Max.x, y=Max.y), span=0.4)+stat_smooth(data=data2, aes(x=Med.x, y=Med.y), span=0.4)+stat_smooth(data=data3, aes(x=Min.x, y=Min.y), span=0.4)+scale_x_continuous(limits=c(126,129))+scale_y_continuous(limits=c(37.5,38.5))

This plots all three smoothlines. However the picture doesnt show up.

I am now stuck... I can get three different plots on mypng. And I can get all three plots but not on mypng. How can I get all three lines on mypng?

Bryan Han
  • 67
  • 6
  • You have a typo in your code: `annotation_rater` should be `annotation_raster`. Also, if you stack your three data frames into a single data frame, you'll need only one call to `geom_smooth`. – eipi10 Dec 21 '16 at 04:17
  • Sorry for the type. Im working on a computer without a internet so I had to manually rewrite the code here.. And to your point.. I cant combine the dataframe because I see three different lines. If I stack them together, it will only create one. – Bryan Han Dec 21 '16 at 04:40
  • Yes, but you add another grouping column that marks which data frame each row came from and you map that column to a `group` or `colour` aesthetic. @Mist's answer shows what I mean. – eipi10 Dec 21 '16 at 05:00

1 Answers1

5

How's this:

library(png)
library(ggplot2)

data1 <- data.frame(series = rep("max",10),
                    x = rnorm(10, mean = 127.5, sd = 1), 
                    y = rnorm(10, mean = 38, sd = 0.25))  
data2 <- data.frame(series = rep("med",10),
                    x = rnorm(10, mean = 127.5, sd = 1), 
                    y = rnorm(10, mean = 38, sd = 0.25))  
data3 <- data.frame(series = rep("min",10),
                    x = rnorm(10, mean = 127.5, sd = 1), 
                    y = rnorm(10, mean = 38, sd = 0.25))  

df <- rbind(data1,data2,data3)

mypng <- readPNG("C:/Users/lorenzr/Pictures/family.png")

ggplot(df, aes(x=x, y=y, colour = series))+
  scale_x_continuous(limits = c(126,129))+
  scale_y_continuous(limits = c(37.5, 38.5))+
  annotation_raster(mypng, xmin=126, xmax=129, ymin=37.5, ymax=38.5)+
  stat_smooth(span=0.4) 

enter image description here

Mist
  • 1,888
  • 1
  • 14
  • 21