How to plot density of values above and below zero in ggplot?

MAPK

I have these two variables.

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)

I want to plot a density plot using ggplot instead of base plot in R (as below). How do I emulate this in 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')
Axeman

Use the stat function to access calculated statistics, and re-map them:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Plot legend below the graphs and legend title above the legend in ggplot2

R plot density ggplot vs plot

How to find values below (or above) average

How do I change the colors on Y-Axis for values above, below zero? Chart.js 2.6

Colour area above diagonal in the plot as red and below as green in R with ggplot

Density plot for multiple groups in ggplot

Passing a function to the ggplot density plot

ggplot - mapping values below and above a threshold in different colour ranges

How to make ggplot2 exclude zero values in this plot?

How to fill density plot within an interval with ggplot?

How to fill the null values with the average of the above value and the below value in python?

annotate ggplot above plot

Density plot in R - Histogram - ggplot

how to overlay a line plot with a density plot? (R, ggplot2)

ggplot: density plot error

How to plot near zero values with qgraph?

ggplot2: how to add sample numbers to density plot?

How to plot a density map between two lines with ggplot2?

How to autofill values in excel based on the cells above and below

How to threshold values in python without if statement (to zero if below threshold, same if above)

How to get Key of Value that is greater than three Values above and below

How can I add a legend that counts points above or below a certain value in ggplot2? Volcano Plot

Pandas, efficient way to get a rows below and and above zero values

How to hash a string into 4 digits with values above 1000 and below 9999?

R : How to get values related to last peak of density plot?

Plot that shows % of values above a threshold - R / ggplot

How to sum rows from a CSV only when not all values are above zero and not all below zero?

Numpy Set Values In Range Above And Below Zero To Zero

how to make pandas row value to zero when row above values are zeros and below value not equal to zero using python pandas