geom_tile different gradient scale and color for d

2020-03-08 06:15发布

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, enter image description here

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!

标签: r ggplot2
2条回答
Explosion°爆炸
2楼-- · 2020-03-08 06:32
ggplot(iris, aes(Sepal.Length, 
                 Petal.Width, 
                 color = Species, 
                 alpha = Sepal.Width)) + 
  geom_point(size = 4)

enter image description here

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

查看更多
在下西门庆
3楼-- · 2020-03-08 06:34

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)

enter image description here

查看更多
登录 后发表回答