Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

How to put legends on interactive ggplot2 heat map

$
0
0
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
Apartment_no <- c("1-SV","1-SV","1-SV","1-SH","1-SH","1-SH","1-1V","1-1V","1-1V","1-1H","1-1H","1-1H","3-SV","3-SV","3-SV","3-1V","3-1V","3-1V","3-1H","3-1H","3-1H")
month <- c("September","October","November","September","October","November","September","October","November","September","October","November","September","October","November","September","October","November","September","October","November")
Days <- c(19,19,28,2,19,28,2,19,28,2,19,28,25,31,28,12,29,24,8,26,19)
Heat_clean <- data.frame(Apartment_no,month,Days)

I have created an interactive heat map using ggplot2. I used the following code:

Heat_clean %>% 
mutate(color = case_when(Days <= 5 ~ "blue", 
Days <= 15 ~ "orange", 
Days <= 25 ~ "pink", 
is.na(Days) ~ "red", TRUE ~ "green")) %>% 
ggplot(aes(month,Apartment_no)) + 
geom_tile(aes(fill=color),color="white")+
scale_fill_identity()+
geom_text(aes(x=month,y=Apartment_no,label=Days))-> p

plotly::ggplotly(p)

Unfortunately, I am getting colors in the place of legends & I want the conditions along with the colors as legends. For eg. if seen in the above code when Days<=5, color=Blue, 525, color=Green. I would like them as legends. My present plot can be seen in the attached figure Current figure


Viewing all articles
Browse latest Browse all 201839

Trending Articles