geom_freqpoly shade quartiles

158 views
Skip to first unread message

Hkesh22

unread,
Apr 7, 2017, 12:53:28 PM4/7/17
to ggplot2
Hi all,

I am fairly new to R. I am plotting a distribution using ggplot2, geom_freqpoly. I can add lines that show 0-25%, 25-50%, 50-75%, and 75-100% of the population, but I'd like to shade each quartile in different colours. I've attached a sample data set.

Here is my code:

library(ggplot2)

CD25 <- read.csv(file = "CD25 distribution Tregs.csv")

summary(CD25)


ggplot(CD25)+
  geom_freqpoly(aes(x=CD25.MFI, y =(..ncount..)),colour = "black",  binwidth = 0.01)+
  scale_x_log10()+geom_vline(xintercept = 52.718,col="grey",lwd = 0.5)+
  geom_vline(xintercept = 101.151,col="grey",lwd = 0.5)+
  geom_vline(xintercept = 165.561,col="grey",lwd = 0.5)+
  theme_bw()+theme(axis.ticks.length= unit(0.3,"cm"),panel.grid.major = element_blank (),panel.grid.minor = element_blank ())

Thanks for any help you can provide,

Hari
CD25 distribution.pdf
CD25 distribution Tregs.csv
Message has been deleted

eipi10

unread,
Jun 13, 2017, 1:52:28 AM6/13/17
to ggplot2

For some reason, the site wouldn't allow me to place the final plot below the code, so the output is above. The code below uses the cut function to mark the quartiles. The interpolation is to ensure that there aren't any gaps in the color fill between quartiles.


p
= ggplot(CD25)+
  geom_freqpoly
(aes(x=CD25.MFI, y =(..ncount..)), colour = "black",  binwidth = 0.01)+

  scale_x_log10
()+
  geom_vline
(xintercept = 52.718,col="grey",lwd = 0.5)+
  geom_vline
(xintercept = 101.151,col="grey",lwd = 0.5)+
  geom_vline
(xintercept = 165.561,col="grey",lwd = 0.5)+
  theme_bw
()+theme(axis.ticks.length= unit(0.3,"cm"),
                   panel
.grid.major = element_blank (),
                   panel
.grid.minor = element_blank ())

# Extract plot data
pb
= ggplot_build(p)
d
= pb$data[[1]][c("x","ncount","count")]

# Get quantile breaks
breaks
= quantile(rep(d$x, d$count))

# Interpolate x and y values
xvals
= seq(min(d$x), max(d$x),length=1000)
d
= as.data.frame(approx(d$x, d$ncount, xout=xvals))

# Add grouping variable by quartile
d$quart
= cut(d$x, c(-Inf, breaks[2:4], Inf))

# Fill plot by quartile
p
+ geom_area(data=d, aes(10^x, y, fill=quart), alpha=0.3, show.legend=FALSE)



eipi10

unread,
Jun 13, 2017, 2:02:03 AM6/13/17
to ggplot2
If you still want the vertical lines, you can do with a single call to geom_vline and you don't need to hard-code the xintercept values. For example p + geom_vline(xintercept=10^breaks[2:4], col="grey", lwd=0.5).
Reply all
Reply to author
Forward
0 new messages