geom_tile different gradient scale and color for d

2020-03-08 06:47发布

问题:

Hi I would like to plot the following dataframe.

d<- data.frame (pid=c("d","b","c"), type=c("rna","rna","rna"), value = c(1,2,3) )
d2 <- data.frame (pid=c("d","b","c"), type=c("dna","dna","dna"), value = c(10,20,30) )
df <- rbind (d,d2)

 ggplot(df, aes(y=pid, x=type  ) ) + geom_tile(aes(fill = value),
  colour = "white") + scale_fill_gradient(low = "white",
  high = "steelblue") 

This produces a plot that looks like this,

however, I would like to have each x factor have its own color gradient, so ideally rna is blue to white while dna is red to white. Is there anyway to do this? Of if different gradient is not possible, then what about just different scales? thanks!

回答1:

Here is what it looks like applying @Brian's suggestion to your original example. You may want to rescale the rna and dna value separately, to make the color ranges more comparable.

p = ggplot(df, aes(y=pid, x=type, fill=type, alpha=value)) +
    geom_tile(colour="white", size=1) + 
    scale_fill_manual(values=c(dna="salmon", rna="steelblue")) +
    theme_bw() +
    theme(panel.grid=element_blank()) +
    coord_cartesian(expand=FALSE)



回答2:

ggplot(iris, aes(Sepal.Length, 
                 Petal.Width, 
                 color = Species, 
                 alpha = Sepal.Width)) + 
  geom_point(size = 4)

Also see: How to create a continuous legend (color bar style) for scale_alpha?



标签: r ggplot2