gplot geom_bar不显示所有绘制的数据框子集

cig3rfwq  于 2023-04-27  发布在  其他
关注(0)|答案(1)|浏览(112)

我试图创建一个gggplot条形图,其中x轴为基因组起始位置,y轴为高度值。
我有一个3000行的表,我试图绘制它的子集,如下所示:

ggplot(data = chr1[1:4,c(1:3,9,14)]) +
    geom_bar(aes(x= start,y=V7), stat="identity")

table的这一部分看起来是这样的

dput(chr1[1:4,c(1:3,9,14)])
structure(list(seqnames = c("chr1", "chr1", "chr1", "chr1"), 
    start = c(858042L, 858076L, 5955119L, 5955150L), end = c(858192L, 
    858155L, 5955238L, 5955257L), V7 = c(22.74576, 8.52652, 10.04841, 
    7.32666), which = c("sorted", "rep3", "rep3", "sorted")), row.names = c(1707L, 
1L, 2L, 1708L), class = "data.frame")
seqnames   start     end       V7  which
1707     chr1  858042  858192 22.74576 sorted
1        chr1  858076  858155  8.52652   rep3
2        chr1 5955119 5955238 10.04841   rep3
1708     chr1 5955150 5955257  7.32666 sorted

在这种情况下,我会得到一个空图,图显示在我的设备上,但我没有得到任何条。
如果我试着画出第一个子集

ggplot(data = chr1[1:2,]) +
    geom_bar(aes(x= start,y=V7), stat="identity")

我能正常接收信号。
如果我使用填充或颜色我得到所有的酒吧以及你可以看到在下面的下一个问题。
为什么会这样呢?
另外,我想绘制所有的条形图,不使用factor(start),使用透明的alpha填充(column which),这样当它们非常接近时,我就可以看到它们。如果x轴上没有factor,alpha因子似乎不会被应用。
比如这个

ggplot(chr1[1:4,], aes(x=start, y=V7, fill=which, color=which)) +
  geom_bar(stat="identity",alpha=0.2) + theme_minimal()

给我的酒吧,但没有透明度应用
有什么变通办法吗?
非常感谢!

q3qa4bjr

q3qa4bjr1#

library(tidyverse)
library(patchwork)

为了说明将start作为数字的问题。

example_df <- data.frame(start = seq(1, 30, 1)^3,
                         V7 = runif(10, 5, 25))

p1 <- ggplot(example_df[1:5, ], aes(x = start, y = V7)) + 
  geom_col() +
  ggtitle("First 5")

p2 <- ggplot(example_df[1:20, ], aes(x = start, y = V7)) + 
  geom_col() +
  ggtitle("First 20")

p3 <- ggplot(example_df, aes(x = start, y = V7)) + 
  geom_col() +
  ggtitle("All")

p4 <- ggplot(example_df, aes(x = factor(start), y = V7)) +
  geom_col() +
  theme(axis.text.x = element_text(angle = 45)) +
  ggtitle("As factor rather than numeric")

p1 + p2 + p3

p4

如果你把start变成一个因子,它们将有相似的间距。

然而,如果两条线之间的距离很重要,则需要调整width参数,但这样做可能会使两条线相互重叠。

ggplot(example_df, aes(x = start, y = V7)) + 
  geom_col(width = 200)
#> Warning: `position_stack()` requires non-overlapping x intervals

创建于2023-04-21使用reprex v2.0.2

相关问题