简体   繁体   中英

Map two colour aesthetics for different factors in ggplot2

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!

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))

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM