简体   繁体   中英

Why do I get an empty plot using geom_tile on Rstudio?

I'm quite new in R programming and I am trying to visualise Normalised Mutual Information between different clusters (obtained with different algorithms).

My goal would be to obtain something like this:

在此处输入图像描述

It looks like a ggcorrplot but i tried with that function and it doesn't work, surely because it isn't correlation coefficients. So now I am trying to obtain it manually using ggplot in ggplot2 package.

I created the data frame manually:

 Nmi_frame <- data.frame(
  "mesure1" = c("fg","fg","fg","fg","fg","fg","fg",
                "sp","sp","sp","sp","sp","sp","sp",
                "wt","wt","wt","wt","wt","wt","wt",
                "lv","lv","lv","lv","lv","lv","lv",
                "sg","sg","sg","sg","sg","sg","sg",
                "ca","ca","ca","ca","ca","ca","ca",
                "cf","cf","cf","cf","cf","cf","cf"),

  "mesure2"= c( "fg","sp","wt","lv","sg","ca","cf",
                 "fg","sp","wt","lv","sg","ca","cf",
                 "fg","sp","wt","lv","sg","ca","cf",
                 "fg","sp","wt","lv","sg","ca","cf",
                 "fg","sp","wt","lv","sg","ca","cf",
                 "fg","sp","wt","lv","sg","ca","cf",
                 "fg","sp","wt","lv","sg","ca","cf"),

  "nmi_value" = c(1,0.3671792,0.3260069,0.2499102,0.2410074,0.2586295,0.1722615,
                  0.3671792,1,0.5884902,0.4380218,0.3148344,0.2399497,0.3783691,
                  0.3260069, 0.5884902, 1,0.474305,0.3441412,0.2264623,0.3448708,
                  0.2499102,0.4380218, 0.474305, 1,0.5431077,0.1560136,0.2078255,
                  0.2410074 ,0.3148344, 0.3441412, 0.5431077,1, 0.1661684,0.1880403,
                  0.2586295,0.2399497, 0.2264623,0.1560136, 0.1661684, 1,0.2315498,
                  0.1722615, 0.3783691, 0.3448708, 0.2078255, 0.1880403, 0.2315498, 1))

And then applied the following code to get the plot:

 ggplot(Nmi_frame, aes(x = mesure1, y = mesure2, fill = nmi_value),
       geom_tile(color = "white", aes(fill = Nmi_frame$nmi),
         scale_fill_gradient(low = "blue", high = "red")))

but I only get a blank plot:

在此处输入图像描述

I already tried to convert my two first columns to factor but it doesn't change the resulting plot.

Any idea about how to get my plot? Or could it be a ggplot bug?

Thank you in advance for your help.

This is basically simple to correct a typo.

library(ggplot2)

ggplot(Nmi_frame, aes(x = mesure1, y = mesure2, fill = nmi_value)) +
  geom_tile() +
  scale_fill_gradient(low = "blue", high = "red")

Created on 2022-09-29 with reprex v2.0.2

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM