-1

Im trying to plot a heatmap in R, but when I run my code it gives me this error:

Error: Can't combine `No.` <integer> and `Mes` <character>.

What I'm doing wrong? Here is my code:

df %>% 
  pivot_longer(-Localidad) %>% 
  ggplot(aes(x = name, y = Localidad  , fill = value)) + 
  geom_tile(colour="gray80", size=0.2) +
  geom_text(aes(label=value)) +
  theme_minimal() +
  scale_fill_distiller(palette = "YlGnBu", direction = -1, na.value = "white")

My df its something like this, Im working with a lot of data, so thats why I didnt want to print all the head of, but here it is.

> dput(head(df))
structure(list(No. = 1:6, Mes = c("oct-10", "oct-10", "oct-10", 
"oct-10", "oct-10", "oct-10"), Delegacion = c("09CIUDAD DE MÉXICO", 
"09CIUDAD DE MÉXICO", "09CIUDAD DE MÉXICO", "09CIUDAD DE MÉXICO", 
"09CIUDAD DE MÉXICO", "09CIUDAD DE MÉXICO"), Localidad = c("09016MIGUEL HIDALGO", 
"09005GUSTAVO A. MADERO", "09005GUSTAVO A. MADERO", "09003COYOACÁN", 
"09010ÁLVARO OBREGÓN", "09011TLÁHUAC"), Esquema = c("U", "U", 
"U", "U", "U", "U"), Número = c(629L, 1402L, 699L, 48L, 539L, 
55L), Nombre = c("MUNDO DE LOS PEQUES", "GUARDERIA EL ARBOL DE LA NIÑEZ", 
"LOS PEQUEÑOS GENIOS II", "MI MUNDO FELIZ", "CENTRO ECOLÓGICO DE DESARROLLO INFANTIL II", 
"ESTANCIA INFANTIL TERCER MILENIO"), X2.1 = c(1L, 1L, 1L, 1L, 
1L, 1L), X2.2 = c(1L, 1L, 1L, 1L, 1L, 1L), X2.3 = c(1L, 1L, 1L, 
1L, 1L, 1L), X2.4 = c(1L, 1L, 1L, 1L, 1L, 1L), X2.5 = c(1L, 1L, 
1L, 1L, 1L, 1L), X2.6 = c(1L, 1L, 0L, 1L, 1L, 0L), X2.7 = c(1L, 
1L, 1L, 1L, 1L, 1L), X2.8 = c(1L, 1L, 1L, 1L, 1L, 1L), X2.9 = c(1L, 
1L, 1L, 1L, 1L, 1L), X2.1.1 = c(1L, 1L, 1L, 1L, 1L, 1L), X2.11 = c(1L, 
1L, 1L, 1L, 1L, 1L), X2.12 = c(1L, 1L, 1L, 1L, 1L, 1L), X3.1 = c(1L, 
1L, 1L, 1L, 1L, 1L), X3.2 = c(1L, 1L, 1L, 1L, 1L, 1L), X5.1 = c(1L, 
1L, 1L, 1L, 1L, 1L), X5.2 = c(1L, 1L, 1L, 1L, 1L, 1L), X5.3 = c(1L, 
1L, 1L, 1L, 1L, 1L), X5.4 = c(1L, 1L, 1L, 1L, 1L, 1L), X5.5 = c(1L, 
1L, 1L, 1L, 1L, 1L), X5.6 = c(1L, 1L, 1L, 1L, 1L, 1L), X5.7 = c(1L, 
1L, 1L, 1L, 1L, 1L), X5.8 = c(1L, 1L, 1L, 1L, 1L, 1L), X6.1 = c(1L, 
1L, 1L, 1L, 1L, 1L), X6.2 = c(1L, 1L, 1L, 1L, 1L, 1L), X6.3 = c(1L, 
1L, 1L, 1L, 1L, 1L), X6.4 = c(1L, 1L, 1L, 1L, 1L, 1L), X6.5 = c(1L, 
1L, 1L, 1L, 1L, 1L), X7.1 = c(1L, 1L, 1L, 1L, 1L, 1L), X7.2 = c(1L, 
1L, 1L, 1L, 1L, 1L), X7.3 = c(1L, 1L, 1L, 1L, 1L, 1L), X7.4 = c(1L, 
1L, 1L, 1L, 1L, 1L), X8.1 = c(1L, 1L, 1L, 1L, 1L, 1L), X8.2 = c(1L, 
1L, 1L, 1L, 1L, 1L), X9.1 = c(1L, 1L, 1L, 1L, 1L, 1L), X9.2 = c(1L, 
1L, 1L, 1L, 1L, 1L), X9.3 = c(1L, 1L, 1L, 1L, 1L, 1L), X9.4 = c(1L, 
1L, 1L, 1L, 1L, 1L), X10.1 = c(1L, 1L, 1L, 1L, 1L, 1L), X10.2 = c(1L, 
1L, 1L, 1L, 1L, 1L), X10.3 = c(1L, 1L, 1L, 1L, 1L, 1L), X10.4 = c(1L, 
1L, 1L, 1L, 1L, 1L), X10.5 = c(1L, 1L, 1L, 1L, 1L, 1L), X10.6 = c(1L, 
1L, 1L, 1L, 1L, 1L), X10.7 = c(1L, 1L, 1L, 1L, 1L, 1L), X10.8 = c(1L, 
1L, 1L, 1L, 1L, 1L), X10.9 = c(1L, 1L, 1L, 1L, 1L, 1L), X11.1 = c(1L, 
1L, 1L, 1L, 1L, 1L), X11.2 = c(1L, 1L, 1L, 1L, 1L, 1L), X11.3 = c(1L, 
1L, 1L, 1L, 1L, 1L), X11.4 = c(1L, 1L, 1L, 1L, 1L, 1L), X11.5 = c(1L, 
1L, 1L, 1L, 1L, 1L), X11.6 = c(1L, 1L, 1L, 1L, 1L, 1L), X11.7 = c(1L, 
1L, 1L, 1L, 1L, 1L), X11.8 = c(1L, 1L, 1L, 1L, 1L, 1L), X11.9 = c(1L, 
1L, 1L, 1L, 1L, 1L), X11.1.1 = c(1L, 1L, 1L, 1L, 1L, 1L), X11.11 = c(1L, 
1L, 1L, 1L, 1L, 1L), X11.12 = c(1L, 1L, 1L, 1L, 1L, 1L), X11.13 = c(1L, 
1L, 1L, 1L, 1L, 1L), X11.14 = c(1L, 1L, 1L, 1L, 0L, 0L), X11.15 = c(1L, 
1L, 1L, 1L, 1L, 0L), X11.16 = c(1L, 1L, 1L, 1L, 1L, 1L), X12.1 = c(1L, 
1L, 1L, 1L, 1L, 1L), X12.2 = c(1L, 1L, 1L, 1L, 1L, 1L), X12.3 = c(1L, 
1L, 1L, 0L, 1L, 1L), X12.4 = c(1L, 1L, 1L, 1L, 1L, 1L), X12.5 = c(1L, 
1L, 1L, 1L, 1L, 1L), X12.6 = c(1L, 1L, 1L, 1L, 1L, 1L), X12.7 = c("SI", 
"SI", "SI", "SI", "NO", "NO"), X12.8 = c(0L, 0L, 0L, 0L, NA, 
NA), X14.1 = c(1L, 1L, 1L, 1L, 1L, 1L), X14.2 = c(1L, 1L, 1L, 
1L, 0L, 1L), Puntos.máximos = c(71L, 71L, 71L, 71L, 70L, 70L), 
    Puntos.alcanzados = c(70L, 70L, 69L, 69L, 68L, 67L), X. = c(98.59, 
    98.59, 97.18, 97.18, 97.14, 95.71), No..de.Padres = c(7L, 
    7L, 6L, 7L, 7L, 7L), Horas = c(14L, 14L, 12L, 14L, 14L, 14L
    )), row.names = c(NA, 6L), class = "data.frame")
