ggplot2中的辅助轴刻度

时间:2020-09-16 15:00:35

标签: r ggplot2

是的,我知道您“不应”使用次级比例尺,因为它“不正确”,但是我的导师仍然希望这两个图形在同一图中,并且我需要添加次级比例尺...编写代码,但是问题出在我需要定义新的比例尺时。我需要将50加到主要值上,然后将所有内容除以.15,如此处

P <- ggplot(data=dd)+
  geom_line(aes(x=dS, y=(nS*.15)-50), color="blue3", size=.25)+
  geom_line(aes(x=dO, y=cO), color="red", size=.25)+
  geom_vline(xintercept = dd$dS[dd$Year %% 5 == 0], color="black", linetype="dashed", size=.25)+
  scale_y_continuous(sec.axis = sec_axis(([~. +50] / 0.15), name = "nss Sulphate (ppb)"), limits=c(-50,-20))+
  theme_bw()+
  labs(x="Depth (m)", y=expression(paste(delta^{18}, "O (‰)")))
P

但是我收到“二进制运算符的非数字参数”错误。我尝试了另一种括号,但似乎没有任何作用。

有什么想法吗?

编辑: 虚拟数据集

dS<-c(1:100)
dO<-c(1:100)
nO<-runif(100, min=-36, max=-25)
nS<-runif(100, min=0, max=150)
Year<-c(1900:2000)
dd<-data.frame(dS,dO,nO,nS,Year)

1 个答案:

答案 0 :(得分:0)

我猜这行y = nS是因为没有cO。要解决此错误,请使用(~./0.15 +50/0.15)

这应该有效。

dS<-c(1:100)
dO<-c(1:100)
nO<-runif(100, min=-36, max=-25)
nS<-runif(100, min=0, max=150)
Year<-c(1900:1999) #df does not have the same number of observations.
dd<-data.frame(dS,dO,nO,nS,Year)

P <- ggplot(data=dd)+
  geom_line(aes(x=dS, y=(nS*.15)-50), color="blue3", size=.25)+
  geom_line(aes(x=dO, y=nS), color="red", size=.25)+ #y = nS
  geom_vline(xintercept = dd$dS[dd$Year %% 5 == 0], color="black", linetype="dashed", size=.25)+
  scale_y_continuous(sec.axis = sec_axis((((~./0.15 +50/0.15))), name = "nss Sulphate (ppb)"), limits=c(-50,-20))+
  theme_bw()+
  labs(x="Depth (m)", y=expression(paste(delta^{18}, "O (‰)")))
P
相关问题