Hypsometric Curve Plotting using ggplot2

dataset <- read_xlsx("bar.xlsx")
db <- (dataset)
db1 =  as.data.frame(db[,1:2]) 
db2 =  db[,c(1,3)]
db3 =  db[,c(1,4)]

ylim.prim <- c(0,4000 )   # in this example, precipitation
ylim.sec <- c(0, 600)    # in this example, temperature
b <- diff(ylim.prim)/diff(ylim.sec)
a <- b*(ylim.prim[1] - ylim.sec[1])

db1 = as.data.frame(db1)
dat.m <- melt(db1, id='Elevation')
dat.m1 <- melt(db2, id='Elevation')
dat.m2 <- melt(db3, id='Elevation')

plot1 <-ggplot() + 
  geom_bar(data = dat.m, aes(x = dat.m$Elevation, y = dat.m$value,fill = variable), stat = 'identity', position = 'dodge')+
  geom_line(data= dat.m1, aes(x= dat.m1$Elevation,y=a+ dat.m1$value*b ),color="black",size=1 )+
  scale_y_continuous("Area (km2)",breaks = c(0,500,1000,1500,2000,2500,3000,3500,4000), labels = c(0,500,1000,1500,2000,2500,3000,3500,4000),
  sec.axis = sec_axis(~ (. - a)/b,breaks = c(0,50,100,150,200,250,300,350,400,450,500,550,600), labels = c(0,50,100,150,200,250,300,350,400,450,500,550,600), name = "Glacerized Area (Km2)"))+
  labs(x="Elevation (m)", title="Gilgit Hysometric Curve")+ theme_bw()+
  scale_x_continuous(limits = c(0,8000),breaks = c(0,500,1000,1500,2000,2500,3000,3500,4000,4500,5000,5500,6000,6500,7000,7500,8000), labels = c(0,500,1000,1500,2000,2500,3000,3500,4000,4500,5000,5500,6000,6500,7000,7500,8000))+
  
  theme(axis.title.x = element_text(color = "black", size = 12, face = "bold"),axis.title.y = element_text(color = "black", size = 10, face = "bold"))+
    theme(axis.text.x = element_text(face="bold", color="black",size=10),
          axis.text.y = element_text(face="bold", color="black",  size=10))
#figure <- ggarrange(plot1,plot2,plot3,plot4,plot5,plot6,
                    #labels = c("Astore", "Gilgit","Hunza","Indus Down Stream","Shigar","Shyok",font.label = 4),
                    #ncol = 2, nrow = 3)
figure <-plot1+ ggsave("hypsometric curve.png",width=10, height= 8,dpi=300)

Dear All, I am trying to develop hypsometric curve using ggplot. As you see in the attachment excel file as well as png picture. I developed the area elevation and glacerized curve but I did not understand how to mean monthly temperature on it.
Kindly guide me how I can plot mean monthly temperature on secondry horizontal axis.


bar.pdf (62.4 KB)

I do not know what you want to plot as a function of mean temperature so I chose glacier area. I simplified your data because I had to type it by hand. It would be much more convenient if you posted code that generates your data rather than an image. I also simplified the code so the plot scaling is not exactly the same as in your example. I hope it gives you an idea of how to set up a second x axis.

library(ggplot2)
dataset <- data.frame(Elev = seq(2000, 7500, 500),
                 Area = c(200, 480, 780,1300, 2200, 3800,3000,760, 120, 13,1,0),
                 Glacier = c(NA, NA, 3,13,57,168,540,300,80,11,1,0),
                 Month = 1:12,
                 MeanTemp = c(6,9,14,18,21,23,23,23,22,18,12,7))

