首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >全年年度时间序列的R热图

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

Stack Overflow用户
提问于 2016-02-24 05:48:51
回答 1查看 1.5K关注 0票数 1

我正在尝试制作一张R河口几年来盐度日平均值的热图。

我希望格式包括在x轴上的月份和y轴上的年,所以每年1月1日直接在另一个1月1日。换句话说,不像典型的年历风格(不像这样:http://www.r-bloggers.com/ggplot2-time-series-heatmaps/)。

到目前为止,我只能在一年中的某一天使用以下命令进行绘图:

{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())

并获得这个:heat map not quite right

有没有人能告诉我一个更好的方法--在x轴上包含月份,而不是一年中的某一天?谢谢。R的新手。

EN

回答 1

Stack Overflow用户

发布于 2016-02-24 12:00:09

对于这样的东西,lubridate包很方便。这段代码能做你想要的吗?我假设你每个月只有一次盐度读数,并且不需要在同一个月中对多个值进行平均。

代码语言:javascript
复制
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))

每个月的结果:

一天一次的结果(你真的想这样吗?366个x轴值很难读懂):

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35588916

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档