Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the ribbon geom and the general graph dialog? #8428

Closed
rdstern opened this issue Jul 10, 2023 · 3 comments · Fixed by #8448
Closed

Improve the ribbon geom and the general graph dialog? #8428

rdstern opened this issue Jul 10, 2023 · 3 comments · Fixed by #8448

Comments

@rdstern
Copy link
Collaborator

rdstern commented Jul 10, 2023

@lilyclements has prepared a nice climatic graph as follows:
image

This uses geom_ribbon which we have already in R-Instat. She uses summary climatic data and then (of course) R code to produce the graph. In the short-term I would like to be able to produce it using the Describe > General > Graph dialog. Later I see this as a nice example of a special climatic climatic graph to go into the Climatic > Describe > Rainfall dialog - which is currently empty and disabled.

Here is the code and I suggest it indicates a need for an improvement in geom_ribbon, and also in the general dialog.

# Percentiles plot -----------------------------------
ggplot(data=CIMH_3_by_Year_Month_by_Month, aes(x=as.numeric(month_abbr))) +
  geom_ribbon(aes(ymin=value, ymax=max_sum_rain), fill = "grey") +
  geom_ribbon(aes(ymin=p10_sum_rain, ymax=p90_sum_rain), fill = "blue") +
  geom_ribbon(aes(ymin=p33_sum_rain, ymax=p67_sum_rain), fill = "lightblue") +
  geom_line(aes(y=median_sum_rain), size=2.0, group = "grey", colour="black") +
  viridis::scale_fill_viridis() + theme_grey() +
  theme(plot.caption=element_text(size=8),
        plot.title=element_text(size=20),
        plot.subtitle=element_text(size=15),
        axis.text.x=element_text()) +
  labs(title="", subtitle="", caption="",
       x = "Month", y = "Rainfall") +
  scale_x_continuous(breaks = 1:12, labels = levels(CIMH_3_by_Year_Month_by_Month$month_abbr)) 

a) @lilyclements could you look at the current geom_ribbon. I think it needs the fill parameter added, Maybe both as an aeshetic and a parameter. It seems the most obvious for ribbon and I wonder why it is missing. I don't see anything else missing?

image

b) Then there is an "interesting" feature in this plot, namely that the x-axis is a factor, but the geom needs a numeric variable. That seems to me to be a general point, so perhaps we could add this option into the dialog? My first go - for comment by @lilyclements is as follows:

image

In this dialog, underneath the x-variable we move the Fill:Colour down and have 2 checkboxes. Both are invisible initially. If the x-variable is a factor, then the first checkbox becomes visible (default unchecked) and has label Use as Numeric.

If checked, then it allows for the as.numeric() in the code above.
If checked a second checkbox becomes visible (default unchecked) with label Display as Factor. If checked it adds roughly the last line of code above to the command.

@lilyclements
Copy link
Contributor

@rdstern

a) @lilyclements could you look at the current geom_ribbon. I think it needs the fill parameter added, Maybe both as an aeshetic and a parameter. It seems the most obvious for ribbon and I wonder why it is missing. I don't see anything else missing?

If we have the fill parameter in the aes (as an aesthetic) then it will come up in the legend.
Any parameter value for fill that are a variable in the data should be in the aes.
Any parameter value for fill that is a string stating a colour should not be in the aes.
Here's an example that might perhaps explain it better (Note that "C" does not work!)

A <- ggplot() + geom_histogram(data = diamonds, aes(x = carat), fill = "blue") +
  labs(subtitle = "A")
B <- ggplot() + geom_histogram(data = diamonds, aes(x = carat, fill = "blue")) +
  labs(subtitle = "B")
# C <- ggplot() + geom_histogram(data = diamonds, aes(x = carat), fill = cut) +
#   labs(subtitle = "C")
D <- ggplot() + geom_histogram(data = diamonds, aes(x = carat, fill = cut)) +
  labs(subtitle = "D")

gridExtra::grid.arrange(A, B, D)

image

b) Then there is an "interesting" feature in this plot, namely that the x-axis is a factor, but the geom needs a numeric variable. That seems to me to be a general point, so perhaps we could add this option into the dialog?

This sounds like a good solution

@lilyclements
Copy link
Contributor

@rdstern there is another point I would want to add. That is that to run this plot, the "Column Summaries" dialog was first used to create the following variables:

max_sum_rain
value (the min)
p10_sum_rain
p33_sum_rain
p67_sum_rain
p90_sum_rain
median_sum_value.

But that is reasonably straight forward R code. My question is whether this is an option in the dialog, or if the user should have created these variables before? If created the variables before, they can specify how they want to treat NAs etc. But perhaps that is too complicated?

To give reproducible R code using the dodoma data:

# Import Dodoma Data:
new_RDS <- readRDS(file="C:/Program Files/R-Instat/0.7.6/static/Library/Climatic/Tanzania/dodoma.RDS")
data_book$import_RDS(data_RDS=new_RDS)

# Code generated by the dialog, Column Summaries
data_book$calculate_summary(data_name="dodoma", columns_to_summarise="rain", factors=c("year","month"), na.rm=TRUE, na_type=c(), j=1, summaries=c("summary_sum"), silent=TRUE)

# Code generated by the dialog, Column Summaries
data_book$calculate_summary(data_name="dodoma_by_year_month", columns_to_summarise="sum_rain", factors="month", na.rm=TRUE, na_type=c(), j=1, summaries=c("summary_min", "summary_max", "summary_median", "p10", "p33", "p67", "p90"), silent=TRUE)

dodoma_by_year_month_by_month <- data_book$get_data_frame("dodoma_by_year_month_by_month")
ggplot(data=dodoma_by_year_month_by_month, aes(x=as.numeric(month))) +
  geom_ribbon(aes(ymin=min_sum_rain, ymax=max_sum_rain), fill = "grey") +
  geom_ribbon(aes(ymin=p10_sum_rain, ymax=p90_sum_rain), fill = "blue") +
  geom_ribbon(aes(ymin=p33_sum_rain, ymax=p67_sum_rain), fill = "lightblue") +
  geom_line(aes(y=median_sum_rain), size=2.0, group = "grey", colour="black") +
  viridis::scale_fill_viridis() + theme_grey() +
  theme(plot.caption=element_text(size=8),
        plot.title=element_text(size=20),
        plot.subtitle=element_text(size=15),
        axis.text.x=element_text()) +
  labs(title="", subtitle="", caption="",
       x = "Month", y = "Rainfall") +
  scale_x_continuous(breaks = 1:12, labels = levels(dodoma_by_year_month_by_month$month)) 

@rdstern
Copy link
Collaborator Author

rdstern commented Jul 11, 2023

@lilyclements many thanks. I will reply at length later. For now @N-thony and maybe @MeSophie (who has done similar changes elsewhere?) I suggest an urgent small fix to geom_ribbon would be very useful, and ideally to include in the July update.

The geom_ribbon is very similar to geom_area. Both are already in R-Instat, but geom_ribbon is missing the fill aesthetic, or parameter. Please could it be added. It should be next to the colour one and I presume just the same as the fill checkbox/aes in geom_area and others.

Also please add Alpha into the parameters. It is there in the aesthetics, but missing in the parameters. Again, use geom area as an example that does include alpha, so that can perhaps be copied.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants