【问题标题】:ggplot2 custom legend (vertical and horizontal lines)ggplot2 自定义图例(垂直和水平线)
【发布时间】:2017-06-26 21:02:16
【问题描述】:

我正在尝试在 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)

【问题讨论】:

  • 您的意思是您想要单独的图例,一个用于来自geom_vline 的垂直图例行,一个用于来自geom_line 的水平图例行?如果他们在同一个传说中,他们不是总是十字架吗?
  • 理想情况下,它们都在同一个图例上(如“全色”方法),“cen”是一条垂直线,“fit”是水平线。但如果不可能,我很乐意拥有 2 或 3 个单独的图例,只要它们的线条方向正确。

标签: r ggplot2 legend


【解决方案1】:

感谢 Marco Sandri,这是一种解决方法(?),可以让线条处于所需的方向(从“颜色和形状”开始)。它涉及仅针对垂直线映射第三美学。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-19
    • 2022-01-21
    • 2017-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多