0

I have been trying to plot min and max values of temperature. I actually wanted to plot using geom_area. My data can be downloaded from here.

library(dplyr)
library(ggplot2)

dat <- read.csv("energydata_complete.csv", stringsAsFactors = FALSE)

#renaming attributes meaningfully
#names(dat)[] <- 'temp_kitchen'
dat <- dat %>% 
dplyr::rename('temp_kitchen'=T1,'temp_living'=T2,'temp_laundry'=T3,
                         'temp_office'=T4,'temp_bath'=T5,'temp_build'=T6,'temp_iron'=T7,
                         'temp_teen'=T8,'temp_parent'=T9,'hum_kitchen'=RH_1,'hum_living'=RH_2,
                         'hum_laundry'=RH_3,'hum_office'=RH_4,'hum_bath'=RH_5,'hum_build'=RH_6,
                         'hum_iron'=RH_7,'hum_teen'=RH_8,'hum_parent'=RH_9)
dat$month <- as.factor(months(dat$date))
dat$date <- strptime(dat$date, format = "%Y-%m-%d %H:%M:%S")
dat$date <- as.POSIXct(dat$date, format = "%Y-%m-%d %H:%M:%S")

I have created another dataframe with month and min and max temperature values of each room.

temparature <- dat %>% group_by(month) %>% dplyr::summarise(min_temp_kitch=min(temp_kitchen),
                                                 max_temp_kitch=max(temp_kitchen),
                                                 min_temp_living=min(temp_living),
                                                 max_temp_living=max(temp_living),
                                                 min_temp_laundry=min(temp_laundry),
                                                 max_temp_laundry=max(temp_laundry),
                                                 min_temp_iron=min(temp_iron),
                                                 max_temp_iron=max(temp_iron),
                                                 min_temp_office=min(temp_office),
                                                 max_temp_office=max(temp_office),
                                                 min_temp_bath=min(temp_bath),
                                                 max_temp_bath=max(temp_bath),
                                                 min_temp_parent=min(temp_parent),
                                                 max_temp_parent=max(temp_parent),
                                                 min_temp_teen=min(temp_teen),
                                                 max_temp_teen=max(temp_teen))

Now I am trying to plot min and max temperature values from this dataframe for each room.

Below code didn't give any plot.

ggplot() + geom_area(data = temparature,aes(x=month,y=min_temp_kitch), position = 'stack') +
  geom_area(data = temparature,aes(x=month, y=max_temp_kitch), position = 'stack')

Tried to create with geom_ribbon as below.

ggplot(temparature) + 
  geom_ribbon(aes(x=month, ymin = min_temp_kitch, ymax = max_temp_kitch), color='blue', alpha = 0.5)

This has given enter image description here

But I want a plot something similar to this with points for each value. enter image description here

Can someone suggest how to do this please.

DTYK
  • 1,098
  • 1
  • 8
  • 33
mockash
  • 1,204
  • 5
  • 14
  • 26
  • You should include your dataset using `dput()` to your post. But it might be because `geom_ribbon` like having a numerical value for the `x axis`. See https://stackoverflow.com/questions/36208239/combining-geom-ribbon-when-x-is-a-factor. – MLavoie Jul 26 '18 at 08:44

1 Answers1

0

You don't need to change your dates to factor and need to make the temperature dataframe into long format :

library(dplyr)
library(ggplot2)
library(lubridate)
dat <- read.csv("energydata_complete.csv", stringsAsFactors = FALSE)


dat <- dat %>% 
  rename('temp_kitchen'=T1,'temp_living'=T2,'temp_laundry'=T3,
            'temp_office'=T4,'temp_bath'=T5,'temp_build'=T6,'temp_iron'=T7,
            'temp_teen'=T8,'temp_parent'=T9,'hum_kitchen'=RH_1,'hum_living'=RH_2,
            'hum_laundry'=RH_3,'hum_office'=RH_4,'hum_bath'=RH_5,'hum_build'=RH_6,
            'hum_iron'=RH_7,'hum_teen'=RH_8,'hum_parent'=RH_9) %>%
  mutate(month = floor_date(date(date), unit = 'months'))


temparature <- dat %>%
  group_by(month) %>%
    summarise(min_temp_kitch=min(temp_kitchen),
               max_temp_kitch=max(temp_kitchen),
               min_temp_living=min(temp_living),
               max_temp_living=max(temp_living),
               min_temp_laundry=min(temp_laundry),
               max_temp_laundry=max(temp_laundry),
               min_temp_iron=min(temp_iron),
               max_temp_iron=max(temp_iron),
               min_temp_office=min(temp_office),
               max_temp_office=max(temp_office),
               min_temp_bath=min(temp_bath),
               max_temp_bath=max(temp_bath),
               min_temp_parent=min(temp_parent),
               max_temp_parent=max(temp_parent),
               min_temp_teen=min(temp_teen),
               max_temp_teen=max(temp_teen))


temp2 <- temparature %>%
    tidyr::gather(temp_min_max, Temp, -month)


ggplot() + 
  geom_area(data = temp2 %>%
          filter(temp_min_max %in% c('min_temp_kitch', 'max_temp_kitch')),
        aes(x=month,y=Temp,fill = temp_min_max, color = temp_min_max),
        position = 'identity') 

enter image description here

Thor6
  • 781
  • 6
  • 9
  • I could replicate the same plot in my system. But the max values that are plotted is the sum of `min_temp+max_temp` . If you look at the `temp2` dataframe, the max temperature in Jan is 23.79 but the plot has added 23.79 and 16.79(min_temp). How to get the actual max_temp – mockash Jul 26 '18 at 10:25
  • Oh, you just need to change the `position = 'stack'` to `position = 'identity'` I edited the answer – Thor6 Jul 26 '18 at 10:28
  • Thanks Thor. I will make other changes as per my requirements. – mockash Jul 26 '18 at 10:46