Map two colour aesthetics for different factors in

2019-05-19 01:48发布

I am trying to use ggplot to make a simple field map. The field design is in a format like this:

design <- data.frame(Genotype=rep(LETTERS[1:4], 4), Status=rep(c("P","N"), each=8),
                 Density=rep(c(40,80), each=4),
                 Row=rep(seq(1:4)),
                 Range=rep(1:4, each=4))

I want a map with fill mapped to Genotype and something else (ideally luminance) mapped to Density. The closest I've come is to use alpha, but it's not ideal, since it goes from very low alpha to very high. At very low alpha it's hard to see what the fill is.

Here's the code for my plot at present:

d.p <- ggplot(design, aes(Row, Range))
d.p1 <- d.p + geom_tile(aes(fill=Genotype, alpha=Density))

I've fiddled around with scale_fill_hue but can't seem to make anything sensible happen.

Thanks in advance.

EDIT

I'm sorry I realised I left out a fundamental aspect of my real world problem when I made the reproducible example, fixed above.

As is pointed out below, alpha can be mapped to Density, but I also need to distinguish between Status. Hatching seems an obvious solution, but I understand that is not an option. I could manually assign similar colour (light blue, dark blue etc), but I would really like to be able to automate this so that I can do it for any field design.

Any ideas appreciated, and sorry for the confusion!

标签: r ggplot2
1条回答
不美不萌又怎样
2楼-- · 2019-05-19 02:46

I will ignore the fact that Row and Genotype contain redundant information, and offer up this:

ggplot(design,aes(x = Row,y = Range)) + 
    geom_tile(fill = "transparent",colour = "black") +
    geom_point(aes(colour = Genotype,size = factor(Density),shape = Status)) + 
    scale_size_manual(values = c(3,6))

enter image description here

But something tells me there will be a reason you can't do this either.

查看更多
登录 后发表回答