简体   繁体   中英

Heat Map not displaying correctly

I'm trying to make a heat map for pokemon types vs against types and I'm having trouble plotting the table. I'm new to R by the way.

Here is my code:

data %>%
  select(Name, HP, Weight, Height, Att, Def, Spe, Spa, Spd, Type_1, Type_2, Abilities, Generation, Legendary, Catch_Rate) %>%
  tibble()


df_fight_against <- select(data, Type_1, Against_Bug:Against_Water)
head(df_fight_against)

#1 find median amongagainst types
df_fight_against_g <- group_by(df_fight_against, Type_1)
df_fight_against_summ <- summarise(df_fight_against_g, 
                                   against_bug = mean(data$Against_Bug), 
                                   against_dark = mean(data$Against_Dark), 
                                   against_dragon = mean(data$Against_Dragon),
                                   against_electric = mean(data$Against_Electric),
                                   against_fairy = mean(data$Against_Fairy),
                                   against_fight = mean(data$Against_Fighting),
                                   against_fire = mean(data$Against_Fire),
                                   against_flying = mean(data$Against_Flying),
                                   against_ghost = mean(data$Against_Ghost),
                                   against_grass = mean(data$Against_Grass),
                                   against_ground = mean(data$Against_Ground),
                                   against_ice = mean(data$Against_Ice), 
                                   against_normal = mean(data$Against_Normal),
                                   against_poison  = mean(data$Against_Poison),
                                   against_psychic = mean(data$Against_Psychic),
                                   against_rock = mean(data$Against_Rock),
                                   against_steel = mean(data$Against_Steel),
                                   against_water = mean(data$Against_Water))


                                                                    


#2 plot heat map
df_fight_against_long <- melt(df_fight_against_summ)
hm.palette <- colorRampPalette(rev(brewer.pal(9, 'RdYlBu')), space='Lab')
ggplot(data=df_fight_against_long, aes(Type_1, variable)) + geom_tile(aes(fill=value)) + scale_fill_gradientn(colours = hm.palette(100)) + coord_equal() + theme(axis.text.x=element_text(angle=90, hjust=0)) + ggtitle("Effectiveness of different types of Pokemon")

Heres some example data:

 Number             Name Type_1 Type_2                                 Abilities HP Att Def Spa Spd Spe BST      Mean Standard_Deviation Generation Experience_type Experience_to_level_100 Final_Evolution Catch_Rate Legendary Mega_Evolution Alolan_Form Galarian_Form Against_Normal
1     12       Butterfree    Bug Flying                           ['Tinted Lens'] 60  45  50  90  80  70 395  65.83333          15.920811          1     Medium Fast                 1000000               1         45         0              0           0             0              1
2       2          Ivysaur  Grass Poison               ['Chlorophyll', 'Overgrow'] 60  62  63  80  80  60 405  67.50000           8.902247          1     Medium Slow                 1059860               0         45         0              0           0             0              1
3       3         Venusaur  Grass Poison               ['Chlorophyll', 'Overgrow'] 80  82  83 100 100  80 525  87.50000           8.902247          1     Medium Slow                 1059860               1         45         0              0           0             0              1

My problem is that when I plot this data in a heat map it should look like a checker board but I just have rows instead. Heres an image of what I'm getting: 在此处输入图像描述

Does anyone have any tips on how to fix this data?

I think the issue you have are NA' s in the df_fight_against_long data.

Below my code when replaced the NA's with random values

df_fight_against_long <- read_csv("df_fight_against_long.csv")

hm.palette <- colorRampPalette(rev(brewer.pal(9, 'RdYlBu')), space='Lab')

ggplot(data=df_fight_against_long, aes(Type_1, variable)) + 
  geom_tile(aes(fill=value)) + 
  scale_fill_gradientn(colours = hm.palette(100)) + 
  coord_equal() + 
  theme(axis.text.x=element_text(angle=90, hjust=0)) + 
  ggtitle("Effectiveness of different types of Pokemon")

Sample data:

df_fight_against_long <-structure(list(X1 = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 
29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 
45, 46, 47, 48, 49, 50, 51, 52, 53, 54), Type_1 = c("Butterfree", 
"Ivysaur", "Venusaur", "Butterfree", "Ivysaur", "Venusaur", "Butterfree", 
"Ivysaur", "Venusaur", "Butterfree", "Ivysaur", "Venusaur", "Butterfree", 
"Ivysaur", "Venusaur", "Butterfree", "Ivysaur", "Venusaur", "Butterfree", 
"Ivysaur", "Venusaur", "Butterfree", "Ivysaur", "Venusaur", "Butterfree", 
"Ivysaur", "Venusaur", "Butterfree", "Ivysaur", "Venusaur", "Butterfree", 
"Ivysaur", "Venusaur", "Butterfree", "Ivysaur", "Venusaur", "Butterfree", 
"Ivysaur", "Venusaur", "Butterfree", "Ivysaur", "Venusaur", "Butterfree", 
"Ivysaur", "Venusaur", "Butterfree", "Ivysaur", "Venusaur", "Butterfree", 
"Ivysaur", "Venusaur", "Butterfree", "Ivysaur", "Venusaur"), 
    variable = c("against_bug", "against_bug", "against_bug", 
    "against_dark", "against_dark", "against_dark", "against_dragon", 
    "against_dragon", "against_dragon", "against_electric", "against_electric", 
    "against_electric", "against_fairy", "against_fairy", "against_fairy", 
    "against_fight", "against_fight", "against_fight", "against_fire", 
    "against_fire", "against_fire", "against_flying", "against_flying", 
    "against_flying", "against_ghost", "against_ghost", "against_ghost", 
    "against_grass", "against_grass", "against_grass", "against_ground", 
    "against_ground", "against_ground", "against_ice", "against_ice", 
    "against_ice", "against_normal", "against_normal", "against_normal", 
    "against_poison", "against_poison", "against_poison", "against_psychic", 
    "against_psychic", "against_psychic", "against_rock", "against_rock", 
    "against_rock", "against_steel", "against_steel", "against_steel", 
    "against_water", "against_water", "against_water"), value = c(0, 
    0, 0, 1, 2, 1, 2, 3, 4, 5, 6, 7, 1, 1, 1, 4, 4, 5, 5, 5, 
    5, 5, 6, 7, 10, 3, 4, 5, 6, 7, 10, 4, 4, 1, 2, 0, 0, 0, 0, 
    3, 4, 5, 6, 7, 10, 4, 4, 1, 2, 1, 1, 0, 0, 0)), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -54L), spec = structure(list(
    cols = list(X1 = structure(list(), class = c("collector_double", 
    "collector")), Type_1 = structure(list(), class = c("collector_character", 
    "collector")), variable = structure(list(), class = c("collector_character", 
    "collector")), value = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1L), class = "col_spec"))

Plot:

在此处输入图像描述

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