ukhsa-collaboration / fingertipscharts

11 stars 6 forks source link

Legend Produced From Trend Function. #15

Closed Woodhouse2 closed 4 years ago

Woodhouse2 commented 4 years ago

I have been using the Trends function within this package, but have had an issue with the legend.

I am unable to get the legend to display a "legend" for the area_name (in this case "Norfolk") alongside the one for "England".

Is there a way to do this at all?

I have copied my current code in below:

HData = fingertips_data(IndicatorID = 11201, AreaTypeID = 102)

HData_Peoples = HData %>% filter(HData$Sex == "Persons") # Ensures that data is not "triple counted" for males, females and all people. 

trends (
HData_Peoples, # IndicatorID = 11201 : Violent crime (including sexual violence) - hospital admissions for violence [Norfolk and England] (Source: Block 2)
timeperiod = Timeperiod,
value = Value,
area = AreaName,
comparator = "England",
area_name = "Norfolk",
fill = ComparedtoEnglandvalueorpercentiles, # Colours the circles depending on whether better or worse than the England average. 
title = "Violent Crime (including sexual violence)",
subtitle = "Hospital Admissions for Violence per 100,000 Population",
ylab = "Admissions Per 100,000 Population",
xlab = "3 Year Period (Financial Years)",
point_size = 3,
lowerci = LowerCI95.0limit,
upperci = UpperCI95.0limit
) + 
theme (
axis.text = (element_text(size = 8,)),
axis.title = (element_text(size = 9, colour = "black")),
plot.title = (element_text(hjust = 0.5, size = 11)),
plot.subtitle = (element_text(hjust = 0.5, size = 9, colour = "black")),
legend.text = (element_text(colour = "black")),
panel.grid.major.y = (element_line(colour = "dark grey", linetype = "dashed")),
panel.grid.minor.y = (element_line(colour = "grey", linetype = "dotted")),
) +
ylim(21,65)
catriggwalk commented 4 years ago

I notice that the example in the package documentation places the name of the area being compared in the chart subtitle. I am assuming from this that the trends function does not currently allow for the area name to be included in the label. Thus, one work around would be to create the area name and subtitle as objects/values before the trends() function, then use the labs() function from ggplot2 to use that subtitle value as the sub-heading. I have added # comments in the code below to indicate where my changes to your code start and end.

library(dplyr)
library(fingertipsR)
library(fingertipscharts)
library(ggplot2)

HData = fingertips_data(IndicatorID = 11201, AreaTypeID = 102)

HData_Peoples = HData %>% filter(HData$Sex == "Persons") # Ensures that data is not "triple counted" for males, females and all people.

# Addition
area <- "Norfolk"
chart_subhead <- paste("Hospital Admissions for Violence per 100,000 Population,", area)
# Addition ends

trends (
  HData_Peoples, # IndicatorID = 11201 : Violent crime (including sexual violence) - hospital admissions for violence [Norfolk and England] (Source: Block 2)
  timeperiod = Timeperiod,
  value = Value,
  area = AreaName,
  comparator = "England",
  area_name = area,
  fill = ComparedtoEnglandvalueorpercentiles, # Colours the circles depending on whether better or worse than the England average.
  title = "Violent Crime (including sexual violence)",
  subtitle = NULL,
  ylab = "Admissions Per 100,000 Population",
  xlab = "3 Year Period (Financial Years)",
  point_size = 3,
  lowerci = LowerCI95.0limit,
  upperci = UpperCI95.0limit
) +
  theme(
    axis.text = (element_text(size = 8,)),
    axis.title = (element_text(size = 9, colour = "black")),
    plot.title = (element_text(hjust = 0.5, size = 11)),
    plot.subtitle = (element_text(hjust = 0.5, size = 9, colour = "black")),
    legend.text = (element_text(colour = "black")),
    panel.grid.major.y = (element_line(colour = "dark grey", linetype = "dashed")),
    panel.grid.minor.y = (element_line(colour = "grey", linetype = "dotted")),
  ) +
# Addition
  labs(subtitle = chart_subhead) +
# Addition ends
  ylim(21,65)
sebastian-fox commented 4 years ago

Hi @Woodhouse2 and @DavidNphe - thanks for the messages. Originally, I developed the package so it produced outputs identical to the Fingertips website. For the trends tab, only England is displayed in the legend.

I think in this case it makes sense to display both areas. I've made an update to the development version of the package. Please could you install it and make sure it is doing what you're expecting. If so, please could you close this issue?

To install it run:

devtools::install_github("PublicHealthEngland/fingertipscharts",
                         build_vignettes = TRUE)
catriggwalk commented 4 years ago

Thanks @sebastian-fox . It works for me. If I was being really pedantic I would like to have the area_name appearing before the comparator in the legend, rather than the comparator label appearing first but this is far from critical. As @Woodhouse2 raised the issue I will leave it with her to close this if she is happy.