ggplot2自定义图例(垂直和水平线)

时间:2017-06-26 21:02:17

标签: r ggplot2 legend

我正在尝试在R中创建一个函数,在同一个图上绘制不同类型的数据,而图例是我的白鲸。我有两个(最接近我想要的)不同的方法,它们都没有给我我希望的结果 - 图例符号中的垂直和水平线。相反,他们产生十字架。任何帮助将非常感谢! 这是RData file的链接。那些被剥夺了功能:

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

1 个答案:

答案 0 :(得分:0)

感谢Marco Sandri,这是一个解决方法(?),可以获得所需方向的线条(从&#34;颜色和形状&#34;开始)。它涉及仅为垂直线绘制第三美学。

######  "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)
相关问题