Reputation: 3584
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:
Upvotes: 1
Views: 1508
Reputation: 56249
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())
Upvotes: 2