R ggplot次轴缩放
我对R和ggplot还是个新手。我有以下代码R ggplot次轴缩放,r,ggplot2,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 <-
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)
然后将绘图代码更改为
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"))
这样做会产生以下结果
这显然是不对的
在这里听起来很愚蠢的风险是,这个问题至少在某种程度上是由于直线图(我想缩放的)位于主轴上吗?可能使用Permile而不是百分比
scale_y_continuous(name = "Cum Test Positivity Rate",
sec.axis = sec_axis(~./1000, name="Weekly ‰ of Pop Tested"))
可能使用Permile而不是percentage
scale_y_continuous(name = "Cum Test Positivity Rate",
sec.axis = sec_axis(~./1000, name="Weekly ‰ of Pop Tested"))
尝试添加
facet\u wrap(~state,scales='free')
Hi检查Dag提供的第二个答案Hjermann@Duck请参阅对OP.的编辑…这有点帮助,但并不是我想要的achieve@user12256545请参阅对OP…的编辑,努力找出如何实现该解决方案。不幸的是,我想我没有“理解”。请尝试添加facet\u wrap(~state,scales='free')
Hi检查Dag提供的第二个答案Hjermann@Duck请参阅对OP.的编辑…这有点帮助,但并不是我想要的achieve@user12256545请参阅对OP…的编辑,努力找出如何实现该解决方案。不幸的是,我想我没有“明白”。