IDEMSInternational / R-Instat

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

Further improvements to Describe > Graphs > General #8675

Open rdstern opened 10 months ago

rdstern commented 10 months ago

@MeSophie this dialog now seems to be working well.

Recently @lilyclements provided some graphs and various issues come from trying to implement them;

image

This has lines (in different colours) and a ribbon. They are columns from the data attached (Dodoma).

I have 2 queries about this solution:

a) The code seems to add the same line for each layer. Does it need to do this. Sometimes I think a layer could use a different dataframe, in which case it does need to be added. But not here:

# Dialog: Graphics

dodoma_by_year_month_abbr_by_month_abbr <- data_book$get_data_frame(data_name="dodoma_by_year_month_abbr_by_month_abbr")
dodoma_by_year_month_abbr_by_month_abbr <- data_book$get_data_frame(data_name="dodoma_by_year_month_abbr_by_month_abbr")
dodoma_by_year_month_abbr_by_month_abbr <- data_book$get_data_frame(data_name="dodoma_by_year_month_abbr_by_month_abbr")
dodoma_by_year_month_abbr_by_month_abbr <- data_book$get_data_frame(data_name="dodoma_by_year_month_abbr_by_month_abbr")
dodoma_by_year_month_abbr_by_month_abbr <- data_book$get_data_frame(data_name="dodoma_by_year_month_abbr_by_month_abbr")
last_graph <- ggplot2::ggplot(data=dodoma_by_year_month_abbr_by_month_abbr, mapping=ggplot2::aes(x=month_abbr, y=max_sum_rain, group=1)) + ggplot2::geom_line() + ggplot2::geom_line(mapping=ggplot2::aes(y=min_sum_rain), data=dodoma_by_year_month_abbr_by_month_abbr, colour="blue") + ggplot2::geom_line(mapping=ggplot2::aes(y=p90_sum_rain), data=dodoma_by_year_month_abbr_by_month_abbr, colour="red") + ggplot2::geom_ribbon(mapping=ggplot2::aes(ymax=p67_sum_rain, ymin=p33_sum_rain), data=dodoma_by_year_month_abbr_by_month_abbr, fill="white") + ggplot2::geom_line(mapping=ggplot2::aes(y=p10_sum_rain), data=dodoma_by_year_month_abbr_by_month_abbr, colour="green") + theme_grey() + ggplot2::theme(legend.position="right")
data_book$add_object(data_name="dodoma_by_year_month_abbr_by_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="dodoma_by_year_month_abbr_by_month_abbr", object_name="last_graph", as_file=TRUE)
rm(list=c("last_graph", "dodoma_by_year_month_abbr_by_month_abbr")) 

b) I could not see how to add the legend? When there are multiple layers, that would be nice? How could that be done?

Here are the data:

dodoma_with_summaries.zip

I just realised I forgot the median. It is easy to add:

image

And here is the dialog:

image

@lilyclements this is one of the 3 ways I suggest we should be able to get this graph. I am having more trouble with the Line dialog - which is good - so we improve that too. But these 2 are each rather like the R code, in that we have to "write" it line by line. I suggest they makes a excellent case for the special dialog. It is a great example. Very useful. I am about to suggest it!

lilyclements commented 10 months ago

@rdstern some changes needed to your code (if you wanted to follow the same as the CIMH code)

You have run geom_line for p10_sum_rain and p90_sum_rain. But actually we want this to be as geom_ribbon with ymin=p10_sum_rain and ymax=p90_sum_rain

This is an instance where the order matters - you need to do the ribbon first, because of layers. The ribbon means a fill between the min and max point. Putting this on top of a line means that the line is covered over. This also means that we want to do the biggest ribbon first (i.e., the p10_sum_rain ad p90_sum_rain first)

ggplot2::ggplot(data=dodoma_by_year_month_abbr_by_month_abbr, mapping=ggplot2::aes(x=month_abbr, group=1)) +
  ggplot2::geom_ribbon(mapping=ggplot2::aes(ymin=p10_sum_rain, ymax=p90_sum_rain), data=dodoma_by_year_month_abbr_by_month_abbr, fill="pink") +
  ggplot2::geom_ribbon(mapping=ggplot2::aes(ymax=p67_sum_rain, ymin=p33_sum_rain), data=dodoma_by_year_month_by_month, fill="white") +
  ggplot2::geom_line(mapping=ggplot2::aes(y=min_sum_rain), data=dodoma_by_year_month_abbr_by_month_abbr, colour="blue") +
  ggplot2::geom_line(mapping=ggplot2::aes(y=max_sum_rain), data=dodoma_by_year_month_abbr_by_month_abbr, colour="blue") +
  ggplot2::geom_line(mapping=ggplot2::aes(y=median_sum_rain), data=dodoma_by_year_month_abbr_by_month_abbr, colour="red") +
  theme_grey() +
  ggplot2::theme(legend.position="right")

