简体   繁体   English

全年年度时间序列的R热图

[英]R heat map of annual time series by entire year

I am trying to make a heatmap of several years of daily averages of salinity in an estuary in R. 我正在尝试绘制R河口的盐度多年平均值的热图。

I would like the format to include month on the x-axis and year on the y-axis, so each Jan 1st directly above another Jan. 1st. 我希望格式在x轴上包含月份,在y轴上包含年份,因此每个1月1日直接位于另一个1月1日之上。 In other words, NOT like a typical annual calendar style (not like this: http://www.r-bloggers.com/ggplot2-time-series-heatmaps/ ). 换句话说,不像典型的年度日历样式(不像这样: http : //www.r-bloggers.com/ggplot2-time-series-heatmaps/ )。

So far I have only been able to plot by the day of the year using: 到目前为止,我只能使用以下命令在一年中的某一天进行绘图:

{r} d <- read.xlsx('GC salinity transposed.xlsx', sheetName = "vert-3", header = TRUE, stringsAsFactors = FALSE, colClasses = c("integer", "integer", "numeric"), endRow = 2254)

{r} ggplot(d, aes(x = Day.Number, y = Year)) + geom_tile(aes(fill = Salinity)) + scale_fill_gradient(name = 'Mean Daily Salinity', low = 'white', high = 'blue') + theme(axis.title.y = element_blank())

And get this: heat map not quite right 并得到这个: 热图不是很正确

Could someone please tell me a better way to do this - a way that would include month, rather than day of the year along the x-axis? 有人可以告诉我一种更好的方法吗?这种方法将包括月份,而不是沿x轴的一年中的某天? Thank you. 谢谢。 New to R. R的新手。

The lubridate package comes in handy for stuff like this. 润滑包装可方便地用于此类物品。 Does this code do what you want? 这段代码能满足您的要求吗? I'm assuming you only have one salinity reading per month and there's no need to average across multiple values in the same month. 我假设您每个月只有一个盐度读数,并且没有必要在同一个月中对多个值进行平均。

library(lubridate)
library(ggplot2)

# Define some data
df <- data.frame(date = seq.Date(from = as.Date("2015-01-01"), by = 1, length.out = 400),
                 salinity = runif(400, min=5, max=7))
# Create fields for plotting
df$day <- paste0(ifelse(month(df$date)<10,"0",""),
                 month(df$date),
                 "-",
                 ifelse(day(df$date)<10,"0",""),
                 day(df$date))
df$month <- paste0(ifelse(month(df$date)<10,"0",""),
                   month(df$date))
df$year <- year(df$date)
library(lubridate)
library(ggplot2)

# Define some data
df <- data.frame(date = seq.Date(from = as.Date("2015-01-01"), by = 1, length.out = 400),
                 salinity = runif(400, min=5, max=7))
# Create fields for plotting
df$day <- paste0(ifelse(month(df$date)<10,"0",""),
                 month(df$date),
                 "-",
                 ifelse(day(df$date)<10,"0",""),
                 day(df$date))
df$month <- paste0(ifelse(month(df$date)<10,"0",""),
                   month(df$date))
df$year <- year(df$date)
#Plot results by month
ggplot(data=df) +
  geom_tile(aes(x = month, y = year, fill = salinity)) +
  scale_y_continuous(breaks = c(2015,2016))
#Plot results by day
ggplot(data=df) +
  geom_tile(aes(x = day, y = year, fill = salinity)) +
  scale_y_continuous(breaks = c(2015,2016))

Results by month: 按月显示的结果:

在此处输入图片说明

Results by day (do you really want this? It's very hard to read with 366 x-axis values): 每天的结果(您真的想要这个吗?很难读取366个x轴值): 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM