本文介绍了ggplot2:在绘图顶部添加二次转换的x轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 [ 2016年4月编辑:)此线程中的解决方案不再正确显示添加的坐标轴 - 此问题的新线程已在 ggplot2 2.1.0破坏了我的代码?二次转换轴现在显示不正确] 我正在处理缩放的x数据,并且需要在绘图顶部添加一个未缩放的x轴以方便解释。我遇到了一种方法来在>我怎样才能把一个变换的比例放在ggplot2的右边?。但是,我无法让它在x轴上正常工作。我确定我不了解代码的某些部分,但我似乎无法弄清楚它是什么。我已经试过在ggplot2帮助文件中寻找,还有Wickham书籍ggplot2:用于数据分析的优雅图形,但是如果任何人都可以指向我的某些相关文档,我将非常感激! 我正在处理温度数据,但是我将使用上面链接中的lake数据,因为代码是为此编写的。 图书馆(ggplot2)图书馆(gtable)图书馆(grid) LakeLevels< -data.frame(Day = c(1:365),Elevation = sin(seq(0,2 * pi,2 * pi / 364))* 10 + 100)$ b $ (p) scale_y_continuous(name =Elevation(m),limits = c(75,125)) p2 scale_y_continuous(name =Elevation(ft),limits = c(75,125 ), breaks = c(80,90,100,110,120), labels = c(262,295,328,361,394)) #extract gtable g1< -ggplot_gtable(ggplot_build(p1)) g2< -ggplot_gtable(ggplot_build(p2)) #覆盖第二幅图的面板(g1 $ layout,name ==panel,se = t:r)) g pp $ l) ia< - 其中(g2 $ layout $ name ==axis-l) ga ax< - ga $ children [[2] ] ax $ widths ax $ grobs< - rev(ax $ grobs) ax $ grobs [[1]] $ x< - ax $ grobs [[1]] $ x - unit(1,npc)+ unit(0.15,cm)g grid.draw(g) 测试一种添加x轴代替y轴,我切换了x和y轴,并将 axis-l 更改为 axis-b 给: 库(ggplot2)库(gtable)库(网格) LakeLevels p1 scale_x_continuous(name =Elevation(m),limits = c(75,125)) p2 break = c(80,90,100,110,120), labels = c (262,295,328,361,394)) #extract gtable g1< -ggplot_gtable(ggplot_build(p1)) g2 #覆盖第一幅图的第二幅图的面板 pp< -c(子集(g1 $布局,名称==panel,se = t:r)) g pp $ l) ia< - 其中(g2 $ layout $ name ==axis-b) ga < - g2 $ grobs [[ia]] ax< - ga $ children [[2]] ax $ widths< - rev(ax $ widths) ax $ grobs < - rev(ax $ grobs) ax $ grobs [[1]] $ x g g #绘制 grid.draw(g) 这会产生一个新的x轴,但有几个问题: 1)它位于图的中间 2)它不会为Elevation(ft)生成新的x标签) 我需要轴出现在图的顶部,我需要一个关联的轴标签。谁能告诉我我做错了什么? b $ b 另外,如上所述,我正在使用缩放温度数据,所以理想情况是滴答不会像在这个例子中那样在顶部和底部的轴上对齐。有什么办法可以在ggplot2中做到这一点?网上的一个任意例子是这样的: 解决方案问题的根源是你修改列而不是行。 设置,在第二个图的X轴上标有缩放标签: ##'base'plot p1 scale_x_continuous(name =Elevation(m),limits = c(75,125)) ##用变换轴绘制 p2 scale_x_continuous(name =Elevation(ft),limits = c(75,125), breaks = c(90,101,120), labels = round(c(90,101,120)* 3.24084)##标签转换为脚) ##提取gtable g1 g2 ##将第二个图的面板与第一个图的面板重叠 pp< - c(subset(g1 $ layout,name ==panel,se = t:r)) g pp $ l) EDIT 使网格线与下轴线对齐,将上面的线条替换为: g ##从第二个绘图中窃取轴并修改 ia< - 其中(g2 $ layout $ name ==axis-b) ga ax 现在,您需要确保您正在修改正确的维度。由于新轴是水平的(一行而不是一列),所以 whatever_grob $ heights 是要修改的向量,以更改给定行中的垂直空间量。如果您想添加新空间,请确保添加一行而不是一列(即使用 gtable_add_rows())。 如果您自己修改grobs(在这种情况下,我们正在更改滴答的垂直对齐),请务必修改 y (垂直位置)而不是 x (水平位置)。 ##蜱和标签的开关位置 ax $ heights ax $ grobs< - rev(ax $ grobs) ax $ grobs [[2]] $ y ##修改现有的行对于轴高度足够高g $ heights [[2]] ##添加新轴g ##为上轴标签添加新行g g #绘制 grid.draw(g) 我将通过 gtable_show_layout( )是一个非常非常方便的功能,用于确定发生了什么。 [ Edit April 2016: the solution in this thread no longer displays the added axis correctly - a new thread on this issue has been opened at ggplot2 2.1.0 broke my code? Secondary transformed axis now appears incorrectly ]I am working with scaled x data, and need to add an unscaled x axis to the top of the plot for easier interpretation. I've come across an approach for adding a secondary a y-axis at How can I put a transformed scale on the right side of a ggplot2?. However, I can't get it to work properly for the x-axis. I'm sure i'm not understanding some part of the code, but I can't seem to figure out what it is. I've tried looking in the ggplot2 help files, and also the Wickham book ggplot2: Elegant Graphics For Data Analysis, but if anyone can point me towards some relevant documentation I would really appreciate it!I am working with temperature data, but I will use the lake data from the link above as the code was written for that. Here is the orignal code from that link:library(ggplot2)library(gtable)library(grid)LakeLevels<-data.frame(Day=c(1:365),Elevation=sin(seq(0,2*pi,2*pi/364))*10+100)p1 <- ggplot(data=LakeLevels) + geom_line(aes(x=Day,y=Elevation)) + scale_y_continuous(name="Elevation (m)",limits=c(75,125))p2<-ggplot(data=LakeLevels)+geom_line(aes(x=Day, y=Elevation))+ scale_y_continuous(name="Elevation (ft)", limits=c(75,125), breaks=c(80,90,100,110,120), labels=c("262", "295", "328", "361", "394"))#extract gtableg1<-ggplot_gtable(ggplot_build(p1))g2<-ggplot_gtable(ggplot_build(p2))#overlap the panel of the 2nd plot on that of the 1st plotpp<-c(subset(g1$layout, name=="panel", se=t:r))g<-gtable_add_grob(g1, g2$grobs[[which(g2$layout$name=="panel")]], pp$t, pp$l, pp$b, pp$l)ia <- which(g2$layout$name == "axis-l")ga <- g2$grobs[[ia]]ax <- ga$children[[2]]ax$widths <- rev(ax$widths)ax$grobs <- rev(ax$grobs)ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)# draw itgrid.draw(g)To test a method for adding an x-axis instead of y-axis, I switched the x and y axes and changed axis-l to axis-b to give:library(ggplot2)library(gtable)library(grid)LakeLevels<-data.frame(Day=c(1:365),Elevation=sin(seq(0,2*pi,2*pi/364))*10+100)p1 <- ggplot(data=LakeLevels) + geom_line(aes(x=Elevation,y=Day)) + scale_x_continuous(name="Elevation (m)",limits=c(75,125))p2<-ggplot(data=LakeLevels)+geom_line(aes(x=Elevation, y=Day))+scale_x_continuous(name="Elevation (ft)", limits=c(75,125), breaks=c(80,90,100,110,120), labels=c("262", "295", "328", "361", "394"))#extract gtableg1<-ggplot_gtable(ggplot_build(p1))g2<-ggplot_gtable(ggplot_build(p2))#overlap the panel of the 2nd plot on that of the 1st plotpp<-c(subset(g1$layout, name=="panel", se=t:r))g<-gtable_add_grob(g1, g2$grobs[[which(g2$layout$name=="panel")]], pp$t, pp$l, pp$b, pp$l)ia <- which(g2$layout$name == "axis-b")ga <- g2$grobs[[ia]]ax <- ga$children[[2]]ax$widths <- rev(ax$widths)ax$grobs <- rev(ax$grobs)ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)# draw itgrid.draw(g)This produces a new x-axis, but there are a couple of problems:1) it is in the middle of the plot2) it does not produce a new x-label for "Elevation (ft)"I need the axis to appear at the top of the plot, and I need an associated axis label. Can anyone tell me what I am doing wrong?Also, as mentioned above, I am working with scaled temperature data, so ideally the ticks would not align on the top and bottom axes as they do in this example. Is there any way to do this in ggplot2? An arbitrary example from the web is this: 解决方案 The root of your problem is that you are modifying columns and not rows.The setup, with scaled labels on the X-axis of the second plot:## 'base' plotp1 <- ggplot(data=LakeLevels) + geom_line(aes(x=Elevation,y=Day)) + scale_x_continuous(name="Elevation (m)",limits=c(75,125))## plot with "transformed" axisp2<-ggplot(data=LakeLevels)+geom_line(aes(x=Elevation, y=Day))+ scale_x_continuous(name="Elevation (ft)", limits=c(75,125), breaks=c(90,101,120), labels=round(c(90,101,120)*3.24084) ## labels convert to feet )## extract gtableg1 <- ggplot_gtable(ggplot_build(p1))g2 <- ggplot_gtable(ggplot_build(p2))## overlap the panel of the 2nd plot on that of the 1st plotpp <- c(subset(g1$layout, name=="panel", se=t:r))g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name=="panel")]], pp$t, pp$l, pp$b, pp$l)EDIT to have the grid lines align with the lower axis ticks, replace the above line with: g <- gtable_add_grob(g1, g1$grobs[[which(g1$layout$name=="panel")]], pp$t, pp$l, pp$b, pp$l)## steal axis from second plot and modifyia <- which(g2$layout$name == "axis-b")ga <- g2$grobs[[ia]]ax <- ga$children[[2]]Now, you need to make sure you are modifying the correct dimension. Because the new axis is horizontal (a row and not a column), whatever_grob$heights is the vector to modify to change the amount of vertical space in a given row. If you want to add new space, make sure to add a row and not a column (ie. use gtable_add_rows()). If you are modifying grobs themselves (in this case we are changing the vertical justification of the ticks), be sure to modify the y (vertical position) rather than x (horizontal position).## switch position of ticks and labelsax$heights <- rev(ax$heights)ax$grobs <- rev(ax$grobs)ax$grobs[[2]]$y <- ax$grobs[[2]]$y - unit(1, "npc") + unit(0.15, "cm")## modify existing row to be tall enough for axisg$heights[[2]] <- g$heights[g2$layout[ia,]$t]## add new axisg <- gtable_add_grob(g, ax, 2, 4, 2, 4)## add new row for upper axis labelg <- gtable_add_rows(g, g2$heights[1], 1)g <- gtable_add_grob(g, g2$grob[[6]], 2, 4, 2, 4)# draw itgrid.draw(g)I'll note in passing that gtable_show_layout() is a very, very handy function for figuring out what is going on. 这篇关于ggplot2:在绘图顶部添加二次转换的x轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-25 15:36