Reputation: 1792
This code for a heatmap, is from a previous answer here. I've changed it so it displays four decimal places:
library(tidyverse)
## make data
dat <- matrix(rnorm(100, 3, 1), ncol=10)
## reshape data (tidy/tall form)
dat2 <- dat %>%
tbl_df() %>%
rownames_to_column('Var1') %>%
gather(Var2, value, -Var1) %>%
mutate(
Var1 = factor(Var1, levels=1:10),
Var2 = factor(gsub("V", "", Var2), levels=1:10)
)
## plot data
ggplot(dat2, aes(Var1, Var2)) +
geom_tile(aes(fill = value)) +
geom_text(aes(label = round(value, 4))) +
scale_fill_gradient(low = "white", high = "red")
With four decimal places my output looks like this:
How can I:
Thank you.
Upvotes: 2
Views: 6590
Reputation: 1792
Thank you to Z.Lin for this which works perfectly.
library(tidyverse)
## make data
dat <- matrix(rnorm(100, 3, 1), ncol=10)
## reshape data (tidy/tall form)
dat2 <- dat %>%
tbl_df() %>%
rownames_to_column('Var1') %>%
gather(Var2, value, -Var1) %>%
mutate(
Var1 = factor(Var1, levels=1:10),
Var2 = factor(gsub("V", "", Var2), levels=1:10)
)
## plot data
ggplot(dat2, aes(Var1, Var2)) +
geom_tile(aes(fill = value)) +
geom_text(aes(label = round(value, 4)), size = 2) +
scale_fill_gradient(low = "white", high = "red") + coord_fixed()
Upvotes: 2