IDEMSInternational / R-Instat

A statistics software package powered by R
http://r-instat.org/
GNU General Public License v3.0
38 stars 102 forks source link

Added An Ordinary Climograph to The Climatic Menu #8971

Open MeSophie opened 1 month ago

MeSophie commented 1 month ago

Fixes #8793 @rdstern I added an ordinary climograph menu on Climatic>Describe>Climograph Dialog. Please have a look.

rdstern commented 1 month ago

@MeSophie thanks that looks to be a good and neat start. I'd also like to use this example to encourage you to move forward yourself on the design and the R code of this dialog. I am thinking that we sort of define a climograph as any graph with these data and with 2 y axes, so one on the right as well as one on the left. You added that feature to the plot subdialog, so I assume you can include it here.
Then I assume that the rainfall will have a bar chart and the temperature, or temperatures, will have either a line, if one temperature variable and perhaps a ribbon, or those nice sort of tiles (if 2 of them)? The minimum could then be rainfall bars with mm on the left and inches on the right? Or it could be a single line if just one temperature is given, with degrees C on the left and F on the right?

So this means that Ok is enabled as soon as any one element is given.

Of course the default is with both rainfall and temperature - which is why the 2 scales are really needed.

And it will be nice to have an option to add labels to the graphs.

Over to you, both on design and implementation? In the short term I hope you can get something working quickly, and discuss suggestions on github where you would like to iterate.

Patowhiz commented 1 month ago

I'm happy to see this has been considered . I've been evaluating this kind of graph for interactive visual quality control as well (a bit more advanced than just showing average values).

MeSophie commented 1 month ago

@rdstern this is the code that I implement

mm_to_inches <- 0.0393701
ghana_by_station_month_abbr <- data_book$get_data_frame(data_name="ghana_by_station_month_abbr")

# Define a color palette for the month abbreviations
month_colors <- setNames(rainbow(length(unique(ghana_by_station_month_abbr$month_abbr))), unique(ghana_by_station_month_abbr$month_abbr))

# Create the plot
last_graph <- ggplot(data=ghana_by_station_month_abbr, aes(x=as.numeric(month_abbr))) +
  geom_ribbon(aes(ymin=min_min_temperature, ymax=max_max_temperature, fill = "#000000"), alpha=0.5) + 
  geom_bar(aes(y=mean_sum_rainfall, fill=factor(month_abbr, levels=names(month_colors))), stat="identity", alpha=0.5) +
  geom_line(aes(y=max_max_temperature, colour="blue", group=1)) +
  geom_line(aes(y=min_min_temperature, colour="red", group=1)) +
  geom_text(aes(y=mean_sum_rainfall, label=round(mean_sum_rainfall, 1)), vjust=-0.5, size=3) +
  geom_text(aes(y=max_max_temperature, label=round(max_max_temperature, 1), colour="blue"), 
            vjust=-0.5, size=3, show.legend=FALSE) +
  geom_text(aes(y=min_min_temperature, label=round(min_min_temperature, 1), colour="red"), 
            vjust=1.5, size=3, show.legend=FALSE) +
  scale_y_continuous(
    name="Rainfall (mm)",
    sec.axis = sec_axis(~ . * mm_to_inches, name = "Rainfall (inches)")
  ) +
  scale_fill_manual(values=month_colors, name="Rainfall", guide="legend") +
  scale_colour_identity(name="Temperatures", guide="legend", labels=c("Max Temperature"="blue", "Min Temperature"="red")) +
  scale_x_continuous(name="Month", breaks=1:length(unique(ghana_by_station_month_abbr$month_abbr)), labels=unique(ghana_by_station_month_abbr$month_abbr)) +  # Ensure the x-axis is treated as discrete
  theme_grey() +
  guides(
    fill = guide_legend(order=1, title="Rainfall"),
    colour = guide_legend(order=2, title="Temperatures")
  )

data_book$add_object(data_name="ghana_by_station_month_abbr", object_name="last_graph", object_type_label="graph", object_format="image", object=check_graph(graph_object=last_graph))
data_book$get_object_data(data_name="ghana_by_station_month_abbr", object_name="last_graph", as_file=TRUE)
rm(list=c("last_graph", "ghana_by_station_month_abbr"))

Its producing this graph image

I suggest that if Tmax and Tmin are filled then can enable the check box Add Ribbon and we can add another box for Labelsand for Fill Identity

The secondary y axis is also added as soon one of receiver is fill fahrenheit if it is temperature and inches if it is rain. If you are okay with it I can start implement it on vb.

MeSophie commented 1 month ago

@rdstern I added Tile, Ribbon and Labels on the Ordinary climograph dialogue please have a look.

rdstern commented 2 weeks ago

@MeSophie I know this is still a draft, but I am very keen we merge a version in the next week, or so, because we are training in Malawi from 1 July and I would like to include this, even if there remains some items to complete in a later pull request. a) You have resolved my point a above - that's great.
b) But then it is still work in progress - as the labels - which were working, see above, don't work now.

I wonder why this all takes so long. Could you please make some good progress soon. Please check with @N-thony, or perhaps you are already?

MeSophie commented 2 weeks ago

@rdstern Please could you check the dialogue again?

rdstern commented 2 days ago

@MeSophie you can see how keen I am, by the speed I tried again. Still no graph though.

MeSophie commented 2 days ago

@rdstern I Fixed the problem. Please have a look. Thank you.

MeSophie commented 9 hours ago

image @rdstern I have tried dodoma daily data the error is about the NA values. I I have filtered the values to remove NA and the dialogue works well.

image The Legend for Lines is to add legend on lines. It was colour Identity before.

image Please I need some clarifications here. I have tried and I get either errors or odd graphs. a) When implementing the temperature tile, should we keep the rainfall bars? I.e. if the three receivers are filled (rain, tmax, tmin) and the Add tile box is ticked then the graph will have the bars, lines and tiles? b) If the previous answer is yes, this means that the geom tile function will be: geom_tile(aes(y=min_temperature, x=month_abbr, fill=max_temperature))?