Travis Heeter Travis Heeter - 5 days ago 5
R Question

GGplot heatmap has 2 labels on each tile

Here's the data I'm trying to plot:

structure(list(Var1 = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), class = "factor", .Label = c("Specificity", "Dunn Index")), Var2 = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L, 1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L), class = "factor", .Label = c("Variance (2)", "Variance (4)", "Variance & Mean (2)", "Variance & Mean (4)", "Radar Only (2)", "Radar Only (4)", "All Data(2)", "All Data(3)", "All Data Scaled(2)", "All Data Scaled(4)")), value = c(-0.692279597863213, -1.48943741434953, -1.09355531182318, -1.08413861964885, 0.912823257976641, -0.626715742011029, -1.94602923014127, -0.746300681279627, 0.912823257976641, 1.64344717562082, -0.491325775754128, 0.350878495792349, 0.912823257976641, 0.90352725291858, 0.661946945469388, 0.668160073306832, 0.311225652165799, 0.688901920415944, 0.511547544016677, -0.308322460765497, -0.692279597863213, -1.48943741434953, -1.09355531182318, -1.08413861964885, 0.912823257976641, -0.626715742011029, -1.94602923014127, -0.746300681279627, 0.912823257976641, 1.64344717562082, -0.491325775754128, 0.350878495792349, 0.912823257976641, 0.90352725291858, 0.661946945469388, 0.668160073306832, 0.311225652165799, 0.688901920415944, 0.511547544016677, -0.308322460765497), ovals = c(48.41, 42.06, 73.81, 28.57, 73.81, 51.59, 73.81, 69.84, 64.29, 67.46, 5.28412698412698, 11.7253968253968, 8.63095238095238, 33.4107142857143, 18.3043650793651, 76.5388888888889, 36.3634920634921, 52.168253968254, 52.1623015873016, 84.1174603174603, 0.00520833333333333, 0.0164803125616411, 0.0292019422400468, 0.0258761022200942, 0.0923386443151634, 0.0563903328454791, 0.0717603398435939, 0.065214418675562, 0.0657912811602958, 0.0380569462508109, 0.72083903433112, 0.757277684759935, 0.633561928614755, 0.496305437129809, 0.435493681015241, 0.402952149580986, 0.296927697111903, 0.300126336907244, 0.247276928682524, 0.257546088468624)), .Names = c("Var1", "Var2", "value", "ovals"), row.names = c(NA, -40L), class = "data.frame")


Here's my Code:

ggplot(hi.m, aes(Var1, Var2)) +
geom_tile(aes(fill = value)) +
geom_label(aes(label = round(ovals,4)),fill="white",alpha=0.65) +
scale_fill_viridis(name="Scaled\nValue",option="plasma")


I have a melted data frame,
hi.m
, that contains scaled versions of the original values, but I want the original values to be the label, so I added them to
hi.m
as a new column, ovals.

Here's the plot outcome, it looks like the scaled values are on top of the unscaled values, but they're all being rounded to 4 decimal places:

enter image description here

Here's what I've tried:


  1. Resetting the graphical parameters with
    par(op)

  2. Placing
    label
    in the 1st
    aes()

  3. Placing
    fill
    in the 1st
    aes()

  4. Removing
    geo_tile
    (both labels still show up)


Answer

Your data has multiple observations for both Var1 and Var2, so it's overlaying each value on the label. If you can, aggregate your data frame prior to plotting. enter image description hereAn example using dplyr is below, I used mean but that could be another aggregation function of your choice (which you'll have to normalize yourself and you may want to do this upstream in your workflow):

library(dplyr)
library(ggplot2)
library(viridis)

plot_data <- hi.m %>% 
  group_by(Var1, Var2) %>% 
  summarize_each(funs(mean), value, ovals)

ggplot(plot_data, aes(Var1, Var2)) +
  geom_tile(aes(fill = value)) + 
  geom_label(aes(label = round(ovals,4)),fill="white",alpha=0.65) +
  scale_fill_viridis(name="Scaled\nValue",option="plasma")
Comments