Juan Jesus
  • 23
  • 6
  • It would be much easier to help if we had representative data to work with. Please make this question *reproducible*. This includes sample code you've attempted (including listing non-base R packages, and any errors/warnings received), sample *unambiguous* data (e.g., `data.frame(x=...,y=...)` or the output from `dput(head(x))`), and intended output given that input. Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Dec 01 '21 at 15:10
  • 2
    Your percentages are all in character (denoted by in your tibble). You need to translate that first to numeric – CIAndrews Dec 01 '21 at 15:10
  • Thanks for answering @r2evans Its a lot of data. – Juan Jesus Dec 01 '21 at 15:19
  • Can you be more specific please. @CIAndrews – Juan Jesus Dec 01 '21 at 15:19
  • This data set `df` is not the same as in your code. `df %>% pivot_longer(-Localidad)` gives `Error: Can't combine `No.` and `Mes` .` – CIAndrews Dec 01 '21 at 15:26
  • Oh my bad, I was working with a table but is the same dataframe, I was trying to see if it works with the table I created, But I have the same error as you can see. @CIAndrews – Juan Jesus Dec 01 '21 at 15:28

1 Answers1

0

You can try to exclude the character columns from pivoting. Not entirely sure if the result will be what you expected though.

library(ggplot2)
library(tidyr)

dff <- pivot_longer(df, colnames(df)[!sapply( df, is.character )] )

ggplot(dff, aes(x = name, y = Localidad  , fill = value)) + 
   geom_tile(colour="gray80", size=0.2) +
   geom_text(aes(label=value)) +
   theme_minimal() +
   scale_fill_distiller(palette = "YlGnBu", direction = -1, na.value = "white")
# plot
Andre Wildberg
  • 12,344
  • 3
  • 12
  • 29