我有一个浮游生物生物量的 .nc 文件,“经度、纬度、时间”尺寸为“180、125 和 480”。时间数据是数字,以 1,2,3,....480 开头。它代表 1980-2000 之间的年份。我想按年绘制生物量。所以我需要用相同的时间值取 lon 和 lat 的平均值?这是我的代码。但是当我运行时,它给出了一个空的情节:
'''
library(ncdf4)
library(tidyverse)
our_nc_data <- nc_open("corr_eco3m_bs44_conversion_factor_v7_withDetritus_MEECE.nc")
print(our_nc_data)
lat <- ncvar_get(our_nc_data, "latitude")
lon <- ncvar_get(our_nc_data, "longitude")
time <- ncvar_get(our_nc_data, "time")
nt <- dim(time)
start_date <- as.Date("1980-01-01")
time_seq <- seq(start_date, by = "15 days", length.out = nt)
biomass_array <- ncvar_get(our_nc_data, "Zo")
fillvalue <- ncatt_get(our_nc_data, "Zo", "_FillValue")
biomass_array[biomass_array == fillvalue$value] <- NA
lonlattime <- as.matrix(expand.grid(lon, lat, time_seq))
head(lonlattime)
dim(lonlattime)
biomass_vec_long <- as.vector(biomass_array)
biomass_vec_long
length(biomass_vec_long)
biomass_obs <- data.frame(cbind(lonlattime, biomass_vec_long))
head(biomass_obs)
colnames(biomass_obs) <- c("Long", "Lat", "Date", "Biomass_Zo")
head(biomass_obs)
biomass_final <- na.omit(biomass_obs)
head(biomass_final)
dim(biomass_final)
biomass_final <- biomass_final[-c(1:2)]
glimpse(biomass_final)
biomass_final$Date <- as.Date(biomass_final$Date)
biomass_final$Biomass_Zo <- as.double(biomass_final$Biomass_Zo)
# Take the mean
biomass_final <- biomass_final %>%
group_by(Date) %>%
summarize(Mean_biomass = mean(Biomass_Zo, na.rm = TRUE))
write.csv(as.data.frame(lswt_final), "Zo_biomass.csv", row.names=T)
# Plot the data using ggplot2
ggplot(biomass_final, aes(x = Date, y = Mean_biomass)) +
geom_line(size = 0.5) +
labs(x = "Date", y = "Biomass_Zo") +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
'''
我刚刚用您提供的
dput
试了一下,我得到了一个非空的情节。
我将 size =
替换为 linewith =
,因为 size
已被弃用,并且我转换为对数刻度,因为您的 Mean_biomass
值范围如此之大:
ggplot(biomass_final, aes(x = Date, y = Mean_biomass)) +
geom_line(linewidth = 0.5) +
labs(x = "Date", y = "Biomass_Zo") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
scale_y_log10()