Lyndz
Lyndz

Reputation: 423

Problem in flipping y and x in ggplot2 when plotting vertical profiles

I am plotting the vertical profile of relative humidity but the displayed plot is not correct.

Here's the data

 dat<- structure(list(Lev = c(1000L, 950L, 900L, 850L, 800L, 750L, 700L, 
 650L, 600L, 550L, 500L, 450L, 400L, 350L, 300L, 250L, 200L, 150L, 
 100L, 50L, 0L), April29 = c(NA, NA, NA, NA, 85, NA, NA, 39.5, 
 33, NA, NA, 56, NA, 57, 49, NA, NA, 42, 20, 15, NA)), class = "data.frame", row.names = 
 c(NA,-21L))

Here's the script:

library(ggplot2)
library(plyr)
inDir<-"."
imgDir <- "."
inFiles<-list.files(inDir,pattern="rh_profile.csv",full.names=T)
for (inFile in inFiles) {
 dat<- read.csv(inFile,header=TRUE,sep=",")
 dat<-data.frame(dat)


p <- ggplot(dat)
p <- p + geom_line(aes(y=April29, x = Lev, color="April29"))
p <- p + geom_point(aes(y=April29, x = Lev),shape=4,size=2,color="red")

p <- p + coord_flip()

p <- p + theme(panel.background=element_rect(fill="white"),
         plot.margin = unit(c(1,1,1,1),"cm"),  
         panel.border=element_rect(colour="black",fill=NA,size=1),
         axis.line.x=element_line(colour="black"),
         axis.line.y=element_line(colour="black"),
         axis.text=element_text(size=15,colour="black",family="serif"),
         axis.title=element_text(size=15,colour="black",family="serif"),
         legend.position = "")
p <- p + scale_colour_manual(name="",values=c("Lin"="red"))

p <- p + scale_x_discrete(breaks=seq(50,1000,50),limits=seq(50,1000,50),labels=c("1000","","900","","800","","700","","600","","500","","400","","300","","200","","100",""))

p <- p + scale_y_discrete(limits=seq(-30,12,by=5))
p <- p + geom_hline(yintercept=0)
p <- p + labs(y="Relative Humidity Bias (%)",x="Pressure Level (hPa)")

outImg <- paste0(imgDir,"/",gsub("\\.csv","",basename(inFile)),".png")
    ggsave(outImg,p,width=5,height=8)
}

Here's the output image but this is not correct. The values does not correspond to the correct coordinates.

Incorrect output

1 Any suggestions on how I can do this in R correctly?

2 I also want the y-axis to be in log form, where the values at the lower levels should be closer. The expected output is similar to this:

sample desired output

Upvotes: 1

Views: 198

Answers (1)

jazzurro
jazzurro

Reputation: 23574

You have NAs in your data. You want to drop them when you draw lines. You also want to create a group variable. If you want to use log values, you can check scale_y_log10(), for instance. I leave this part for you.

library(dplyr)
library(ggplot2)

mutate(dat, group = 1) %>% 
filter(complete.cases(April29)) -> dat

# I modified your code.

ggplot(dat) +
geom_line(aes(x = Lev, y = April29, group = factor(group)), color = "red") +
geom_point(aes(x = Lev, y = April29), shape = 4, size = 2, color = "red") +
geom_hline(yintercept = 0) +
coord_flip() +
scale_x_reverse(breaks = seq(from = 50, to = 1000, by = 50),
                limits = c(1000, 50),
                labels = c("", "100", "", "200", "",
                           "300", "", "400", "",
                           "500", "", "600", "",
                           "700", "", "800", "",
                           "900", "", "1000")) +
labs(x = "Pressure Level (hPa)", y = "Relative Himidity Bias (%)") +
theme(panel.background = element_rect(fill = "white"),
      plot.margin = unit(c(1,1,1,1),"cm"),  
      panel.border = element_rect(colour="black",fill=NA,size=1),
      axis.line.x = element_line(colour="black"),
      axis.line.y = element_line(colour="black"),
      axis.text = element_text(size = 15, colour = "black", family = "serif"),
      axis.title = element_text(size = 15, colour = "black",family = "serif"),
      legend.position = "")

enter image description here

Upvotes: 3

Related Questions