[R] fill circles

jim holtman jholtman at gmail.com
Sat Aug 25 23:12:47 CEST 2007

Here is a function that will generate a color sequence for an input
vector,  You can specify the colors to use, the range and the number
of color steps:

# specify the colors and the number of increments you want for a specified
# range.  It will return the colors for the input vector
# specify the colors and the number of increments you want for a specified
# range.  It will return the colors for the input vector
f.color <-
function(input, # input vector
         colors=c('green','yellow','red'),  # desired colors
         input.range=c(0,0.01),  # range of input to create colors
         input.steps=10)  # number of increments
    myColors <- colorRampPalette(colors)(input.steps)  # generate colors
    myColors[cut(input, seq(input.range[1], input.range[2],
        labels=FALSE, include.lowest=TRUE)]
# generate a legend to show colors
plot.new()  # create blank plot
x <- round(runif(15), 3)
legend('topleft', legend=x, fill=f.color(x, input.range=c(0,1)))
legend('topright', legend=x, fill=f.color(x, input.range=c(0,1),
legend('top', legend=x, fill=f.color(x, input.range=c(0,1),

So you should be able to use something like this.

On 8/25/07, Cristian cristian <taccioli at gmail.com> wrote:
> Hi all,
> I'm an R newbie,
> I did this script to create a scatterplot using the "tree" matrix from
> "datasets" package:
> library('datasets')
> with(trees,
> {
> plot(Height, Volume, pch=3, xlab="Height", ylab="Volume")
> symbols(Height, Volume, circles=Girth/12, fg="grey", inches=FALSE,
> add=FALSE)
> }
> )
> I'd like to use the column Named "Height" to fill the circles with colors
> (ex.: the small numbers in green then yellow and the high numbers in red).
> I'd like to have a legend for the size and the colors too.
> I did it manually using a script like that:
> color[(x>=0.001)&(x<0.002)]<-"#41FF41"
> color[(x>=0.002)&(x<0.003)]<-"#2BFF2B"
> color[(x>=0.003)&(x<0.004)]<-"#09FF09"
> color[(x>=0.004)&(x<0.005)]<-"#00FE00"
> color[(x>=0.005)&(x<0.006)]<-"#00F700"
> color[(x>=0.006)&(x<0.007)]<-"#00E400"
> color[(x>=0.007)&(x<0.008)]<-"#00D600"
> color[(x>=0.008)&(x<0.009)]<-"#00C300" and so on but I don't like to do it
> manually... do know a solution...
> Thank you very much
> chris
>        [[alternative HTML version deleted]]
> ______________________________________________
> R-help at stat.math.ethz.ch 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.

Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

More information about the R-help mailing list