ggplot作图设置双坐标轴-各行其是

很多时候我们做柱状图组合折线图,或者双变量折线图,这时候需要设置双坐标轴,如果两组数据数值相差太大,会使得图的展示不合理,设置双坐标轴,各自使用各自坐标,则会对比明显。双坐标轴作图prism和original都可以实现,这里我们使用ggplot2实现。

读入数据并加载R包。

df<-read.csv("A.csv",header = T)
library(ggpubr)
library(scales)
library(ggplot2)
colnames(df)

作图。

ggplot(df, aes(y=M0_like,x=Diameter))+
  geom_line(color="#6FB585")+
  geom_point(size=3,color="#6FB585")+
  geom_line(aes(y=M1_like,x=Diameter), color="#E8BF80")+
  geom_point(aes(y=M1_like, x=Diameter),size=3,color="#E8BF80")

image.png

设置双坐标。


ggplot(df, aes(y=M0_like,x=Diameter))+
  geom_line(color="#6FB585")+
  geom_point(size=3,color="#6FB585")+
  geom_line(aes(y=M1_like,x=Diameter), color="#E8BF80")+
  geom_point(aes(y=M1_like, x=Diameter),size=3,color="#E8BF80")+
  scale_y_continuous(sec.axis = sec_axis(~.*1, name = 'M1_like'))+
  theme_bw()+
  theme(panel.grid.major=element_blank(),panel.grid.minor=element_blank())+
  theme(panel.border = element_blank())+
  theme(axis.line = element_line(colour = "black",size = 1))+
  theme_classic(base_size = 15)

image.png

虽然设置了双坐标,但是可以看出,右侧与左侧相同,黄色折线由于数值小,压的很低,看不出效果,所以需要调整。


ggplot(df, aes(y=M0_like,x=Diameter))+
  geom_line(color="#6FB585")+
  geom_point(size=3,color="#6FB585")+
  scale_y_continuous(expand = c(0,0),limits = c(0,6500),
                        sec.axis = sec_axis(~./5,
                                            name = 'M1_like',
                                            breaks = seq(0,1000,200)))+
  geom_line(aes(y=`M1_like`*5,x=Diameter), color="#E8BF80")+
  geom_point(aes(y=`M1_like`*5, x=Diameter),size=3,color="#E8BF80")+
  theme_bw()+
  theme(panel.grid.major=element_blank(),panel.grid.minor=element_blank())+
  theme(panel.border = element_blank())+
  theme(axis.line = element_line(colour = "black",size = 1))+
  theme_classic(base_size = 15)

image.png

这样就解决问题了!但是legend要添加!

觉得小编内容有用的、有意思的,点赞、分享、关注一下呗!更多精彩内容请关注我的公众号《KS科研分享与服务》!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值