eliocamp / metR

Tools for Easier Analysis of Meteorological Fields
https://eliocamp.github.io/metR/
140 stars 22 forks source link

Error using geom_text_contour #101

Closed jkittner closed 4 years ago

jkittner commented 5 years ago

Hi eliocamp, I really love your package it makes things so easy, but reacently for some reason I get this random error:

Error in `[.data.table`(data, , `:=`(angle, .cont.angle(x, y, min.size)),  : 
  Type of RHS ('logical') must match LHS ('double'). To check and coerce would impact performance too much for the fastest cases. Either change the type of the target column, or coerce the RHS of := yourself (e.g. by using 1L instead of 1)

when using geom_text_contour(). But none of my vectors are logical, all of them are numeric ... My code is:

library(metR)
library(openair)
library(ggplot2)

RawData <- read.table(file = "InData.txt", header = T)
RawData$date <- as.POSIXct(RawData$date, tz = "Etc/GMT-1")

# Define Function
plotThermopleths <- function(InData, breaks, DateLabelFormat, avgTime, bins, xlab){
  workData <- InData
  workData %>%
    mutate(Hour = hour(as.POSIXct(workData$date, tz = "Etc/GMT-1")))-> workData
  workData %>%
    mutate(Day = as.Date(workData$date, tz = "Etc/GMT-1"))-> workData

  workData <- data.frame(workData)
  workData$Day <- as.POSIXct(workData$Day)

  workData <- timeAverage(workData, avg.time = avgTime, statistic = "mean", type = "hour", fill = TRUE)
  workData <- data.frame(workData)

  #defince Plot
  thermIsoPlot <- ggplot(data = workData, aes(x = workData$Day, y = workData$Hour, z = workData$temp2m))+
    geom_contour_fill(bins = bins, na.fill = TRUE)+
    geom_contour2(bins = bins, color = "black", na.fill = TRUE)+
    scale_fill_gradientn(colors = rev(brewer.pal(10, "Spectral")))+
    theme_bw()+
    theme(legend.position = "bottom")+
    coord_cartesian(expand = FALSE)+
    scale_x_datetime(breaks = breaks, date_labels = DateLabelFormat)+
    scale_y_continuous(breaks = seq(0,24,2))+
    geom_text_contour(stroke = 0.1, check_overlap = TRUE)+ # The Error occurres in this line...
    labs(fill = "Temperatur [°C]")+
    xlab(xlab)+
    ylab("Uhrzeit [MEZ]")+
    ggtitle("Isoplethendiagramm der Lufttemperatur")
  return(thermIsoPlot)
}

# call function
plotThermopleths(InData = RawData, 
                 breaks = "month", 
                 DateLabelFormat = "%m/%y", 
                 avgTime = "month", 
                 bins = 10, 
                 xlab = "Monat")

For some reason for InDataHH.txt it works fine, but for InDataBI.txt it throws the error. Both datasets have the exact same format. I can't make any sense out of this. It seems totally random for me... The data I am using: InDataBI.txt InDataHH.txt

eliocamp commented 4 years ago

Hi! Sorry I missed this issue for so long!

I found that the problem is that in your code your data has just two points in the x direction. Because of that, most of your contour lines have only two points, which then breaks the way geom_text_contour() computes the position of the text.

I've pushed a patch to remove the error and return empty labels when something like that happens (you can install with remotes::install_github("eliocamp/metR")). A full fix requires some more serious changes and will take a while.

jkittner commented 4 years ago

Hi, sorry for my late response. It fixed the problem and everything works fine now. Thank you so much! I'll close the issue here.