conditional fill of points

1,469 views
Skip to first unread message

Matthew

unread,
Sep 23, 2010, 8:51:34 AM9/23/10
to ggplot2
Hi, newbie here. I am trying to produce a scatter plot with a mixture
of open and filled points with the points above a certain value (.9)
on the x-axis are filled. Here's the data:


MCE Duration Probability fillcnd
-47.457 40ms 0.99845999999809 L1
108.402 40ms 0.997699999998094 L1
13.368 40ms 0.980699999998172 L1
6.951 40ms 0.999909999998084 L1
-5.685 40ms 0.999489999998086 L1
24.725 40ms 0.994939999998107 L1
-13.791 40ms 0.999729999998085 L1
28.125 40ms 0.999809999998085 L1
21.885 40ms 0.99845999999809 L1
169.494 40ms 0.998239999998092 L1
73.946 40ms 0.995099999998106 L1
0.975 40ms 0.999419999998086 L1
147.867 40ms 0.999529999998086 L1
61.604 40ms 0.999939999998084 L1
14 40ms 0.933839999998385 L1
8.85 40ms 0.997559999998095 L1
-30.128 40ms 0.163660000000023 L0
4.43 40ms 0.136939999999996 L0
-4.759 40ms 0.000580000000000001 L0
31.626 40ms 0.999979999998084 L1
16.025 40ms 0.677619999999551 L0
64.307 50ms 0.826759999998872 L0
52.189 50ms 0.854059999998748 L0
4.842 50ms 0.664009999999613 L0
15.67 50ms 0.932029999998393 L1
10.316 50ms 0.896049999998557 L0
70.55 50ms 0.760599999999173 L0
179.84 50ms 0.902839999998526 L1
19.41 50ms 0.902459999998528 L1
29.8 50ms 0.840989999998807 L0
-95.482 50ms 0.822339999998892 L0
-38.578 50ms 0.767289999999143 L0
40.768 50ms 0.889369999998587 L0
-29.949 50ms 0.885369999998605 L0
89.436 50ms 0.938289999998365 L1
8.175 50ms 0.3405300000002 L0
28.375 50ms 0.84042999999881 L0
44.127 50ms 2e-04 L0
64.554 50ms 0 L0
-29.966 50ms 0 L0
27.65 50ms 0.964479999998245 L1
10.645 50ms 0.0581700000000071 L0


And here's code that produces the plot I want with all open points:
mce.df<- read.table("df.txt", header=TRUE)
clr<- colors()

ggplot(mce.df, aes(Probability, MCE)) +
labs(x="Posterior Probability") +
geom_line(x=.9, color='darkgrey', size=.5)+
geom_smooth(span=1, se=FALSE, color='darkgrey', size=1.5) +
geom_point(aes(color=Duration, shape=Duration),size=4) +
scale_colour_manual(values=c(clr[174],clr[318])) +
scale_shape_manual(values=c(21,22))

But I would like the points to the right of the vertical line (i.e. >.
9) to be filled. I've tried this (and many variants), but have not
been able to get it to work.

ggplot(mce.df, aes(Probability, MCE)) +
labs(x="Posterior Probability") +
geom_line(x=.9, color='darkgrey', size=.5)+
geom_smooth(span=1, se=FALSE, color='darkgrey', size=1.5) +
geom_point(aes(color=factor(Duration), shape=factor(Duration),
fill=factor(fillcnd)),size=4) +
scale_colour_manual(values=c(clr[174],clr[318])) +
scale_shape_manual(values=c(21,22)) +
scale_fill_manual(values=c(clr[174],clr[318]))


Any help would be greatly appreciated!

Thank you,

Matthew

Brandon Hurr

unread,
Sep 23, 2010, 9:43:18 AM9/23/10
to Matthew, ggplot2
I could be wrong, but I don't think points have a fill.

You've already used your color scale for differentiating between Duration so you can't do it again with fillcnd. 

What about plotting another layer with geom_point(), but only plotting the subset of the data and using filled in point shapes? Although, you've used your point shape scale already too. 

Very puzzling. 

Brandon

P.S. dput()'d data for S&Gs.

mce.df <- structure(list(MCE = c(-47.457, 108.402, 13.368, 6.951, -5.685, 
24.725, -13.791, 28.125, 21.885, 169.494, 73.946, 0.975, 147.867, 
61.604, 14, 8.85, -30.128, 4.43, -4.759, 31.626, 16.025, 64.307, 
52.189, 4.842, 15.67, 10.316, 70.55, 179.84, 19.41, 29.8, -95.482, 
-38.578, 40.768, -29.949, 89.436, 8.175, 28.375, 44.127, 64.554, 
-29.966, 27.65, 10.645), Duration = structure(c(1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L), .Label = c("40ms", "50ms"), class = "factor"), 
    Probability = c(0.99845999999809, 0.99769999999809, 0.98069999999817, 
    0.99990999999808, 0.99948999999809, 0.99493999999811, 0.99972999999809, 
    0.99980999999809, 0.99845999999809, 0.99823999999809, 0.99509999999811, 
    0.99941999999809, 0.99952999999809, 0.99993999999808, 0.93383999999839, 
    0.9975599999981, 0.16366000000002, 0.13694, 0.00058, 0.99997999999808, 
    0.67761999999955, 0.82675999999887, 0.85405999999875, 0.66400999999961, 
    0.93202999999839, 0.89604999999856, 0.76059999999917, 0.90283999999853, 
    0.90245999999853, 0.84098999999881, 0.82233999999889, 0.76728999999914, 
    0.88936999999859, 0.88536999999861, 0.93828999999837, 0.3405300000002, 
    0.84042999999881, 2e-04, 0, 0, 0.96447999999825, 0.05817000000001
    ), fillcnd = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 
    1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 
    1L, 1L, 2L, 1L), .Label = c("L0", "L1"), class = "factor")), .Names = c("MCE", 
"Duration", "Probability", "fillcnd"), class = "data.frame", row.names = c(NA, 
-42L))
 




