controlling dot colors in a scatter plot

959 views
Skip to first unread message

Data Analytics Corp.

unread,
Mar 29, 2012, 9:28:15 AM3/29/12
to ggp...@googlegroups.com
Hi,

I created a simple data frame of sales and price for a fictitious
product to use in a larger example for a client. This made-up product
has a promotion flyer which is just a binary 0/1 variable (1 = flyer
promotion, 0 = no flyer promotion). The following code creates a
scatter to show the relationship between sales and price with the flyer
promotion indicate:

p <- ggplot(data = x, aes(x = own.price, y = sales, colour = flyer))
p <- p + geom_point(size = 2)
p <- p + scale_colour_hue("Flyer\nPromotion")
p <- p + scale_x_continuous("Price per Unit ($)", labels = dollar) +
scale_y_continuous("Unit Sales (000)")
p <- p + opts(title = "Beverage Product Study\nPrice and Sales Data\nRaw
Data\nFlyer Promotion Effect")
p

My question is: how can I control the color of the dots? I want one to
be black and the other red. I'm using the latest version of ggplot2.

Thanks,

Walt

________________________

Walter R. Paczkowski, Ph.D.
Data Analytics Corp.
44 Hamilton Lane
Plainsboro, NJ 08536
________________________
(V) 609-936-8999
(F) 609-936-3733
wa...@dataanalyticscorp.com
www.dataanalyticscorp.com
_____________________________________________________

Roman Luštrik

unread,
Mar 29, 2012, 9:29:23 AM3/29/12
to wa...@dataanalyticscorp.com, ggp...@googlegroups.com
For some top notch colors see  http://had.co.nz/ggplot2/scale_brewer.html (among others).


Cheers,
Roman





--
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+unsubscribe@googlegroups.com
More options: http://groups.google.com/group/ggplot2



--
In God we trust, all others bring data.

Jean-Olivier Irisson

unread,
Mar 29, 2012, 9:31:24 AM3/29/12
to wa...@dataanalyticscorp.com, ggp...@googlegroups.com
On 2012-Mar-29, at 15:28 , Data Analytics Corp. wrote:
>
> My question is: how can I control the color of the dots? I want one to be black and the other red. I'm using the latest version of ggplot2.

scale_colour_manual(values=c("red", "black"))

Jean-Olivier Irisson
---
Observatoire Océanologique
Station Zoologique, B.P. 28, Chemin du Lazaret
06230 Villefranche-sur-Mer
Tel: +33 04 93 76 38 04
Mob: +33 06 21 05 19 90
http://jo.irisson.com/

Roman Luštrik

unread,
Mar 29, 2012, 9:39:49 AM3/29/12
to wa...@dataanalyticscorp.com, ggp...@googlegroups.com
Doh, too quick to jump the gun. Of course the answer is as provided by Jean-Olivier with scale_colour.

Cheers,
Roman

Data Analytics Corp.

unread,
Mar 29, 2012, 10:00:28 AM3/29/12
to ggp...@googlegroups.com

Thanks to all who responded. This worked perfectly. But now I have
another simple question. I added a facet using

p <- p + facet_grid(. ~ flyer)

to get two panels: one for data with a flyer promotion, the other
without a flyer promotion. How do I get one panel to have red dots and
the other black dots?

Thanks,

Walt


On 3/29/2012 9:31 AM, Jean-Olivier Irisson wrote:
> On 2012-Mar-29, at 15:28 , Data Analytics Corp. wrote:
>> My question is: how can I control the color of the dots? I want one to be black and the other red. I'm using the latest version of ggplot2.
> scale_colour_manual(values=c("red", "black"))
>
> Jean-Olivier Irisson
> ---

> Observatoire Oc�anologique

Dennis Murphy

unread,
Mar 29, 2012, 11:24:59 AM3/29/12
to wa...@dataanalyticscorp.com, ggp...@googlegroups.com
Hi:

It's not clear whether your binary variable is factor, character or
numeric, and it matters in terms of the specification given below.
Assuming it's numeric,

scale_color_manual('Flyer\nPromotion', values = c(0 = 'black', 1 = 'red'),
labels = c('No flyer', 'Flyer'))

If the variable is a factor, then you need something like

scale_color_manual('Flyer\,Promotion', breaks = levels(x$flyer),
values = c('black', 'red'),
labels = c('No flyer', 'Flyer'))

If it's character, then quote the 0 and 1 in the first statement.

Dennis

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

Reply all
Reply to author
Forward
0 new messages