'How to visualize the original data by the values
I have a dataset that looks like
df <- data.frame(
name = LETTERS[seq(from = 1, to = 10)],
var1 = sample(1:20, 10),
var2 = sample(5:25, 10),
var3 = sample(10:30, 10),
var4 = sample(15:35, 10),
var5 = sample(20:40, 10)
)
How can I present the original data filling the units by the value in ggplot? Like what can be done by the Excel 'conditional formatting' function?
I guess geom_segment is involved here while my columns are a set of variables (they have meaningful names in the 'real' dataset) rather than numeric values so does my rownames (actually the first column). So I am totally lost how to set x, xend, y, yend, etc and make it just the way it is.
Thank you for your help.
Solution 1:[1]
You could achieve your desired result via geom_tile by first converting your data to long format using e.g. tidyr::pivot_longer like so:
set.seed(123)
library(ggplot2)
library(tidyr)
df_long <- df |>
pivot_longer(-name, names_to = "var")
ggplot(df_long, aes(x = var, y = name, fill = value)) +
geom_tile() +
geom_text(aes(label = value)) +
scale_fill_gradient(low = "#F7FCF5", high = "#005A32")

Solution 2:[2]
library(data.table)
ggplot(melt(setDT(df), id.var="name"),aes(variable, name, fill=value)) +
geom_bin2d() +
geom_text(aes(label=value)) +
scale_fill_gradient(low = "lightgreen", high = "darkgreen") +
scale_x_discrete(position = "top") +
scale_y_discrete(limits=rev) +
labs(x="", y="") +
theme(legend.position="none")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | stefan |
| Solution 2 | langtang |