--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Dennis Murphy

unread,
Sep 23, 2010, 10:31:38 AM9/23/10
to Matthew, ggplot2
Hi:

After toying around for a while and getting nowhere, it suddenly dawned on me that R has open and filled plotting characters...duh :)  With that brilliant inspiration in mind, try this:

clr<- colors()
mce.df <- transform(mce.df, isfill = ifelse(Probability <= 0.9, 0, 1))

# This factor is the interaction of Duration and I(Probability > 0.9)
mce.df$durafill <- with(mce.df, ifelse(Duration == '40ms' & isfill == 1,
                       '40ms +', ifelse(Duration == '40ms' & isfill == 0,
                       '40ms -', ifelse(Duration == '50ms' & isfill == 1,
                       '50ms +', '50ms -'))))

ggplot(mce.df, aes(Probability, MCE)) +
  labs(x="Posterior Probability") +
  geom_line(x=.9, color='darkgrey', size=.5)+
  geom_smooth(span=1, se=FALSE, color='darkgrey', size=1.5) +
  geom_point(aes(colour = Duration, shape= durafill),size=4) +
  scale_colour_manual('Duration', values=c(clr[174], clr[318])) +
  scale_shape_manual('', values=c(1, 16, 2, 17))

Unfortunately, you kind of need both scales - the top one indicates fill color while the bottom one deals with shape/fill interactions. I couldn't think of a decent title for the bottom legend so I left it blank. If you want to get rid of one of the legends, type    legend = FALSE    after the values argument. If you have Hadley's book, see Figure B.1 on p. 197 for the various plotting characters and their corresponding numerical codes.

It's not perfect, but it's progress...

Dennis

Matthew

unread,
Sep 23, 2010, 10:52:03 AM9/23/10
to ggplot2
Thank you very much Dennis, that works! I was certainly going about
it in the wrong way.

Cheers,

Matthew
> > To unsubscribe: email ggplot2+u...@googlegroups.com<ggplot2%2Bunsu...@googlegroups.com >
> > More options:http://groups.google.com/group/ggplot2

Ista Zahn

unread,
Sep 23, 2010, 12:21:44 PM9/23/10
to Matthew, ggplot2
Hi all,
According to the documentation, geom_point does have a fill aesthetic.
The problem seems to be that it does not interact well with
scale_shape:

test.df <- expand.grid(S=c("a", "b"), C=c("c", "d"), F=c("f", "g"))
test.df$X <- 1:8
test.df$Y <- 1:8
p <- ggplot(test.df, aes(x=X, y=Y))
p + geom_point(aes(fill=F)) # does not work because points are solid,
cannot see the fill color
p + geom_point(aes(fill=F), shape=21) # works
p + geom_point(aes(fill=F, color=C), shape=21) # works
p + geom_point(aes(fill=F, shape=S)) # does not work Error in if
(!is.null(data$fill) && !all(is.na(data$fill)) && data$shape == :
missing value where TRUE/FALSE needed. Bug?

Session info in case Hadley agrees that this is a bug:
sessionInfo()
R version 2.11.1 (2010-05-31)
i686-pc-linux-gnu

locale:
[1] LC_CTYPE=en_US.ISO-8859-1 LC_NUMERIC=C
[3] LC_TIME=en_US.ISO-8859-1 LC_COLLATE=en_US.ISO-8859-1
[5] LC_MONETARY=C LC_MESSAGES=en_US.ISO-8859-1
[7] LC_PAPER=en_US.ISO-8859-1 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.ISO-8859-1 LC_IDENTIFICATION=C

attached base packages:
[1] grid stats graphics grDevices utils datasets methods
[8] base

other attached packages:
[1] ggplot2_0.8.8 proto_0.3-8 reshape_0.8.3 plyr_1.2.1

loaded via a namespace (and not attached):
[1] digest_0.4.2 tools_2.11.1

Best,
Ista

> More options: http://groups.google.com/group/ggplot2
>

--
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org

Brandon Hurr

unread,
Sep 23, 2010, 12:38:00 PM9/23/10
to Ista Zahn, Matthew, ggplot2
Ista, 

Thanks for the info on point fill. I can verify the same result on my system. 

> p + geom_point(aes(fill=F, shape=S))
Error in if (!is.null(data$fill) && !all(is.na(data$fill)) && data$shape ==  : 
  missing value where TRUE/FALSE needed
> sessionInfo()
R version 2.11.1 (2010-05-31) 
x86_64-apple-darwin9.8.0 

locale:
[1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_0.8.8 reshape_0.8.3 plyr_1.2.1    proto_0.3-8  

loaded via a namespace (and not attached):
[1] digest_0.4.2


Brandon
Reply all
Reply to author
Forward
0 new messages