ggplot辅助轴缩放

时间:2020-07-08 15:21:56

标签: r ggplot2

我仍然是R和ggplot的新手。我有以下代码

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)

哪个会生成此图表

enter image description here

乔治亚州显然一直在弄乱自己的COVID数据,所以不要介意负面测试:)

我想做的是缩放副轴,以免测试速率线被压扁...它们的数量很小,但我希望能够看到更多差异。任何有关如何实现这一目标的指导将不胜感激。

编辑: 下面的一个建议是将facet_wrap(~ state)更改为facet_wrap(~ state, scales='free'),这样做只是稍微改变了图表

enter image description here

我可以修复标签锚点,但这确实没有提供我希望的线条图中的差异化水平。

第二个建议是将sec.axis = sec_axis(~./100, name="Weekly % of Pop Tested"))更改为sec.axis = sec_axis(~./1000, name="Weekly % of Pop Tested"))

据我所知,这对实际绘图没有任何作用,只是更改了轴标记:

enter image description here

最后,我一直在努力实现Dag Hjermann提供的here解决方案。我的第二轴是每周测试人口百分比,在geom_col中表示。合理的范围是0-1.1。主轴是线图,测试阳性率,范围是0-30。因此,如果我遵循该解决方案,则应该添加

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])

然后将绘图代码更改为

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

和辅助轴

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

这样做会产生以下结果

enter image description here

这显然是不对的。

在这里听起来真的很蠢,这个问题至少在某种程度上是由于线图(我要缩​​放的比例)位于主要轴上吗?

1 个答案:

答案 0 :(得分:0)

也许用Permille代替百分比。

scale_y_continuous(name = "Cum Test Positivity Rate", 
                     sec.axis = sec_axis(~./1000, name="Weekly ‰ of Pop Tested"))
相关问题