ggplot(data = dataset) + 
  geom_bar(aes(x = Elev, y = Area), stat = 'identity')+
  geom_line(aes(x= Elev,y=Glacier * 5),color="black",size=1 )+
  geom_line(aes(x = MeanTemp * 320, y = Glacier * 5), color = "red", size = 1) +
  scale_y_continuous("Area (km2)",breaks = seq(0,4000, 500), 
                     labels = seq(0,4000, 500),
                     sec.axis = sec_axis(~ . /5)) +
  labs(x="Elevation (m)", title="Gilgit Hysometric Curve")+ theme_bw()+
  scale_x_continuous(limits = c(0,8000),breaks = seq(0,8000, 500), 
                     labels = seq(0,8000, 500),
                     sec.axis = sec_axis(~ ./320, name = "Mean Temp.")) +
  
  theme(axis.title.x = element_text(color = "black", size = 12, face = "bold"),axis.title.y = element_text(color = "black", size = 10, face = "bold"))+
  theme(axis.text.x = element_text(face="bold", color="black",size=10),
        axis.text.y = element_text(face="bold", color="black",  size=10),
        axis.text.x.top = element_text(color = "red"))
#> Warning: Removed 2 row(s) containing missing values (geom_path).
#> Warning: Removed 1 row(s) containing missing values (geom_path).

Created on 2020-06-28 by the reprex package (v0.3.0)

https://drive.google.com/drive/folders/1AH6N9hjhmSYPS-l_7ZhyHhZ1Ue93d95c?usp=sharing
Dear Sir,
Thank you so much. To a some extent, I want to plot like similar plot. It would be more good if you can also show Month names on secondary horizontal axis with scale values.

I shared with you my google drive link where you can find my R code as well as excel file. Can we display glacier area on secondry vertical axis in more effective way?

https://drive.google.com/drive/folders/1AH6N9hjhmSYPS-l_7ZhyHhZ1Ue93d95c?usp=sharing

I am confused about what you want on the secondary x axis. In your first post you said:

Kindly guide me how I can plot mean monthly temperature on secondry horizontal axis.

In your most recent post you said:

It would be more good if you can also show Month names on secondary horizontal axis with scale values.

What does it mean to have both temperature and the month on the secondary x axis?

More generally, plotting so many quantities on one graph is likely to be confusing. Might it make more sense to have two plots?

Dear Sir,
Thank you for your reply.
Sorry for inconvenience. I am saying that I also want to show month names on secondary horizontal axis. Currently, you plotted mean monthly temperature values but its difficult for readers to understand which month has specific temperature value. Although, it look fine to understand mean monthly temperature change but we can analyse which month has higher or lower value. Can we show month name on temperature line in someway?

Thanks

Thanks

Maybe using labels would work. The plot below needs some adjustment but conveys the idea.

library(ggplot2)
dataset <- data.frame(Elev = seq(2000, 7500, 500),
                 Area = c(200, 480, 780,1300, 2200, 3800,3000,760, 120, 13,1,0),
                 Glacier = c(NA, NA, 3,13,57,168,540,300,80,11,1,0),
                 Month = 1:12,
                 MeanTemp = c(6,9,14,18,21,23,23,23,22,18,12,7))

MonthData <- data.frame(TEMP= c(6,9,14,18, 21, 23,18, 12,7),
                        Y = c(3700, 3700, 3700, 3700,3700, 3300, 3300, 3300, 3300),
                        LABEL= c("Jan", "Feb", "Mar", "Apr", "May", 
                                 "Jun-Sep", "Oct", "Nov", "Dec"))
ggplot(data = dataset) + 
  geom_bar(aes(x = Elev, y = Area), stat = 'identity')+
  geom_line(aes(x= Elev,y=Glacier * 5),color="black",size=1 )+
  geom_line(aes(x = MeanTemp * 320, y = Glacier * 5), color = "red", size = 1) +
  geom_label(aes(x= TEMP *320, y = Y, label = LABEL), data = MonthData, color= "red") +
  scale_y_continuous("Area (km2)",breaks = seq(0,4000, 500), 
                     labels = seq(0,4000, 500),
                     sec.axis = sec_axis(~ . /5)) +
  labs(x="Elevation (m)", title="Gilgit Hysometric Curve")+ theme_bw()+
  scale_x_continuous(limits = c(0,8000),breaks = seq(0,8000, 500), 
                     labels = seq(0,8000, 500),
                     sec.axis = sec_axis(~ ./320, name = "Mean Temp.")) +
  
  theme(axis.title.x = element_text(color = "black", size = 12, face = "bold"),axis.title.y = element_text(color = "black", size = 10, face = "bold"))+
  theme(axis.text.x = element_text(face="bold", color="black",size=10),
        axis.text.y = element_text(face="bold", color="black",  size=10),
        axis.text.x.top = element_text(color = "red"))
