Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the alpha values in R{graphics} while the colour argument is used

I'm so used to doing this in ggplot2 that I'm having a very hard time figuring out how to specify alpha values using R base graphics, while the col= argument in plot() is used for assigning a colour type to a categorical variable.

Using the iris data set (although in this context it doesn't really make sense why we would need to change the alpha values)

data(iris)
library(ggplot2)
g <- ggplot(iris, aes(Sepal.Length, Petal.Length)) + geom_point(aes(colour=Species), alpha=0.5) #desired plot

plot(iris$Sepal.Length, iris$Petal.Length, col=iris$Species) #attempt in base graphics

What about mapping another variable to the alpha value using {graphics}? For example in ggplot2:

g2 <- ggplot(iris, aes(Sepal.Length, Petal.Length)) + geom_point(aes(colour=Species, alpha=Petal.Width)) 

Any help is appreciated!

like image 309
user3389288 Avatar asked Apr 14 '14 14:04

user3389288


People also ask

How do I change the color of a data point in R?

To change the color and the size of points, use the following arguments: col : color (hexadecimal color code or color name). For example, col = "blue" or col = "#4F6228" .

What does Alpha do in Ggplot?

Alpha refers to the opacity of a geom. Values of alpha range from 0 to 1, with lower values corresponding to more transparent colors.

What is the function to give color to plot?

R plot() Function (Add Titles, Labels, Change Colors and Overlaying Pots)

How do you make a transparent graph in R?

The only way to specify alpha values at present are via the special colour name "transpar- ent", which produces a fully transparent white, or using the alpha graphical parameter in grid (see the examples below). The alpha graphical parameter is a number from 0 to 1 (opaque to transparent).


2 Answers

Adjusting alpha is pretty easy with adjustcolor function:

COL <- adjustcolor(c("red", "blue", "darkgreen")[iris$Species], alpha.f = 0.5)
plot(iris$Sepal.Length, iris$Petal.Length, col = COL, pch = 19, cex = 1.5) #attempt in base graphics

enter image description here

Mapping alpha to variable requires a bit more hacking:

# Allocate Petal.Length to 7 length categories
seq.pl <- seq(min(iris$Petal.Length)-0.1,max(iris$Petal.Length)+0.1, length.out = 7)

# Define number of alpha groups needed to fill these
cats <- nlevels(cut(iris$Petal.Length, breaks = seq.pl))

# Create alpha mapping
alpha.mapping <- as.numeric(as.character(cut(iris$Petal.Length, breaks = seq.pl, labels = seq(100,255,len = cats))))

# Allocate species by colors
COLS <- as.data.frame(col2rgb(c("red", "blue", "darkgreen")[iris$Species]))

# Combine colors and alpha mapping
COL <- unlist(lapply(1:ncol(COLS), function(i) {
  rgb(red = COLS[1,i], green = COLS[2,i], blue = COLS[3,i], alpha = alpha.mapping[i], maxColorValue = 255)
  }))

# Plot
plot(iris$Sepal.Length, iris$Petal.Length, col = COL, pch = 19, cex = 1.5) 

enter image description here

like image 127
Mikko Avatar answered Oct 02 '22 14:10

Mikko


You can try to use the adjustcolor function

For example:

getColWithAlpha <- function(colLevel, alphaLevel)
{
  maxAlpha <- max(alphaLevel)
  cols <- rainbow(length(levels(colLevel)))
  res <- cols[colLevel]
  sapply(seq(along.with=res), function(i) adjustcolor(res[i], alphaLevel[i]/maxAlpha) )
}

 plot(iris$Sepal.Length, iris$Petal.Length, 
      col = getColWithAlpha(iris$Species, iris$Petal.Width), pch = 20)

Hope it helps,

alex

like image 33
alko989 Avatar answered Oct 01 '22 14:10

alko989