From this some suggestions are

  1. Can we have a reorder button for the different geoms in the "Layers" bit? Or is this too complex? It might make things a bit easier when you create a plot because for this instance, the order matters
  2. Agreed we don't like having dodoma_by_year_month_abbr_by_month_abbr <- ... repeated every time. Do we have a suggestion on how to avoid this? We have this occur in other dialogs too. I suggest we want this as it's own issue?
  3. Good point about the legend. This is the difference between running the fill or colour argument in the aes vs not in the aes. To get this up in the legend, it has to be run in the aes. You can then rename using scale_fill_identity and scale_colour_identity

To point 3 (or your b):

To get the colour/fill argument in the legend we want to have it with the aesthetics in the aes code:

ggplot2::ggplot(data=dodoma_by_year_month_abbr_by_month_abbr, mapping=ggplot2::aes(x=month_abbr, group=1)) +
  ggplot2::geom_ribbon(mapping=ggplot2::aes(ymin=p10_sum_rain, ymax=p90_sum_rain, fill="pink"), data=dodoma_by_year_month_abbr_by_month_abbr) +
  ggplot2::geom_ribbon(mapping=ggplot2::aes(ymax=p67_sum_rain, ymin=p33_sum_rain, fill="white"), data=dodoma_by_year_month_by_month) +
  ggplot2::geom_line(mapping=ggplot2::aes(y=min_sum_rain, colour="blue"), data=dodoma_by_year_month_abbr_by_month_abbr) +
  ggplot2::geom_line(mapping=ggplot2::aes(y=max_sum_rain, colour="blue"), data=dodoma_by_year_month_abbr_by_month_abbr) +
  ggplot2::geom_line(mapping=ggplot2::aes(y=median_sum_rain, colour="red"), data=dodoma_by_year_month_abbr_by_month_abbr) +
  theme_grey() +
  ggplot2::theme(legend.position="right") +
  scale_fill_identity(name = NULL, guide = "legend",
                      labels = c('Normal (Between Lower and Upper Tercile)','Range 10th-90th Percentile')) +
  scale_colour_identity(name = NULL, guide = "legend",
                      labels = c("Record Low/High", "Median"))
image

I can look into if there's another way to add a legend without having to move it into the aes part of the code.

MeSophie commented 6 months ago

@rdstern , @lilyclements I think it would be a good to create a legend group with two radios buttons legend position and scale identity. If legend position is ticked we'll have our traditional legend position, if it's scale identity then we'll have two checked boxes one for fill and the other for colour, and input text control for the label of each of these boxes. What do you think please?

lilyclements commented 5 months ago

@MeSophie I'm not sure if I'm understanding correctly

Are you saying that one of the radio buttons doesn't run this bit, but the other would?

  scale_fill_identity(name = NULL, guide = "legend",
                      labels = c('Normal (Between Lower and Upper Tercile)','Range 10th-90th Percentile')) +
  scale_colour_identity(name = NULL, guide = "legend",
                      labels = c("Record Low/High", "Median"))

I think we always want these scale_*_identity's because of how we colour bits in the R code. Without it, we'd have very meaningless labels - if I'm understanding correctly. E.g., try running -

ggplot2::ggplot(data=dodoma_by_year_month_abbr_by_month_abbr, mapping=ggplot2::aes(x=month_abbr, group=1)) +
  ggplot2::geom_ribbon(mapping=ggplot2::aes(ymin=p10_sum_rain, ymax=p90_sum_rain, fill="pink"), data=dodoma_by_year_month_abbr_by_month_abbr) +
  ggplot2::geom_ribbon(mapping=ggplot2::aes(ymax=p67_sum_rain, ymin=p33_sum_rain, fill="white"), data=dodoma_by_year_month_by_month) +
  ggplot2::geom_line(mapping=ggplot2::aes(y=min_sum_rain, colour="blue"), data=dodoma_by_year_month_abbr_by_month_abbr) +
  ggplot2::geom_line(mapping=ggplot2::aes(y=max_sum_rain, colour="blue"), data=dodoma_by_year_month_abbr_by_month_abbr) +
  ggplot2::geom_line(mapping=ggplot2::aes(y=median_sum_rain, colour="red"), data=dodoma_by_year_month_abbr_by_month_abbr) +
  theme_grey() +
  ggplot2::theme(legend.position="right") 

Perhaps I've misunderstood though?