[R] dotplot

Daniel Nordlund djnordlund at frontier.com
Sat May 16 22:19:49 CEST 2015

On 5/16/2015 12:32 PM, li li wrote:
> Hi all,
>    I wrote the following code and have two questions:
>    (1) As you can see, I would like different colors for different
> types. It does not come out that way in the graph from this code.
> Anyone know how to fix this?
>     (2) How do I made the lots number on x axis eligible to read?
>     (3) How do I remove the verticle line in the graph.
>      Thanks for your help.
>        Hanna
> lot <- as.character(c("D1300000" ,"D1300005" ,"D1300010", "D1300015",
> "D1300020",
>           "D1300025" ,"D1300030" ,"D1300035", "D1300040",  "D1300045",
>           "D1300000" ,"D1300005" ,"D1300010", "D1300015",  "D1300020",
>           "D1300025" ,"D1300030" ,"D1300035", "D1300040",  "D1300045",
>           "D1300000" ,"D1300005" ,"D1300010", "D1300015",  "D1300020",
>           "D1300025" ,"D1300030" ,"D1300035", "D1300040",  "D1300045"))
> val <- rnorm(30)
> type <- rep(1:3,each=10)
> tmp <- as.data.frame(cbind(val, type, lot))
> tmp$sybm <- rep(c(1, 12, 16), each=10)
> tmp$color <- rep(c("blue", "red", "green"), each=10)
> tmp$val <- as.numeric(tmp$val)
> dotplot(val ~ lot, tmp,
>          ylab = "values", xlab="lot", aspect=1,
>          pch=tmp$sybm, color=tmp$color)

Li Li,

1. if the dotplot function you are referring to is from the lattice 
package, then the correct name for the "color" parameter is col.  You 
could just use col=type to get different colors but you won't have 
control over the colors chosen.  I created a vector, color, specifying 
the colors and the order they will be applied.

2.  the scales parameter is what you are looking for.  The parameter is 
looking for a list which consists of name=value pairs. You can rotate 
the labels by a specified number of degrees.  In my example, I chose 30 
degrees, rot=30.

3. I don't know how to eliminate the vertical lines, but if you read the 
lattice documentation, you will find it (if it is possible).

color=c("blue", "red", "green")
dotplot(val ~ lot, tmp,
         ylab = "values", xlab="lot", scales=list(rot=30), aspect=1,
         pch=tmp$sybm, col=color)

Hope this is helpful,


Daniel Nordlund
Bothell, WA USA

More information about the R-help mailing list