【问题标题】:ggplot2 geom_area producing unexpected outputggplot2 geom_area 产生意外输出
【发布时间】:2017-08-10 23:47:48
【问题描述】:

在尝试解决我的question yesterday 时,我想到了整理数据,以便可以正确绘制数据点,而无需诉诸geom_step,它不会填充床位图下方的区域。

我生成了以下代码,它应该会生成一个阴影床位图,类似于output produced by the plotBedgraph function

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)

但是,它会产生如下所示的乱码输出。注意上面2的值,其中数据的最大高度是1.377:

geom_area 函数更改为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

【问题讨论】:

  • 看看你的数据,这个图表很有意义......你有一些介于 882 和 933 之间的非零数据,然后是介于 933 和 42032 之间的 0.0000,然后是一些从 42032 到 42187 的非零数据。这就是我所看到的:两个尖峰。您能否说明您希望看到什么样的输出?
  • @nicolaskruchten 第二个图的填充版本。我不确定图表如何“有意义”,因为线条到处都是。
  • 明白了。我怀疑尖峰太细了,以至于 geom_area 由于某种原因导致了一些图形伪影。
  • 你应该澄清你的实际问题是什么......你想知道为什么 geom_area 不起作用或......?
  • @nicolaskruchten 那和修复它的方法。

标签: r ggplot2


【解决方案1】:

尝试将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 数据框。除了第一行和最后一行,对于起始列中的每个数字,值列中都有 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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-02
    • 2021-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-07
    • 2021-04-09
    相关资源
    最近更新 更多