2017-06-26 60 views
1

Rで同じプロットの異なる種類のデータをプロットする関数を作成しようとしています。凡例は私の白い鯨です。私は2つ(私が欲しいものに最も近い)異なるアプローチを持っており、どちらも私に望んでいる結果を与えません - 凡例のシンボルには垂直線と水平線があります。代わりに、彼らは十字架を生みます。どんな助けでも大歓迎です! ここにはRData fileへのリンクがあります。そして、それらが機能をストリップダウンされています(?)マルコSandriへggplot2カスタム凡例(垂直線と水平線)

###### "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) 

Example plot

+0

あなたは別の伝説、 'geom_vline'と' geom_line'から水平伝説ラインのための1からの垂直伝説ラインのための1をしたいわけですか?彼らが同じ伝説にいるならば、必ずしも必ずしも十字架ではないでしょうか? – eipi10

+0

理想的には、それらはすべて「cen」が垂直線で「フィット」が水平である(「すべての色」アプローチの場合と同じ)凡例になります。しかし、それが不可能な場合は、線の向きが正しい限り、2つまたは3つの別々の凡例を持つことができれば幸いです。 – Dzmitry

答えて

0

おかげで、ここでの回避策(「色&形状」から始まる)所望の向きに行を取得しています。これは、垂直線のためにマッピングされた第3の美学を有することを含む。

###### "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) 
関連する問題