如何在ggplot中绘制高于零和低于零的值的密度?

时间:2019-03-11 21:30:46

标签: r ggplot2 plot

我有这两个变量。

test_pos <- c(25L, 29L, 142L, 142L, 163L, 163L, 164L, 164L, 164L, 164L, 165L, 
165L, 168L, 170L, 170L, 237L, 237L, 237L, 237L, 237L, 238L, 238L, 
244L, 244L, 244L, 244L, 244L, 244L, 244L, 247L, 248L, 248L, 248L, 
248L, 248L, 250L, 250L, 250L, 250L, 250L, 250L, 252L, 252L, 254L, 
289L, 289L, 289L, 299L, 302L, 314L, 354L, 373L, 373L, 373L, 373L, 
396L, 396L, 396L, 396L, 396L, 396L, 396L, 396L, 396L, 396L, 396L, 
396L, 396L, 396L, 396L, 491L, 493L, 493L, 499L, 536L, 552L, 563L, 
568L, 607L, 669L, 791L, 791L, 793L, 834L, 845L, 849L, 856L, 856L, 
884L, 884L, 992L, 992L, 995L, 995L, 995L, 998L, 1005L, 1005L, 
1064L, 1104L)

test_neg <- c(1100L, 1100L, 1272L, 1841L, 1965L, 1980L, 1980L, 2724L, 2744L, 
2744L, 2744L, 2748L, 2907L, 2918L, 2918L, 2919L, 2920L, 2921L, 
3050L, 3062L, 3065L, 3065L, 3077L, 3088L, 3088L, 3088L, 3088L, 
3089L, 3089L, 3089L, 3089L, 3090L, 3105L, 3141L, 3182L, 3212L, 
3212L, 3219L, 3219L, 3219L, 3220L, 3223L, 3223L, 3223L, 3224L, 
3225L, 3225L, 3226L, 3227L, 3370L, 3394L, 3396L, 3398L, 3402L, 
3403L, 3447L, 3456L, 3470L, 3522L, 3523L, 3524L, 3524L, 3524L, 
3525L, 3607L, 3607L, 3607L, 3607L, 3618L, 3624L, 3624L, 3624L, 
3629L, 3638L, 3638L, 3639L, 3639L, 3639L, 3639L, 3639L, 3641L, 
3641L, 3641L, 3641L, 3641L, 3641L, 3641L, 3641L, 3642L, 3642L, 
3642L, 3642L, 3642L, 3642L, 3642L, 3647L, 3647L, 3647L, 3647L, 
3647L)

我想使用ggplot而不是R中的基本图来绘制密度图(如下所示)。如何在ggplot中模拟?

#calculate the densities
chr22_neg_density <- density(test_neg)
chr22_pos_density <- density(test_pos)

#display the negative strand with negative values
chr22_neg_density$y <- chr22_neg_density$y * -1

plot(chr22_pos_density,
     ylim = range(c(chr22_neg_density$y, chr22_pos_density$y)),
     main = "Coverage plot of mapped CAGE reads",
     xlab = "Chromosome 22",
     col = 'blue',
     lwd=2.5)
lines(chr22_neg_density, lwd=2.5, col = 'red')

## histogram
plot(chr22_pos_density,
     ylim = range(c(chr22_neg_density$y, chr22_pos_density$y)),
     main = "Coverage plot reads",
     xlab = "Chromosome 14",
     col = 'blue',
     type='h'
)

lines(chr22_neg_density, type='h', col = 'red')

1 个答案:

答案 0 :(得分:6)

使用stat函数访问计算出的统计数据,然后重新映射它们:

ggplot() +
  geom_density(aes(x), data.frame(x = test_pos), fill = 'navy') +
  geom_density(aes(x, -stat(density)), data.frame(x = test_neg), fill = 'firebrick')

enter image description here