Open rdstern opened 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
geom_ribbon
you want to do this for
ymin=p10_sum_rain
and ymax=p90_sum_rain
ymin=p33_sum_rain
and ymax=p67_sum_rain
geom_line
you want to do this for
min_sum_rain
max_sum_rain
median_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
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?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"))
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.
@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?
@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?
@MeSophie this dialog now seems to be working well.
Recently @lilyclements provided some graphs and various issues come from trying to implement them;
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:
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:
And here is the dialog:
@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!