#> Warning: Removed 2 row(s) containing missing values (geom_path).
#> Warning: Removed 1 row(s) containing missing values (geom_path).

Created on 2020-06-28 by the reprex package (v0.3.0)

Dear Thank you so much.

I have mean monthly temperature data. I want to add points to the lines. But my legends are not working properly. I want to plot the same maps as I did in excel. Thanks in advance. The code and excel file also present in google drive link.

library(readxlsx)
library(ggplot2)
library(scales)
library(lubridate)
library(dplyr)
library(tidyr)
library(readxl)
library(RColorBrewer)
dataset <- read_xlsx("Monthly_temp.xlsx", sheet = 1)
db = as.data.frame(dataset)
db$Month<- ymd(db$Month)
plt <- ggplot(db, aes(x= db$Month))
plot1<-plt + geom_line(data= db,aes(y=db$Gilgit,color="Gilgit"),size=1 ) + xlab("Month") +
  geom_point(data= db,aes(y=db$Gilgit,color="Gilgit_point"),size=3 )+
  geom_line(data= db,aes(y=db$Gupis,color="Gupis"),size=1) +
  geom_point(data= db,aes(y=db$Gupis),shape = 24,size=3 )+
  geom_line(data= db,aes(y=db$Ushkore,color="Ushkore"),size=1)+
  geom_point(data= db,aes(y=db$Ushkore),shape = 22,size=3 )+
  geom_line(data= db,aes(y=db$Yasin,color="Yasin"),size=1)+
  geom_point(data= db,aes(y=db$Yasin,color="Yasin_point"),shape = 23,size=3 )+
  scale_color_manual(name="Legends",values = c('Gilgit' = 'black',"Gilgit_point"="black",'Gupis' = 'red',"Ushkore"= "lightsalmon2","Yasin"= "brown","Yasin_point"="brown"))+ theme_bw() 
#plot5 <- plot1+ scale_x_date(date_labels = "%b")+ theme(axis.text.x = element_text(angle=45, hjust = 1))   
plot2<-plot1 + scale_x_date(date_labels = "%b",breaks='1 months',expand=c(0,0))+ scale_y_continuous(limits = c(-10,30),breaks = c(-5,-10,0,5,10,15,20,25,30), labels = c(-5,-10,0,5,10,15,20,25,30))+
  labs(y=expression(paste("Average Temperature ( ºC )"),colour = "Parameter"),size =14)+theme(axis.title.x = element_text(color = "black", size = 12, face = "bold"),axis.title.y = element_text(color = "black", size = 14, face = "bold"))+
  theme(axis.title.x = element_text(color = "black", size = 12, face = "bold"),axis.title.y = element_text(color = "black", size = 12, face = "bold"))+
  theme(axis.text.x = element_text(face="bold", color="black",size=12),
        axis.text.y = element_text(face="bold", color="black",  size=12))+
  theme(plot.title = element_text(size = 12, face = "bold"),
        legend.title=element_text(size=12,face = "bold"), 
        legend.text=element_text(size=12,face = "bold"))


https://drive.google.com/file/d/1-aESvsyDf3rgFF4RwtOop_1As43VtlBq/view?usp=sharing,%20https://drive.google.com/file/d/1RqJQvdivzaZ9l-2r-pUG5ZMFJkaDEg4f/view?usp=sharing

