ggplot2双纵坐标轴

library("scales")
library(tidyverse)
df4 <- tibble(
  year = c("2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020"),
  ResearchInput = c(1.56, 2.54, 2.99, 5.63, 11.08, 12.31, 16.77, 25.92),
  ProportioninOperatingIncome = c(6.8, 6.9, 5, 4.9, 6.77, 5.6, 5.1, 4.75)
)

df4$year <- as.factor(df4$year)
df4$ProportioninOperatingIncome1 <- rescale(df4$ProportioninOperatingIncome, c(4.75, 28))
gg5 <- ggplot() +
  geom_col(data = df4, mapping = aes(x = year, y = ResearchInput), fill = '#1ec0ff', width = 0.6) +
  geom_line(data = df4, mapping= aes(x = year, y = ProportioninOperatingIncome1, group = 1), size = 1.2, colour = "#6441A4", linetype="longdash") +
  geom_point(df4, mapping = aes(x = year, y = ProportioninOperatingIncome1), size = 2.5, colour = "#4F86C6") +
  scale_y_continuous(
    breaks = seq(0, 28, 4),
    sec.axis = sec_axis(~rescale(.,c(4.5,7.5)),
                        labels = sprintf("%g%%",seq(4.5, 7.5, 0.5)))
  )

gg6 <- gg5 +
  geom_text(data = df4, mapping = aes(x = year,y = ResearchInput + 0.6,label = ResearchInput), color = "#1ec0ff") +
  geom_text(data = df4, mapping = aes(x = year,y = ProportioninOperatingIncome1 + 1.5,label = sprintf("%g%%", ProportioninOperatingIncome)), colour = "#4F86C6")

gg7 <- gg6 + theme(axis.ticks = element_line(linetype = "blank"),
    axis.text = element_text(size = 12),
    panel.background = element_rect(fill = NA)) +labs(x = NULL, y = NULL) +
  geom_hline(yintercept = 0, colour = "#DCDDD8")

在这里插入图片描述

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值