Creating side by side columns in geom_point plot

35 views
Skip to first unread message

Alastair

unread,
Mar 16, 2011, 8:54:15 AM3/16/11
to ggplot2
Hi,

I'm trying to recreate a plot similar to those shown in the
geom_boxplots examples where the fill factor creates side by side
plots for the same x axis value. Rather than boxplots I'd like to plot
the data as geom_points. In my example I've got 4 problem instances
and for each of those instances I want to plot 6 columns of values.
I've managed to get quite close to what I need using the
position_dodge but I feel like there is probably another way to get
the six distinct measurements to form columns.

Here's my example:

library(ggplot2)
m.data <- structure(list(instance = structure(c(1L, 1L, 1L, 2L, 2L,
2L,
3L, 3L, 3L, 4L, 4L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L,
4L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 1L, 1L,
1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 1L, 1L, 1L, 2L, 2L, 2L,
3L, 3L, 3L, 4L, 4L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L,
4L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 1L, 1L,
1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 1L, 1L, 1L, 2L, 2L, 2L,
3L, 3L, 3L, 4L, 4L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L,
4L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 1L, 1L,
1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L), .Label = c("competition01",
"competition02", "small_1", "small_2"), class = "factor"), partition =
structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1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1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label =
c("VNS",
"VNS-Skip"), class = c("ordered", "factor")), variable =
structure(c(1L,
1L, 1L, 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, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), .Label =
c("event.clashes",
"overlaps", "room.valid", "final.timeslot", "three.in.a.row",
"single.event"), class = "factor"), value = c(533L, 604L, 643L,
546L, 575L, 544L, 510L, 565L, 466L, 1004L, 1431L, 1098L, 551L,
617L, 636L, 627L, 636L, 600L, 563L, 635L, 503L, 1432L, 1324L,
1241L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 315L, 317L, 320L, 316L,
333L, 328L, 96L, 96L, 101L, 116L, 122L, 115L, 315L, 329L, 325L,
328L, 314L, 323L, 92L, 95L, 97L, 109L, 101L, 108L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 259L, 202L, 233L, 180L, 204L, 223L, 244L,
138L, 225L, 418L, 351L, 389L, 255L, 225L, 256L, 236L, 215L, 200L,
238L, 174L, 247L, 422L, 435L, 629L, 87L, 62L, 97L, 77L, 98L,
89L, 67L, 64L, 62L, 88L, 178L, 105L, 84L, 85L, 89L, 113L, 113L,
94L, 63L, 101L, 106L, 134L, 207L, 170L)), .Names = c("instance",
"partition", "option", "variable", "value"), row.names = c(NA,
-144L), class = "data.frame")
plot <- ggplot(data = m.data, aes(instance, value))
plot + geom_point(aes(colour=factor(option), shape =
factor(variable)), position = position_dodge(width = 0.3))

Any pointers or advice anyone could give me would be gratefully
received.
Best regards,
Alastair

Adam_L...@keybank.com

unread,
Mar 16, 2011, 10:46:41 AM3/16/11
to Alastair, ggplot2

Alastair,

I'm no expert, but I had to do something similar to this recently, and something like the plot below helped me compare several factors at once.

ggplot(m.data,aes(instance,value,colour=variable))+
geom_point(aes(shape=variable))+
geom_line(stat="summary",fun.y=median,aes(group=variable))+
facet_grid(.~option)




Adam Loveland




Alastair <alastai...@gmail.com>
Sent by: ggp...@googlegroups.com

03/16/2011 08:54 AM

To
ggplot2 <ggp...@googlegroups.com>
cc
Subject
Creating side by side columns in geom_point plot





--
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




This communication may contain privileged and/or confidential information. It
is intended solely for the use of the addressee. If you are not the intended
recipient, you are strictly prohibited from disclosing, copying, distributing
or using any of this information. If you received this communication in error,
please contact the sender immediately and destroy the material in its entirety,
whether electronic or hard copy. This communication may contain nonpublic personal
information about consumers subject to the restrictions of the 
Gramm-Leach-Bliley Act. You may not directly or indirectly reuse or redisclose
such information for any purpose other than to provide the services for which
you are receiving the information.

127 Public Square, Cleveland, OH 44114



If you prefer not to receive future e-mail offers for products or services from Key
send an e-mail to mailto:DNERe...@key.com with 'No Promotional E-mails' in the
SUBJECT line.

Dennis Murphy

unread,
Mar 16, 2011, 12:30:12 PM3/16/11
to Alastair, ggplot2
How about something completely different? :)

g <- ggplot(m.data, aes(x = value, y = variable))
g + geom_point(aes(colour = option), size = 2.5) + facet_wrap(~ instance, ncol = 1) +
    theme_bw() + opts(legend.position = c(0.88, 0.88))

To reduce some of the overplotting,

g + geom_point(aes(colour = option), size = 2.5, position = position_jitter(height = 0.2)) +
    facet_wrap(~ instance, ncol = 1) +
    theme_bw() + opts(legend.position = c(0.88, 0.88))

HTH,
Dennis

Alastair

unread,
Mar 19, 2011, 10:44:54 AM3/19/11
to ggplot2
Thanks for the answers Adam and Dennis.

I think I prefer Dennis' facet based solution. The only thing is that
I'm trying to plot about 20 instances for my actual data set. It would
look a lot more concise if I could the instance as the x-axis rather
than using that as the facet variable. What I was trying to achieve
would probably quickly become too noisy and hard to interpret. The
figure is for a thesis so there isn't really the same pressure for
space as some users face.

Thanks a lot guys.
Cheers,
Alastair

Dennis Murphy

unread,
Mar 19, 2011, 1:07:49 PM3/19/11
to Alastair, ggplot2
Hi:

Actually, there's another one I like better: (data in the original post not reproduced here)

h <- ggplot(m.data, aes(x = value, y = instance))
h + geom_point(aes(colour = option), size = 2.5, position = position_jitter(height = 0.2)) +
    facet_wrap(~ variable, ncol = 1) +
    theme_bw() + opts(legend.position = c(0.88, 0.75))

This assumes the facets represent different variables, so the comparisons among instances are evident and easy to interpret. If you have a number of variables and space is no issue, then you could make several ggplots that would take up a few pages. One way to do this is to create a new variable in your data frame whose values would map to different graphics pages, and then use successive subsets of the data to plot individual pages. The reason why it's better to have instances on the y-axis is because it's a lot easier to read the labels that way. With 20 instances, you could probably get two, maybe three faceted variables per page.

HTH,
Dennis
Reply all
Reply to author
Forward
0 new messages