绘制没有NA的图

时间:2019-11-16 14:21:03

标签: r

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

我获得了某些公寓的编号以及每个月的数据。在某些月份中,没有数据提供,所以没有数据。在图形中绘制数据时如何避免这些问题。 PS:单元号不是字符列,而其他是数字。我尝试了以下代码,但没有成功:

plot(data=na.omit(Heatmeters)as.factor(Apartment_no), January,data=Heatmeters)

&可以在一张图中绘制所有月份的数据。实际上,我只希望图表上显示的公寓具有与任何月份对应的数据。

1 个答案:

答案 0 :(得分:1)

您的数据包含'',它是一个空字符串,而不是NA。如果将''显式设置为NA,并且还将月份数据更改为数字,则绘图将产生结果。我假设您想要的是这种情节。

这是一些代码。

Heatmeters = data.frame(
    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[Heatmeters == ''] = NA
Heatmeters[,2:13] = mutate_all(Heatmeters[,2:13], as.numeric)

plot(Heatmeters$Apartment_no, Heatmeters$August)

这是情节的结果...

enter image description here