ggplot2 geom_area产生意外输出

时间:2017-08-10 23:47:48

标签: r ggplot2

在尝试解决我的question yesterday时,我想到了对数据进行争论,以便可以正确地绘制数据点而无需求助于geom_step,而wrangleBdg<-function(dfInput){ starts<-subset(dfInput,select=c("start","value")) ends<-subset(dfInput,select=c("end","value")) colnames(ends) <- c("start", "value") dataWrite <- rbind(data.frame(starts, index = 1:nrow(starts)), data.frame(ends, index = 1:nrow(ends))) dataWrite <- dataWrite[order(dataWrite$index), c("start", "value")] dataPlot<-ggplot(dataWrite,aes(x=start,y=value))+geom_area() + xlim(chromstart, chromend) return(dataPlot) } p3<-wrangleBdg(df) print(p3) 不会填充床铺图下的区域。

我生成了以下代码,该代码应生成阴影bedgraph图,类似于output produced by the plotBedgraph function

geom_area

然而,它会产生如下所示的乱码输出。请注意高于2的值,其中数据的最大高度为1.377:

enter image description here

geom_line功能更改为 chrom start end value chr13 0 882 0.00000 chr13 882 885 0.02968 chr13 885 886 0.00000 chr13 886 887 0.02968 chr13 887 888 0.00000 chr13 888 890 0.02968 chr13 890 892 0.00000 chr13 892 894 0.09667 chr13 894 908 0.00000 chr13 908 909 0.02968 chr13 909 923 0.00000 chr13 923 926 0.02968 chr13 926 927 0.09667 chr13 927 928 0.02968 chr13 928 933 0.09667 chr13 933 42032 0.00000 chr13 42032 42035 0.52301 chr13 42035 42056 0.45043 chr13 42056 42059 0.52301 chr13 42059 42063 0.45043 chr13 42063 42065 0.38020 chr13 42065 42075 0.29973 chr13 42075 42078 0.15975 chr13 42078 42079 0.23307 chr13 42079 42080 0.29973 chr13 42080 42086 0.38020 chr13 42086 42087 1.28112 chr13 42087 42114 1.18640 chr13 42114 42116 1.28112 chr13 42116 42123 1.37702 chr13 42123 42127 1.28112 chr13 42127 42128 1.18640 chr13 42128 42139 1.09943 chr13 42139 42142 1.07272 chr13 42142 42144 1.09943 chr13 42144 42147 1.18640 chr13 42147 42148 0.29973 chr13 42148 42150 1.18640 chr13 42150 42152 1.28112 chr13 42152 42162 0.38020 chr13 42162 42167 0.29973 chr13 42167 42168 0.23307 chr13 42168 42171 0.29973 chr13 42171 42173 0.38020 chr13 42173 42181 0.29973 chr13 42181 42186 0.45043 chr13 42186 42187 0.52301 chr13 42187 43000 0.00000 会删除乱码,但我们会回到正方形,并且没有阴影。

enter image description here

用于生成绘图的数据如下所示:

Private Sub Worksheet_Change(ByVal Target As Range)

    If Cells(1, 14).Value <> "" Then
        CommandButton2.Visible = True
    Else
        CommandButton2.Visible = False
    End If
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
End Sub

1 个答案:

答案 0 :(得分:2)

尝试将position = "identity"添加到geom_area命令:

#original:
dataPlot<-ggplot(dataWrite,aes(x=start,y=value))+geom_area() + xlim(chromstart, chromend)

#modified version:
dataPlot<-ggplot(dataWrite,aes(x=start,y=value))+geom_area(position = "identity") + xlim(chromstart, chromend)

<强>解释

看看你的dataWrite数据帧。除了第一个&amp;最后一行,在起始列中每个数字的值列中有2个不同的数字。

> head(dataWrite %>% arrange(start))
  start   value
1     0 0.00000
2   882 0.00000
3   882 0.02968
4   885 0.02968
5   885 0.00000
6   886 0.00000

因此,当您使用geom_area绘制x = start, y = value时,其默认位置调整会将一个值叠加在另一个值之上。在原始图(2.658)中观察到的最大值是两个值在start = 42116(1.281和1.377)加在一起的结果。

相关问题