I have two soil moisture hdf images. The first one is 3Km resolution and the second one is 36km. Using same code, the second one can map in the following code as below: soil moisture in 36Km resolution
The first one doesn't show the soil moisture information: soil moisture in 3km resolution
The code I'm using is as below
install.packages("hdf5")
BiocManager::install("hdf5")
devtools::install_github("hhoeflin/hdf5r")
library(devtools)
library(BiocManager)
library(rhdf5)
library(tidyr)
library(ggplot2)
mydata <- h5read("/Users/ss/Downloads/SMAP_L2_SM_A_01725_D_20150529T123629_R13080_001.h5",
"Soil_Moisture_Retrieval_Data")
str(mydata)
latitude<-mydata$latitude
longitude<-mydata$longitude
soil_moisture<-mydata$soil_moisture
soil_moisture[soil_moisture==-9999]<-NA
soil_moisture[soil_moisture>0.5]<-NA
soil_moisture[soil_moisture<0.02]<-NA
data<-cbind(latitude,longitude,soil_moisture)
data1<-data[complete.cases(data),]
soil_moisture = soil_moisture*4
data1<-as.data.frame(data1)
xlmax<-max(data1$longitude)
xlmin<-min(data1$longitude)
ylmax<-max(data1$latitude)
ylmin<-min(data1$latitude)
dataplot<-ggplot(data1)+
geom_tile(aes(x=longitude,y=latitude,fill=soil_moisture))+
xlab("Longitude (deg)") + # x-axis label
ylab("Latitude (deg)") +
geom_path(data = map_data("world"),
aes(x = long, y = lat, group = group))+
scale_fill_distiller(palette = "YlOrRd",limits=c(0.02,0.5),name="SM") +
coord_fixed(xlim =c(floor(xlmin),ceiling(xlmax)),ylim=c(floor(ylmin),ceiling(ylmax)))
print(dataplot)
# ggsave("myplot.png",width=8,height=8,unit="cm",dpi=300)
to anyone who might encounter the same problem, you can use
to plot the soil moisture map of 3km resolution.
replace geom_tile with geom_point.