2

I'm still pretty much a novice with R and ggplot. I have the following code

library(ggplot2)  
library(dplyr)    
library(tidyr)  

maxDate <- "2020-07-07"

my_dates <- function(d) {
  seq( d[1] + (wday(maxDate) - wday(d[1])+1) %% 7, d[2] + 6, by = "week")
}

stateWeekly <- #structure at https://pastebin.com/jT8WV4dy
endpoints <- stateWeekly %>% 
  group_by(state) %>%
  filter(weekStarting == max(weekStarting)) %>%
  select(weekStarting, posRate, state, cumRate, posRateChange) %>%
  ungroup()

g <- stateWeekly %>% ggplot(aes(x = as.Date(weekStarting))) +
  geom_col(aes(y=100*dailyTest), size=0.75, color="darkblue", fill="white") +
  geom_line(aes(y=cumRate), size = 0.75, color="red") +
  geom_line(aes(y=posRate), size = 0.75, color="forestgreen") +
  geom_point(data = endpoints,size = 1.5,shape = 21,
             aes(y = cumRate), color = "red", fill = "red", show.legend = FALSE) +
  geom_label(data=endpoints, aes(label=paste(round(cumRate,1),"%",sep=""),
                                 x=as.Date("2020-04-07", format="%Y-%m-%d"), y = 80), 
             color="red",
             show.legend = FALSE, 
             nudge_y = 12) +
  geom_label(data=endpoints, aes(label=paste(round(posRateChange,1),"%",sep=""),
                                 x=as.Date("2020-04-28", format="%Y-%m-%d"), y = 80), 
             color="forestgreen",
             show.legend = FALSE, 
             nudge_y = 12) +
  scale_y_continuous(name = "Cum Test Positivity Rate", 
                     sec.axis = sec_axis(~./100, name="Weekly % of Pop Tested")) +
  scale_x_date(breaks = my_dates, date_labels = "%b %d") +
  labs(x = "Week Beginning") +
  #title = "COVID-19 Testing",
  #subtitle = paste("Data as of", format(maxDate, "%A, %B %e, %y")),
  #caption = "HQ AFMC/A9A \n Data: The COVID Tracking Project (https://covidtracking.com)") +
  theme(plot.title = element_text(size = rel(1), face = "bold"),
        plot.subtitle = element_text(size = rel(0.7)),
        plot.caption = element_text(size = rel(1)),
        axis.text.y = element_text(color='red'),
        axis.title.y = element_text(color="red"),
        axis.text.y.right = element_text(color="blue"),
        axis.title.y.right = element_text(color="blue"),
        axis.text.x = element_text(angle = 45,hjust = 1),
        strip.background =element_rect(fill="white"),
        strip.text = element_text(colour = 'blue')) +
  #coord_cartesian(ylim=c(0,90)) +
  facet_wrap(~ state)


print(g)

Which produces this chart

enter image description here

Georgia has obviously been screwing with their COVID data (again) so nevermind the negative testing :)

What I'd like to do is scale the secondary axis so that the testing rate lines aren't so squished...they're very small numbers but I'd like to be able to see more differentiation. Any guidance on how to achieve that would be most appreciated.

Edit: One suggestion below was to change facet_wrap(~ state) to facet_wrap(~ state, scales='free') Doing so changes the chart only slightly

enter image description here

I can fix the label anchors, but this really didn't offer the level of differentiation in the line plots I was hoping for.

A second suggestion was the change sec.axis = sec_axis(~./100, name="Weekly % of Pop Tested")) to sec.axis = sec_axis(~./1000, name="Weekly % of Pop Tested"))

As far as I can tell, that does nothing to the actual plot and just changes the axis markings:

enter image description here

Finally, I've been struggling to implement the solution found here from Dag Hjermann. My secondary axis is the Weekly % of Population Tested, which is represented in the geom_col. A reasonable range for that is 0-1.1. The primary axis is the line plots, test positivity rates, which vary from 0-30. So if I follow that solution, I should add

ylim.prim <- c(0, 30)   
ylim.sec <- c(0, 1.1)

b <- diff(ylim.prim)/diff(ylim.sec)
a <- b*(ylim.prim[1] - ylim.sec[1])

and then change the plot code to read

geom_col(aes(y=a + 100*dailyTest*b), size=0.75, color="darkblue", fill="white")

and the secondary axis to

sec.axis = sec_axis(~ (. -a)/(b*100), name="Weekly % of Pop Tested"))

Doing so produces the following

enter image description here

which is clearly not right.

At the risk of sounding really dumb here, is the issue at least somewhat due to the line plots (what I want to scale) being on the primary axis?

jerH
  • 1,085
  • 1
  • 12
  • 30
  • Try adding `facet_wrap(~ state,scales='free')` – Duck Jul 08 '20 at 15:25
  • Hi check https://stackoverflow.com/questions/3099219/ggplot-with-2-y-axes-on-each-side-and-different-scales the second answer provided by Dag Hjermann – user12256545 Jul 08 '20 at 15:34
  • @Duck See edits to OP....that helped a little, but not really what I was trying to achieve – jerH Jul 09 '20 at 03:36
  • @user12256545 See edits to OP...struggling to figure out how to implement that solution. I think I'm just not "getting it" unfortunately. – jerH Jul 09 '20 at 03:37

1 Answers1

0

Maybe use permille instead of percentage.

scale_y_continuous(name = "Cum Test Positivity Rate", 
                     sec.axis = sec_axis(~./1000, name="Weekly ‰ of Pop Tested"))
user12256545
  • 2,755
  • 4
  • 14
  • 28