How to avoid NA's while plotting a graph

Apartment_no <- c('1-SV','1-SH','3-SV','3-SH','5-SV','5-SH','7-SV','7-SH')
January <- c('', '', '3','5','9','','15','19')
February <- c('5', '', '3','5','','15','','19')
March <- c('5', '', '3','5','16','15','7','')
April <- c('', '', '3','5','','15','','19')
May <- c('', '8', '3','5','','15','','19')
June <- c('12', '', '','5','','15','','19')
July <- c('5', '', '3','5','','15','','19')
August <- c('51', '', '3','','','15','','19')
September <- c('5', '', '3','5','','15','','19')
October <- c('57', '', '','5','','15','','19')
November <- c('5', '', '','5','','21','','19')
December <- c('', '', '33','5','','15','','19')
Heatmeters <- data.frame(Apartment_no,January,February,March
                         ,April,May,June,July,August,September,August,November,December)
library(tidyverse)
library(dplyr)
library(ggplot2)
Heatmeters%>%select(Apartment_no,January,February,March,April,May,June,July,August,September,August,November,December) 
#>   Apartment_no January February March April May June July August September
#> 1         1-SV                5     5             12    5     51         5
#> 2         1-SH                                8                           
#> 3         3-SV       3        3     3     3   3         3      3         3
#> 4         3-SH       5        5     5     5   5    5    5                5
#> 5         5-SV       9             16                                     
#> 6         5-SH               15    15    15  15   15   15     15        15
#> 7         7-SV      15              7                                     
#> 8         7-SH      19       19          19  19   19   19     19        19
#>   November December
#> 1        5         
#> 2                  
#> 3                33
#> 4        5        5
#> 5                  
#> 6       21       15
#> 7                  
#> 8       19       19

I am given certain apartment no's along with the data for each month. For some of the months the data is not given so there are na's. How to avoid these na's while plotting the data in the graph.
PS: Apartment no is a character column however the others are numeric. I tried the following code but it didn't work out:
plot(data=na.omit(Heatmeters)as.factor(Apartment_no), January,data=Heatmeters).
& is it possible to plot data for all the months in one graph.

Hi @kartik.kaushal

You can play around with how you actually plot the data, but here is how I would change the data structure to clean it up for plotting...

heat_clean <- 
Heatmeters %>% 
  pivot_longer(2:last_col(), names_to = 'month') %>% 
  mutate(value = as.numeric(na_if(value, '')),
         month = factor(month, levels = month.name, ordered = TRUE))

heat_clean %>% 
  ggplot(aes(month, value, fill = Apartment_no)) +
  geom_col() +
  coord_flip() +
  facet_wrap(~Apartment_no)
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.