How to have multiple color scales into one plot R

2019-07-31 23:17发布

This question already has an answer here:

I have three data series which are composed of :

  • X (float)
  • Y (float)
  • S (float)
  • Class (discrete values)

All three data series are sharing the same X coordinate but every other component is different from each other data series. By using one geom_point() for each of my three data series (the library ggpplot2 in R) I would like to plot each of the data series with a color scale according to it specific S as follow :

ggplot(data, aes(x=X)) + geom_point(aes(y=Y, colour=S, shape=Class)) 

This works if I am using only one data series. The problem is that if I define three geom_points() as specified using their own Y and S, they all have the same color scale and this is a bit confusing on the plot.

As I am already using the shapes to distinguish between the Classes I would really enjoy to have a specific color with its own color gradient for each of my data series .

Lets say for example :

  • from dark blue to light blue for the data series 1
  • from dark red to light red for the data series 2
  • from dark yellow to light yellow for the data series 3

I looked around but I haven't found anything satisfying my needs. Some comments where saying that using ggplot2 it is not possible to have more than one color scale per plot... Is it true ?

If anyone has already figured out this kind of plot with or without ggplot2 I would greatly appreciate his or her solution.

标签: r colors ggplot2
1条回答
淡お忘
2楼-- · 2019-08-01 00:05

In ggplot you can really only use alpha for what you're asking. I've made up some data:

df1 <- data.frame(X=rnorm(16), Y=rnorm(16), S=rep(1:4,times=4), Class=rep(LETTERS[1:4], each=4))
df2 <- data.frame(X=rnorm(16), Y=rnorm(16), S=rep(1:4,times=4), Class=rep(LETTERS[1:4], each=4))
df3 <- data.frame(X=rnorm(16), Y=rnorm(16), S=rep(1:4,times=4), Class=rep(LETTERS[1:4], each=4))

ggplot(df1, aes(x)) + geom_point(aes(y=Y, colour=S, shape=Class))
df1$id <- 1
df2$id <- 2
df3$id <- 3
df.list <- list(df1, df2, df3)
df.all <- ldply(df.list, rbind)

ggplot(df.all, aes(X, Y)) + geom_point(aes(colour=as.factor(id), shape=Class, alpha=S))

Not sure if that meets your requirements...

查看更多
登录 后发表回答