简体   繁体   中英

Custom colours with geom_tile

If I have a data frame that looks like this:

    Name Track Position   Color
1      A     0        1 #009ACD
2      B     1       15 #50568B
3      C     2       55 #8C7125
4      A     0       44 #009ACD
5      B     3       98 #50568B
6      D     0       99 #77DF98

What would be a correct way to use geom_tile so that each level of the Name column was plotted with the Track as the x-axis point, Position as the y-axis point, and Color as the actual color of the tile?

It should look something like this:

在此处输入图片说明

We need to set col and fill argument to Color variable, then use scale_color_identity and scale_fill_identity :

library(ggplot2)

df1 <- read.table(text = "Name Track Position Color
A 0 1 #009ACD
B 1 15 #50568B
C 0 55 #8C7125
A 0 44 #009ACD
B -1 98 #50568B
D 0 99 #77DF98",header=TRUE,comment.char = "")

ggplot(df1, aes(Track, Position, col = Color, fill = Color)) +
  geom_tile() +
  scale_color_identity() +
  scale_fill_identity() +
  #prettify
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.ticks.y = element_blank(),
        axis.text.y = element_blank(),
        axis.title = element_blank())

在此处输入图片说明

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