简体   繁体   中英

plot min and max temperature by months

I'm very new to using R. I want to create a stacked bar chart displaying the proportion of temperature readings <38C or >=38C as a function of time.

Date    Temperature (C) 
10/22/18 8:00   36.8
10/22/18 12:00  36.8
10/22/18 16:00  36.8
10/23/18 12:00  36.8
10/30/18 16:00  36.8
10/22/18 0:00   36.9
10/29/18 20:00  36.9
10/31/18 8:00   36.9
10/18/18 4:00   37
10/20/18 20:00  37
10/21/18 20:00  37
10/30/18 4:00   37
6/15/18 20:00   36.7
6/16/18 4:00    37
6/16/18 8:00    37.1
6/16/18 12:00   37.1
6/16/18 16:00   37.1
6/16/18 0:00    37.4
4/27/18 20:00   36.4
4/28/18 0:00    36.5
4/28/18 4:00    36.5
4/27/18 18:00   36.7
4/28/18 8:00    36.8
7/31/18 0:00    36.6
8/1/18 4:00     36.6
7/31/18 8:00    36.8
7/31/18 12:00   36.8
7/31/18 16:00   36.8
8/1/18 8:00     36.8
7/30/18 20:00   36.9
7/31/18 4:00    36.9

I've tried a variety of different codes, but they don't seem to work well. This is the most recent one I've tried.

ggplot(master, aes(event, mastertemps)) + 
 geom_line() + 
 ylim(c(0, 1)) + 
 xlab("") + 
 ylab("Temperature") + 
 scale_x_date(date_breaks = "1 month", date_labels = "%b %y") + 
 theme(axis.text.x = element_text(angle = 90, hjust = 1)) + 
 ggtitle("2018 Temperatures")

This is the error message I received when I ran this

"Invalid input: date_trans works with objects of class Date only"

I want a plot that looks similar to this:

每月最高和最低温度

Here's a crack at plotting daily min-max based on the sample data you have provided:

library(tidyverse)
dat <- read.csv("test.csv") # stored your sample data as a csv

dat <- dat %>% mutate(date = as.Date(Date, format="%m/%d/%y %H:%M")) # new variable in Date class

d2 <- dat %>% group_by(date) %>% summarise(min = min(Temperature), max = max(Temperature)) # new variables with min and max daily temperature

d2 <- d2 %>% gather(var, val, -date) # change the data to long format


ggplot(d2, aes(date, val, colour=var)) + geom_line() + geom_point() # plot

在此处输入图片说明

To plot the daily in monthly min-max format:

d3 <- dat %>% 
  mutate(date = as.Date(Date, format="%m/%d/%y %H:%M")) %>% 
  mutate(month = format(date, "%m")) 

d3 <- d3 %>% group_by(month) %>%  summarise(min = min(Temperature), max = max(Temperature))

d3 <- d3 %>% gather(var, val, -month)

d3$month <- as.numeric(d3$month)

ggplot(d3,aes(month, val, colour=var)) + 
  geom_line() + 
  geom_point() + 
  scale_x_continuous("Month", breaks=seq(4, 10, 2), labels=c("Apr", "Jun", "Aug", "Oct")) +
  ylab("Temperature")

在此处输入图片说明

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