When I click the google drive link I get the message "No preview available. File is in owner's trash."

https://drive.google.com/drive/folders/1AH6N9hjhmSYPS-l_7ZhyHhZ1Ue93d95c?usp=sharing

Dear Kindly check it.

Thanks

ggplot is much easier to use if you have your data in a tidy long format. Take a look at the structure of the DFlng data frame in the code below. The legend can then be made automatically.

library(openxlsx)
library(tidyr)
library(ggplot2)
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#> 
#>     date
DF <- read.xlsx("~/R/Play/Monthly_temp.xlsx", )
DFlng <- DF %>% gather(key = "Region", value = "Value", -Month )
DFlng$Month <- as.Date(DFlng$Month,origin = "1899-12-30")
DFlng$MonthLabel <- month(DFlng$Month, label = TRUE)
COLORS <- c("Gilgit" = "black", "Gupis" = "red", "Shendur" = "lightsalmon2", "Ushkore" = "brown")
SHAPES <- c("Gilgit" = 18, "Gupis" = 4, "Shendur" = 15, "Ushkore" = 17)
ggplot(DFlng, aes(x = MonthLabel, y = Value, group = Region, color = Region, shape = Region)) +
  geom_point(size = 3) +
  geom_line(size = 1) +
  scale_color_manual(values = COLORS) +
  scale_shape_manual(values = SHAPES)

Created on 2020-07-03 by the reprex package (v0.2.1)

1 Like

Dear Sir,
Thank you so much. Its working Now.

I am trying to convert my h5 daily files into a raster format. I converted into raster format. When I extracted my area of interest. I observed that issue is related to extent. Kindly anyone guides me on how to solve this issue. The R code and image is present in link with the attachment. Thanks

https://drive.google.com/drive/folders/18-hj2LEYWBN-uIDDTdqZ-x-WUxpCJu7H?usp=sharing

if (!requireNamespace("BiocManager", quietly = TRUE))
  install.packages("BiocManager")

BiocManager::install("rhdf5")
# browseVignettes("rhdf5")
library(rhdf5)
library(sp)
library(raster)


# h5ls("reconstruction_Sierra_2015.h5")
h5ls("reconstruction_indus_CY2001.h5")
# h5readAttributes(file = "reconstruction_Sierra_2015.h5", name = "Grid")
h5readAttributes(file = "reconstruction_indus_CY2001.h5", name = "Grid")



# h5f = H5Fopen("reconstruction_Sierra_2015.h5")
h5f = H5Fopen("reconstruction_indus_CY2001.h5")
# h5f
# h5f&'Grid'

This question is not related to the earlier posts in the thread. You are much more likely to get good responses if you start a new thread with a title that accurately summarizes the question.

Dear Sir,
Right, Thanks

using the data.table package

library(openxlsx)
library(data.table)
library(ggplot2)
#data.table
DT <- read.xlsx("~/R software/RR SCRIPT/R Data Table/Monthly_temp.xlsx", )
DT<-setDT(DT)
DTlng<-melt(DT,
            id.vars       = "Month",
            measure.vars  = c(2:5),
            variable.name = "Region",
            value.name    = "Value",
            variable.factor = FALSE)
DTlng[,Month :=as.Date(Month,origin = "1899-12-30"),]
DTlng[,MonthLabel:=factor(month(Month), labels= month.abb, ordered = TRUE),]
##PLOT
COLORS <- c("Gilgit" = "black", "Gupis" = "red", "Shendur" = "lightsalmon2", "Ushkore" = "brown")
SHAPES <- c("Gilgit" = 18, "Gupis" = 4, "Shendur" = 15, "Ushkore" = 17)
DTlng[,ggplot(.SD, aes(x = MonthLabel, y = Value, group = Region, color = Region, shape = Region)) +
  geom_point(size = 3) +
  geom_line(size = 1) +
  scale_color_manual(values = COLORS) +
  scale_shape_manual(values = SHAPES),]