简体   繁体   中英

geom_area plot with min and max values

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 在此处输入图片说明

But I want a plot something similar to this with points for each value. 在此处输入图片说明

Can someone suggest how to do this please.

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') 

在此处输入图片说明

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