1

I am plotting a continuous variable in X-axis against the the corresponding counts (not the density) in the Y-axis using ggplot2.

This is my code

p <- ggplot(matched.frame, aes(x = AGE, color = as.factor(DRUG_KEY))) + geom_freqpoly(binwidth=5)
p1 <- p + theme_minimal()
plot(p1)

This produces a graph like this this:

enter image description here

I want the areas under these lines to be filled with colors and with little bit of transparency. I know to do this for density plots in ggplot2, but I am stuck with this frequency polygon.

Also, how do I change the legends on the right side? For example, I want 'Cases' instead of 26 and Controls instead of '27'. Instead of as.factor(DRUG_KEY), I want it to appear as 'Colors"

Sample data

matched.frame <- data.frame("AGE"=c(18,19,20,21,22,23,24,25,26,26,27,18,19,20,24,23,23,23,22,30,28,89,30,20,23))
matched.frame$DRUG_KEY <- 26
matched.frame$DRUG_KEY[11:25] <- 27
j1897
  • 1,507
  • 5
  • 21
  • 41
  • Please add reproducible examples to your R questions. This makes things easier. Therefore, you may want to read (1) [how do I ask a good question](http://stackoverflow.com/help/how-to-ask), (2) [How to create a MCVE](http://stackoverflow.com/help/mcve) as well as (3) [how to provide a minimal reproducible example in R](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#answer-5963610). – lukeA May 17 '16 at 07:37

1 Answers1

3

You can use geom_ribbon to fill the area under the curves and scale_fill_discrete (fill color) as well as scale_color_discrete (line color) to change the legend labels:

library(ggplot2)
set.seed(1)
df <- data.frame(x = 1:10, y = runif(20), f = gl(2, 10))
ggplot(df, aes(x=x, ymin=0, ymax=y, fill=f)) + 
  geom_ribbon(, alpha=.5) + 
  scale_fill_discrete(labels = c("1"="foo", "2"="bar"), name = "Labels")

With regards to your edit:

ggplot(matched.frame, aes(x=AGE, fill=as.factor(DRUG_KEY), color=as.factor(DRUG_KEY))) + 
  stat_bin(aes(ymax=..count..,), alpha=.5, ymin=0, geom="ribbon", binwidth =5, position="identity", pad=TRUE) + 
  geom_freqpoly(binwidth=5, size=2) + 
  scale_fill_discrete(labels = c("26"="foo", "27"="bar"), name = "Labels") + 
  scale_color_discrete(labels = c("26"="foo", "27"="bar"), name = "Labels")
lukeA
  • 53,097
  • 5
  • 97
  • 100
  • This is not giving me the intended result. The Y-axis have to be the counts. – j1897 May 17 '16 at 07:44
  • Provide a reproducible example on which one can build upon. `y` are counts (matter of interpretation). – lukeA May 17 '16 at 07:50
  • Added a sample data similar to the one which I have – j1897 May 17 '16 at 07:56
  • Thanks, but why is the left part of the area unshaded? – j1897 May 17 '16 at 10:36
  • 1
    Because the lines start at 0 at either ends of x, the polygons start at the first values. Seems as if freqpoly adds two empty bins; you can let stat_bin mimic that by using `pad=TRUE` - see my update. – lukeA May 17 '16 at 11:43