Does anyone know how to use ggplot2 to convert a data frame in R with continous values into a pretty figure. This would be similar to the answer from this post but with ggplot2.
Is this possible?
New to R and ggplot2 so thanks in advance for any advice.
Here's an example using the mtcars
data (scaled to give comparable values, so the numbers don't mean much).
The key things are the use of gather
to tidy the data, geom_tile
filled by value, and geom_text
for the labels. Everything else is just manipulation of that particular data frame.
You could also just use one of the scale_fill_gradient
geoms.
library(tidyverse)
library(viridis)
mtcars %>%
scale() %>%
as.data.frame() %>%
rownames_to_column(var = "make") %>%
gather(var, val, -make) %>%
ggplot(aes(var, make)) +
geom_tile(aes(fill = val)) +
geom_text(aes(label = round(val, 2)),
size = 3) +
coord_fixed() +
scale_fill_viridis() +
guides(fill = FALSE)
Or using:
+ scale_fill_gradient2(midpoint = 1.5)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.