[R] Help with a scatter plot

Jim Lemon jim at bitwrit.com.au
Wed Oct 26 11:10:01 CEST 2011


On 10/26/2011 05:48 PM, RanRL 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...
>
Hi RanRL,
I swapped the colors and product names, but this rather inelegant code 
might do what you want:

ranrl<-read.table("ranrl.dat",header=TRUE)
plot(ranrl$Price,ranrl$StoreC,ylim=range(ranrl[,3:5],na.rm=TRUE),
  type="n",xlab="Price",ylab="Number sold")
text(ranrl$Price[1],ranrl[1,3:5],
  paste("ProdA",names(ranrl)[3:5],sep="\n"),
  col="red")
text(ranrl$Price[2],ranrl[2,3:5],
  paste("ProdA",names(ranrl)[3:5],sep="\n"),
  col="green")
text(ranrl$Price[3],ranrl[3,3:5],
  paste("ProdA",names(ranrl)[3:5],sep="\n"),
  col="blue")
text(ranrl$Price[4],ranrl[4,3:5],
  paste("ProdA",names(ranrl)[3:5],sep="\n"),
  col="yellow")
text(ranrl$Price[5],ranrl[5,3:5],
  paste("ProdB",names(ranrl)[3:5],sep="\n"),
  col="blue")
text(ranrl$Price[6],ranrl[6,3:5],
  paste("ProdC",names(ranrl)[3:5],sep="\n"),
  col="green")

Jim



More information about the R-help mailing list