[R] Help with a scatter plot
Dennis Murphy
djmuser at gmail.com
Wed Oct 26 11:38:19 CEST 2011
Hi:
The sort of thing you appear to want is fairly straightforward to in
lattice and ggplot2, as both have ways to automate conditioning plots.
Since you were looking at ggpot2, let's consider that problem. You
don't really show enough data to provide a useful demonstration, but
let's see if we can capture the essential structure.
> I want to create a plot where the colors of the hits represent the Product
> (A,B,C..), the character represents the color (X for yellow, box for green,
> etc..), the X axis is the price and the Y axis is the number (0-5) from the
> different Stores (A,B,C,D). I've thought either to create a matrix of 4
> plots ( for the 4 stores) or in some creative way combine them into one
> plot?
The first step is to melt the data so that Store becomes a factor
variable and its corresponding values are assigned to another
variable. To that end, one can invoke the very useful melt() function
in the reshape2 package:
library('reshape2')
mdata <- melt(mydata, id = c('Product', 'Color', 'Price'))
This creates a new data frame with variables Product, Color, Price,
variable and value. variable contains StoreA, ... StoreD as factor
levels and value is a numeric variable consisting of the corresponding
values. For its structure, see
str(mdata)
If you want to change StoreA - StoreD to A - D, say, then you could
optionally do
mdata$variable <- factor(mdata$variable, labels = LETTERS[1:4])
Assuming that you've done enough reading to understand what aesthetics
are about, the problem is essentially this:
x = Price
y = value
shape = Color
faceting variable = variable (the stores)
So a template of a ggplot2 graph for the melted data might look something like
library('ggplot2')
ggplot(mdata, aes(x = Price, y = value, shape = Color)) +
geom_point() +
facet_wrap( ~ variable, ncol = 2) +
scale_shape_manual('Color', breaks = levels(mdata$Color),
values = c(4, 0, 2, 8),
labels = c('Blue',
'Green', 'Red', 'Yellow'))
This assigns x, box, triangle and asterisk as shapes via their
numeric codes (see Hadley Wickham's ggplot2 book, p. 197 for the
reference). The labels = argument lets you change the letters B, G, R,
Y (which would comprise the default labels) to something more
evocative.
If for some odd reason you wanted to add corresponding colors to the
shapes, you could also do that, as follows:
ggplot(mdata, aes(x = Price, y = value, shape = Color, colour = Color)) +
geom_point() +
facet_wrap( ~ variable, ncol = 2) +
scale_shape_manual('Color', breaks = levels(mdata$Color),
values = c(4, 0, 2, 8),
labels = c('Blue',
'Green', 'Red', 'Yellow')) +
scale_colour_manual('Color', breaks = levels(mdata$Color),
values = c('blue',
'green', 'red', 'yellow'),
labels = c('Blue',
'Green', 'Red', 'Yellow'))
This should color the shapes in the graph and provide one (merged)
legend with colored shapes as symbols.
HTH,
Dennis
On Tue, Oct 25, 2011 at 11:48 PM, RanRL <rnrlrn at gmail.com> wrote:
> Hi everyone,
>
> I have some data about a market research which I want to arrange in one plot
> for easy viewing,
> the data looks something like:
>
> Product Color StoreA StoreB StoreC StoreD Price
>
> ProdA R NA 4.33 2 4.33 35
> G NA 4.33 2 4.33
> 35
> B NA 4.33 2 3.76
> 58
> Y NA 3.72 3 5.33
> 23
>
> ProdB B 5.44 NA 4.22 3.76 87
>
> ProdC G 4.77 3.22 4.77 2.10 65
> B ... ... ... ...
> ..
>
> And so on...
>
> I want to create a plot where the colors of the hits represent the Product
> (A,B,C..), the characther represent the color (X for yellow, box for green,
> etc..), the X axis is the price and the Y axis is the number (0-5) from the
> different Stores (A,B,C,D). I've thought either to create a matrix of 4
> plots ( for the 4 stores) or in some creative way combine them into one
> plot?
>
> Please help me or point me in the right direction as to which functions to
> look into, I've been playing around with ggplot for a few days, but can't
> seem to wrap my head around it yet...
>
> Thanks
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Help-with-a-scatter-plot-tp3939585p3939585.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list