I am trying to create a function in R to plot different kinds of data on the same plot, and the legend is my white whale. I've got two (closest to what I want) different approaches, and neither of them gives me the result I am hoping for - a vertical and a horizontal lines in the legend symbols. Instead, they produce crosses. Any help would much appreciated! Here is the link to the RData file . And those are stripped down functions:
###### "all color" variant
plot.color <- function(bed,genome,circles=NULL,pointers=NULL,rectangles=NULL) {
require(ggplot2)
leg.colors<-c("cen"="green3","fit"="gray25","value"="blue","circles"="gray50","pointers"="orange","rectangles"="red")
leg.shapes<-c(NA,NA,20,1,6,0)
leg.lines<-c("solid","solid","blank","blank","blank","blank")
leg.sizes<-c(1,1,4,4,4,5)
plot<-ggplot(bed)
plot<-plot+scale_y_continuous(limits = c(0.8, 2.2)) ## y scale
plot<-plot+scale_x_continuous(limits=c(0,NA)) ## x scale
plot<-plot+facet_grid(chr ~ .,scales = "free", space = "free_x") ## Facet
plot<-plot+geom_segment(aes(x=genome$cen,xend=genome$cen,y=1,yend=2),data=genome,color="green3") ## Green vlines
plot<-plot+geom_vline(aes(xintercept=-1,color="cen"),data=genome) ## Dummy for the legend (makes vlines)
plot <- plot+geom_rect(aes(xmin=start,xmax=end,ymin=0.83,ymax=0.97,color="rectangles"),data=rectangles,fill=NA) ## Rectangles
plot<-plot+geom_segment(aes(x=0,xend=end,y=0.9,yend=0.9),data=genome,size=.7) ## Chromosome length line
plot <- plot+geom_point(aes(x=coord,y=0.9,color="circles"),data=circles,size=2,fill="white",shape=21) ## Circles
plot <- plot+geom_point(aes(x=coord,y=.95,color="pointers"),data=pointers,size=2.3,fill=NA,shape=6) ## Triangles
plot<-plot+geom_line(aes(x=coord,y=fit,group=group,color="fit")) ## Line
plot<-plot+geom_point(aes(x=coord,y=value,color="value"),shape=20,size=.1) ## Dots
plot<-plot+scale_colour_manual(values=leg.colors,guide=guide_legend(override.aes=list(linetype=leg.lines,shape=leg.shapes,size=leg.sizes)), breaks=names(leg.colors))
X11()
print(plot)
}
plot.color(data,genome,ori,pointers,region)
###### "color & shape" variant
plot.fill <- function(bed,genome,circles=NULL,pointers=NULL,rectangles=NULL) {
require(ggplot2)
my.colors <- c("cen"="green3","fit"="gray25","value"="blue")
color.lines <- c("solid","solid","blank")
color.shapes <- c(NA,NA, 20)
color.sizes <- c(1,1,4)
my.fills <- c("circles"="white","pointers"="orange","rectangles"=NA)
fill.shapes <- c(21,25,22)
fill.sizes <- c(4,4,5)
fill.colors <- c("black","black","red")
plot<-ggplot(bed)
plot<-plot+scale_y_continuous(limits = c(0.8, 2.2)) ## y scale
plot<-plot+scale_x_continuous(limits=c(0,NA)) ## x scale
plot<-plot+facet_grid(chr ~ .,scales = "free", space = "free_x") ## Facet
plot<-plot+geom_segment(aes(x=genome$cen,xend=genome$cen,y=1,yend=2),data=genome,,color="green3") ## Green vlines
plot<-plot+geom_vline(aes(xintercept=-1,color="cen"),data=genome) ## Dummy for the legend (makes vlines)
plot <- plot+geom_rect(aes(xmin=start,xmax=end,ymin=0.83,ymax=0.97),data=rectangles,color="red",fill=NA) ## Rectangles
plot<-plot+geom_segment(aes(x=0,xend=end,y=0.9,yend=0.9),data=genome,size=.7) ## Chromosome length line
plot <- plot+geom_point(aes(x=0,y=0.7,fill="rectangles"),data=rectangles,color="red",shape=22,show.legend=T) ## Dummy to fit the rectangle into "fill" legend
plot <- plot+geom_point(aes(x=coord,y=0.9,fill="circles"),data=circles,color="black",size=2,shape=21) ## Circles
plot <- plot+geom_point(aes(x=coord,y=.95,fill="pointers"),data=pointers,size=3,shape=25,stroke=0.5) ## Triangles
plot<-plot+geom_line(aes(x=coord,y=fit,group=group,color="fit")) ## Line
plot<-plot+geom_point(aes(x=coord,y=value,color="value"), size=0.1,alpha=0.5) ## Dots
plot<-plot+scale_colour_manual(values=my.colors,guide = guide_legend(override.aes = list(
linetype = color.lines, shape = color.shapes,size=color.sizes)),breaks=names(my.colors))
plot<-plot+scale_fill_manual(values=my.fills,guide = guide_legend(override.aes = list(
size=fill.sizes,shape=fill.shapes,color=fill.colors)),breaks = names(my.fills))
X11()
print(plot)
}
plot.fill(data,genome,ori,pointers,region)
Thanks to Marco Sandri, here is a workaround(?) that gets the lines in the desired orientation (starting from "color & shape"). It involves having 3rd aesthetics mapped just for the vertical line.
###### "color & shape" variant
plot.fill <- function(bed,genome,circles=NULL,pointers=NULL,rectangles=NULL) {
require(ggplot2)
my.colors <- c("fit"="gray25","value"="blue")
color.lines <- c("solid","blank")
color.shapes <- c(NA, 20)
color.sizes <- c(1,4)
my.fills <- c("circles"="white","pointers"="orange","rectangles"=NA)
fill.shapes <- c(21,25,22)
fill.sizes <- c(4,4,5)
fill.colors <- c("black","black","red")
plot<-ggplot(bed)
plot<-plot+scale_y_continuous(limits = c(0.8, 2.2)) ## y scale
plot<-plot+scale_x_continuous(limits=c(0,NA)) ## x scale
plot<-plot+facet_grid(chr ~ .,scales = "free", space = "free_x") ## Facet
plot<-plot+geom_segment(aes(x=genome$cen,xend=genome$cen,y=1,yend=2),data=genome,,color="green3") ## Green vlines
plot<-plot+geom_vline(aes(xintercept=-1,linetype="cen"),data=genome) ## Dummy for the legend (makes vlines)
plot <- plot+geom_rect(aes(xmin=start,xmax=end,ymin=0.83,ymax=0.97),data=rectangles,color="red",fill=NA) ## Rectangles
plot<-plot+geom_segment(aes(x=0,xend=end,y=0.9,yend=0.9),data=genome,size=.7) ## Chromosome length line
plot <- plot+geom_point(aes(x=0,y=0.7,fill="rectangles"),data=rectangles,color="red",shape=22,show.legend=T) ## Dummy to fit the rectangle into "fill" legend
plot <- plot+geom_point(aes(x=coord,y=0.9,fill="circles"),data=circles,color="black",size=2,shape=21) ## Circles
plot <- plot+geom_point(aes(x=coord,y=.95,fill="pointers"),data=pointers,size=3,shape=25,stroke=0.5) ## Triangles
plot<-plot+geom_line(aes(x=coord,y=fit,group=group,color="fit")) ## Line
plot<-plot+geom_point(aes(x=coord,y=value,color="value"), size=0.1,alpha=0.5) ## Dots
plot<-plot+scale_colour_manual(values=my.colors,guide = guide_legend(override.aes = list(
linetype = color.lines, shape = color.shapes,size=color.sizes)),breaks=names(my.colors))
plot<-plot+scale_fill_manual(values=my.fills,guide = guide_legend(override.aes = list(
size=fill.sizes,shape=fill.shapes,color=fill.colors)),breaks = names(my.fills))
plot<-plot+scale_linetype_manual(values=c("cen"="solid"),guide = guide_legend(override.aes = list(color="green3",size=1,shape=NA)))
X11()
print(plot)
}
plot.fill(data,genome,ori,pointers,region)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.