0

I need to show 3 ggplot scatterplots and one dendrogram on one page. How can I make the point colours the same in each scatter plot (i.e. I need the points for group two to be the same colour for all 3 graphs).

require(graphics)
require(ggplot)
require(ggdendro)


#Scatter plots
df1<-data.frame(x=c(3,4,5),y=c(15,20,25),grp=c(1,2,2))
df1$grp =factor(df1$grp)
colnames(df1)[3]="Group"
p<-ggplot(df1,aes(x,y))
p<-p+ geom_point(aes(colour=factor(Group)),size=4)
p1<-p + coord_fixed()


df2<-data.frame(x=c(3,4,5,6),y=c(15,20,25,30),grp=c(1,2,2,3))
df2$grp =factor(df2$grp)
colnames(df2)[3]="Group"
p<-ggplot(df2,aes(x,y))
p<-p+ geom_point(aes(colour=factor(Group)),size=4)
p2<-p + coord_fixed()


df3<-data.frame(x=c(3,4,5,6,7),y=c(15,20,25,30,35),grp=c(1,2,2,3,4))
df3$grp =factor(df3$grp)
colnames(df3)[3]="Group"
p<-ggplot(df3,aes(x,y))
p<-p+ geom_point(aes(colour=factor(Group)),size=4)
p3<-p + coord_fixed()

#Dendrogram
dis <- hclust(dist(USArrests), "ave")       
d<-as.dendrogram(dis)
ddata<-dendro_data(d,type="rectangle")
dp<-ggplot(segment(ddata)) + geom_segment(aes(x=x,y=y,xend=xend,yend=yend))
dp<-dp+geom_hline(aes(yintercept=50),colour="red")

I tried used the multi plot function multiplot(p1,p2,p3,dp,cols=2) and got:

enter image description here

Bonus: The graphs all have a fixed aspect ratio such that scatterplot are different sizes, which is fine but I really don't need the scatterplot to take up so much space. How can I control how much space each figure is given in the final figure?

Drew Steen
  • 16,045
  • 12
  • 62
  • 90
Elizabeth
  • 6,391
  • 17
  • 62
  • 90

0 Answers0