我有double-y-axis
图表制作Excel
.在Excel中,它只需要基本技能.我想做的是使用ggplot2
库复制此图表R
.
我已经这样做了,但我需要绘制Response 2nd-y-axis
.
我附上了我用过的可重现的代码:
#Data generation Year <- c(2014, 2015, 2016) Response <- c(1000, 1100, 1200) Rate <- c(0.75, 0.42, 0.80) df <- data.frame(Year, Response, Rate) #Chart library(ggplot2) ggplot(df) + geom_bar(aes(x=Year, y=Response),stat="identity", fill="tan1", colour="sienna3")+ geom_line(aes(x=Year, y=Rate),stat="identity")+ geom_text(aes(label=Rate, x=Year, y=Rate), colour="black")+ geom_text(aes(label=Response, x=Year, y=0.9*Response), colour="black")
setempler.. 23
首先,规模Rate
由Rate*max(df$Response)
和修改0.9
响应文本的规模.
其次,包括第二轴scale_y_continuous(sec.axis=...)
:
ggplot(df) + geom_bar(aes(x=Year, y=Response),stat="identity", fill="tan1", colour="sienna3")+ geom_line(aes(x=Year, y=Rate*max(df$Response)),stat="identity")+ geom_text(aes(label=Rate, x=Year, y=Rate*max(df$Response)), colour="black")+ geom_text(aes(label=Response, x=Year, y=0.95*Response), colour="black")+ scale_y_continuous(sec.axis = sec_axis(~./max(df$Response)))
产量:
首先,规模Rate
由Rate*max(df$Response)
和修改0.9
响应文本的规模.
其次,包括第二轴scale_y_continuous(sec.axis=...)
:
ggplot(df) + geom_bar(aes(x=Year, y=Response),stat="identity", fill="tan1", colour="sienna3")+ geom_line(aes(x=Year, y=Rate*max(df$Response)),stat="identity")+ geom_text(aes(label=Rate, x=Year, y=Rate*max(df$Response)), colour="black")+ geom_text(aes(label=Response, x=Year, y=0.95*Response), colour="black")+ scale_y_continuous(sec.axis = sec_axis(~./max(df$Response)))
产量: