Change Colors of Ranges in ggplot2 Heatmap in R (2 Examples) | Gradient & Categories | geom_tile()

preview_player
Показать описание
R code of this video:

y = letters[1:5],
values = runif(50, 0, 10))

library("ggplot2") # Load ggplot2 package

ggplot(data, aes(x, y, fill = values)) + # Default ggplot2 heatmap
geom_tile()

ggplot(data, aes(x, y, fill = values)) + # Change gradient color
geom_tile() +
scale_fill_gradient(low = "#353436",
high = "#f6f805",
guide = "colorbar")

data_new <- data # Duplicate data
data_new$groups <- cut(data_new$values, # Add group column
breaks = c(0, 2, 4, 6, 8, 10))

ggplot(data_new, aes(x, y, fill = groups)) + # Specify colors manually
geom_tile() +
scale_fill_manual(breaks = levels(data_new$groups),
values = c("#353436", "#f6f805", "#1b98e0", "white", "green"))

Follow me on Social Media:

Рекомендации по теме
Комментарии
Автор

As always clear and to the point. Thanks

georgiosvovas
Автор

Thank you. If I want to use my specific data table, how can I introduce the table of data to R to use the codes?

ZohrehFazelan
Автор

Thank you! What is geom_tile() actually doing? I couldn't understand by its documentation...

larissacury
visit shbcf.ru