Home > Back-end >  How to visualize the original data by the values
How to visualize the original data by the values

Time:05-04

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.

CodePudding user response:

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

CodePudding user response:

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

  • Related