想制作这样的图吗?
double_y_axis.jpeg
啥也不说,直接放double_y_axis
代码:
Market_Size
# A tibble: 7 x 3
year size speed
<dbl> <dbl> <dbl>
1 2009 44.4 NA
2 2010 58.9 32.8
3 2011 89.4 51.6
4 2012 117. 31.1
5 2013 163. 39.3
6 2014 199. 21.8
7 2015 236. 18.8
library(ggplot2)
library(scales)
ggplot() +
geom_bar(data = Market_Size, aes(x = year, y = size), stat="identity", colour="gray", size=1) +
geom_line(data = Market_Size, aes(x = year, y = speed * 3 ), colour = "red", size = 1.5) +
scale_y_continuous("Market Size\n(billion rmb)",
sec.axis = sec_axis( ~ ./3 ,
name = "Growing speed",
labels = function(b) {paste(b, "%", sep = "")})) +
scale_x_continuous("Year", breaks = 2009:2015) +
ggtitle("Market size and growing speed \nof Health care products in China" ) +
theme(plot.title = element_text(lineheight=1, face="bold", color="black",size=24, hjust = 0.5))
关键在于sec_axis
函数。
sec.axis = sec_axis( ~ ./3 ,
name = "Growing speed",
labels = function(b) {paste(b, "%", sep = "")}))
如果不懂,欢迎留言。
网友评论