如何制作双Y轴图

2019-07-29  本文已影响0人  见龙在田007er2770

想制作这样的图吗?


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 = "")}))


参考

如果不懂,欢迎留言。

上一篇下一篇

猜你喜欢

热点阅读