Question about ggplot2: Changing fill colors with geom_area

2,081 views
Skip to first unread message

John Ramey

unread,
Sep 22, 2010, 5:31:18 PM9/22/10
to manipulatr
Hello, everyone.

I am trying to update the graphics in my class notes with ggplot2.
Here I am displaying two continuous uniform distributions to show that
they have equal length and width. The two distributions are
Uniform(0,2) and Uniform(3,5).

The plots look good, but I do not know how to change the fill colors.
I have been unsuccessful with a number of scale_fill_* variants. Any
thoughts?

Here is my code:

a1 <- 0
b1 <- 2
a2 <- 3
b2 <- 5

x1 <- seq(a1, b1, by = 0.01)
x2 <- seq(a2, b2, by = 0.01)

y1 <- dunif(x1, a1, b1)
y2 <- dunif(x2, a2, b2)

df1 <- cbind.data.frame(x = x1, y = y1, Distribution = "Uniform(0,2)")
df2 <- cbind.data.frame(x = x2, y = y2, Distribution = "Uniform(3,5)")

df <- rbind(df1, df2)
df$Distribution <- factor(df$Distribution)

unif.loc.ggplot <- ggplot(df, aes(x = x, y = y, group = Distribution))
unif.loc.ggplot <- unif.loc.ggplot + geom_area(aes(fill =
Distribution))
unif.loc.ggplot <- unif.loc.ggplot + ylim(0.0, 1.0)
unif.loc.ggplot


Thanks in advance for any advice you can give.

Dennis Murphy

unread,
Sep 22, 2010, 10:41:27 PM9/22/10
to John Ramey, manipulatr
Hi:

Try this:

unif <- ggplot(df, aes(x = x, y = y, group = Distribution, fill = Distribution))
unif + geom_area() + ylim(0.0, 1.0) +
          scale_fill_manual(values = c('red', 'blue'))

HTH,
Dennis


--
You received this message because you are subscribed to the Google Groups "manipulatr" group.
To post to this group, send email to manip...@googlegroups.com.
To unsubscribe from this group, send email to manipulatr+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/manipulatr?hl=en.


Martin....@sanofi-aventis.com

unread,
Sep 23, 2010, 7:34:57 AM9/23/10
to djm...@gmail.com, john...@gmail.com, manip...@googlegroups.com
Hi,
I played around with the piece of code and got some weird behavior: (graphical output attached as pdf)

> library(ggplot2)

> df1<-cbind.data.frame(x=c(0,2),y=c(1/2,1/2),Distribution="Uniform(0,2)")

> df2<-cbind.data.frame(x=c(2,5),y=c(1/3,1/3),Distribution="Uniform(2,5)")

> df <-rbind(df1,df2)

> df$Distribution<-factor(df$Distribution)

> df

x y Distribution

1 0 0.5000000 Uniform(0,2)

2 2 0.5000000 Uniform(0,2)

3 2 0.3333333 Uniform(2,5)

4 5 0.3333333 Uniform(2,5)

> print(ggplot(df,aes(x=x,y=y,group=Distribution))

+ +geom_area(aes(fill=Distribution))+ylim(0,1))

> df$x[3]=df$x[3]+0.00000001

> df

x y Distribution

1 0 0.5000000 Uniform(0,2)

2 2 0.5000000 Uniform(0,2)

3 2 0.3333333 Uniform(2,5)

4 5 0.3333333 Uniform(2,5)

> print(ggplot(df,aes(x=x,y=y,group=Distribution))

+ +geom_area(aes(fill=Distribution))+ylim(0,1))



From: manip...@googlegroups.com [mailto:manip...@googlegroups.com] On Behalf Of Dennis Murphy
Sent: Thursday, September 23, 2010 4:41 AM
To: John Ramey
Cc: manipulatr
Subject: Re: Question about ggplot2: Changing fill colors with geom_area

Dennis Murphy

unread,
Sep 23, 2010, 8:24:13 AM9/23/10
to Martin....@sanofi-aventis.com, john...@gmail.com, manip...@googlegroups.com
Hi:

> tail(df)
       x         y Distribution
198 1.97 0.5000000 Uniform(0,2)
199 1.98 0.5000000 Uniform(0,2)
200 1.99 0.5000000 Uniform(0,2)
201 2.00 0.5000000 Uniform(0,2)
202 2.00 0.3333333 Uniform(2,5)
203 5.00 0.3333333 Uniform(2,5)

You have two y's defined at x = 2.00. Add them up and the problem becomes clear.

HTH,
Dennis

Martin....@sanofi-aventis.com

unread,
Sep 23, 2010, 7:40:27 AM9/23/10
to Martin....@sanofi-aventis.com, djm...@gmail.com, john...@gmail.com, manip...@googlegroups.com

(Sorry for the fragmented post.)

The right polygon appears to depend on the whether there are consecutive identical x values or not.
Could some expert please explain?

 Best regards

Dr. Martin Burschka

Sanofi-Aventis Deutschland GmbH
Industriepark Hoechst
Bldg. H831, Room B.0436
D-65926 Frankfurt am Main
w:www.sanofi-aventis.de

*********************************************************************************************************************************************************************
Sanofi-Aventis Deutschland GmbH ·  Sitz der Gesellschaft: Frankfurt am Main · Handelsregister: Frankfurt am Main, Abt. B Nr. 40661
Vorsitzender des Aufsichtsrats: Hanspeter Spek - Geschäftsführer: Dr. Martin Siewert (Vorsitzender), Ulf Bialojahn, Dr. Matthias Braun,
Peter Guenter, Prof. Dr. Dr. Werner Kramer, Dr. Klaus Menken, Dr. Heinz Riederer
*********************************************************************************************************************************************************************



From: Burschka, Martin R&D/DE
Sent: Thursday, September 23, 2010 1:35 PM
To: 'Dennis Murphy'; John Ramey
Cc: manipulatr
Subject: RE: Question about ggplot2: Changing fill colors with geom_area

WeirdExample.pdf
Reply all
Reply to author
Forward
0 new messages