1

Ok, lets take some sample data.

A <- sample(1:100, 25)
B <- sample(1:25, 25)
df.1 <- data.frame(A,B)


C <- sample(1:80, 15)
D <- sample(1:15, 15)
df.2 <- data.frame(C,D)

Then we plot the data using ggplot

library(ggplot2)

(plot2 <- ggplot(NULL) + 
   geom_point(data=df.1, aes(x=A, y=B), 
              color='black', cex=1, pch=16 ) +
   geom_smooth(data=df.1, aes(x=A, y=B), method="lm", size=1, 
               se=FALSE, colour="black", linetype=2)+
   geom_point(data=df.2, aes(x=C, y=D), 
              color='black', cex=1, pch=15 ) +
   geom_smooth(data=df.2, aes(x=C, y=D), method="lm", size=1,  
               se=FALSE, colour="black", linetype=1)+
   scale_y_continuous("Y scale") +
   ggtitle("Plot") +
   theme_bw()+
   theme(plot.title = element_text(face="bold", size=20),
         axis.title.x = element_text(vjust=-0.25),
         axis.title.y = element_text(vjust=1),
         axis.title = element_text(face="bold", size=15)
   )
)

So we have created and modified the title, axis, etc.

But I want to create a legend which shows the linetype's from the geom_smooth() function of df.1 and df.2. It should be in the top right of the graph.

(so for df.1 we want a solid line and df.2 a dashed line)

The example here walks you through an example, but the data comes from within the same data set

3
  • I'm curious, why do you need the two dataframes? Would it not be easier to first merge the data and then group by source? Commented Aug 6, 2015 at 14:03
  • lets say they are of differenet lengths and different variables name. But happy to see a solution if you can combine to one dataframe Commented Aug 6, 2015 at 14:04
  • 1
    Done. In hindsight, merge was the wrong word. I meant combine. Commented Aug 6, 2015 at 14:12

1 Answer 1

1

Here you go:

#combine and create x and y (as mappings follow
#same pattern)
df.1$group <- "df.1"
df.1$x <- df.1$A
df.1$y <- df.1$B

df.2$group <- "df.2"
df.2$x <- df.2$C
df.2$y <- df.2$D

library(plyr) #for rbind.fill

df.all <- rbind.fill(df.1,df.2)

plot3 <- ggplot(df.all, aes(x=x,y=y,group=group)) + 
  geom_point(color='black', cex=1, pch=16 ) +
  geom_smooth(aes(linetype=group),method="lm", size=1, 
                         se=FALSE, colour="black") +
  scale_y_continuous("Y scale") +
  ggtitle("Plot") +
  theme_bw()+
  theme(plot.title = element_text(face="bold", size=20),
        axis.title.x = element_text(vjust=-0.25),
        axis.title.y = element_text(vjust=1),
        axis.title = element_text(face="bold", size=15)
  ) +
#add custom linetypes (not necessary now, as default mapping to 1 and 2)
plot3 + scale_linetype_manual(values=c("df.1"=1,"df.2"=2))

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

thanks, so when you have data.frames of variables length, its best to combine them and then plot. thanks for the suggestion
Not necessarily, but when you're doing essentially the same with each dataframe (like you're doing here) it's better to combine with a column indicating which group a measurement belongs to. Then you can group by group indicator and assign aesthetics (using aes(...) which automatically generate the legend.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.