在ggplot(R)上显示两个平行轴 [英] Display two parallel axes on a ggplot (R)

查看:146
本文介绍了在ggplot(R)上显示两个平行轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  library(ggplot2)
df =数据.frame(y = c(0,1.1,2.3,3.1,2.9,5.8,6,7.4,8.2,9.1),x = seq(1,100,length.out = 10))
ggplot(df,aes (x = x,y = y))+ geom_point()

x z 完全相关。关系是: Constant = x ^ 2 * z = 1.23
因此我可以像这样重写data.frame:

  df = cbind(df,1.23 / df $ x ^ 2)






问题是:

如何显示两个变量 x z 一个x轴?可以是一个在底部,一个在顶部图表或两者都在底部。

解决方案

这是一个危险的尝试。以前版本的日志规模仅为错误

  library(ggplot2)
df = data.frame(y = c(0,1.1,2.3,3.1,2.9,5.8,6,7.4,8.2,9.1),
x = seq(1,100,length.out = 10))
df $ z = 1.23 / df $ x ^ 2


##让我们至少删除网格线
p1 < - ggplot(df,aes(x = x, y = y))+ geom_point()+
scale_x_continuous(expand = c(0,0))+
theme(panel.grid.major = element_blank(),
panel.grid。 minor = element_blank())

##确保两个图都有expand = c(0,0)
##否则数据和顶轴不一定会对齐...
p2 < - ggplot(df,aes(x = z,y = y))+ geom_point()+
scale_x_continuous(expand = c(0,0))
$ b $ (gtable)
g1 < - ggplotGrob(p1)
g2 < - ggplotGrob(p2)
tmp< - gtable_filter(g2,pattern =axis-b)

##丑陋的技巧来提取和重塑轴
轴< - tmp [[grobs]] [[1]] [[children]] [[axis ]]#腐败儿童
轴$ layout < - axis $ layout [2:1,]
轴$ grobs [[1]] [[y]] < - axis $ grobs [[1]] [ [y]] - 单位(1,npc)+单位(0.15,cm)
##返回正常

g1 < - gtable_add_rows(g1 ,sum(tmp $ heights),2)
gtableAddGrobs< - gtable_add_grob#alias,确保@!hadley没有看到这个
g1< - gtableAddGrobs(g1,
grobs = list(gtable_filter(g2,pattern =xlab),axis),
t = c(1,3),l = 4)
grid.newpage()
grid.draw(g1 )


Let's say we have a simple plot of the following kind.

library(ggplot2)
df = data.frame(y=c(0,1.1,2.3,3.1,2.9,5.8,6,7.4,8.2,9.1),x=seq(1,100, length.out=10))
ggplot(df,aes(x=x,y=y)) + geom_point()

x perfectly correlates with z. The relation is: Constant=x^2*z=1.23 therefore I could rewrite the data.frame like this:

df = cbind(df,1.23/df$x^2)


The question is:

How can I display both variables xand zone the x-axis? It could be one at the bottom and one at the top of the graph or both at the bottom.

解决方案

Here's a dangerous attempt. Previous version with a log-scale was just wrong.

library(ggplot2)
df = data.frame(y=c(0,1.1,2.3,3.1,2.9,5.8,6,7.4,8.2,9.1),
                x=seq(1,100, length.out=10))
df$z = 1.23/df$x^2


## let's at least remove the gridlines
p1 <- ggplot(df,aes(x=x,y=y)) + geom_point() +
  scale_x_continuous(expand=c(0,0)) +
  theme(panel.grid.major=element_blank(),
        panel.grid.minor = element_blank())

## make sure both plots have expand = c(0,0) 
## otherwise data and top-axis won't necessarily be aligned...
p2 <- ggplot(df,aes(x=z,y=y)) + geom_point() +
  scale_x_continuous(expand=c(0,0))

library(gtable)
g1 <- ggplotGrob(p1)
g2 <- ggplotGrob(p2)
tmp <- gtable_filter(g2, pattern="axis-b")

## ugly tricks to extract and reshape the axis
axis <- tmp[["grobs"]][[1]][["children"]][["axis"]] # corrupt the children
axis$layout <- axis$layout[2:1,]
axis$grobs[[1]][["y"]] <- axis$grobs[[1]][["y"]] - unit(1,"npc") + unit(0.15,"cm")
## back to "normality"    

g1 <- gtable_add_rows(g1, sum(tmp$heights), 2)
gtableAddGrobs <- gtable_add_grob # alias, making sure @!hadley doesn't see this
g1 <- gtableAddGrobs(g1, 
                     grobs=list(gtable_filter(g2, pattern="xlab"),axis), 
                     t=c(1,3), l=4)
grid.newpage()
grid.draw(g1)

这篇关于在ggplot(R)上显示两个平行轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