xxx
xxx

Reputation: 307

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?

enter image description here

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.

Upvotes: 0

Views: 49

Answers (2)

langtang
langtang

Reputation: 24832

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

bin2d

Upvotes: 2

stefan
stefan

Reputation: 125373

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

Upvotes: 3

Related Questions