本文介绍了Log10顶部和Y轴geom_line上的x轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 depth = c(1.6,2.6,3.6,4.6)我已经在ggplot2中创建了一个图形,如下所示: (5.6,6.6,7.6,8.6) ri df = data.frame(depth,ri) library(ggplot2) m m + scale_x_log10(Richardson Number,breaks = c(0.1,0.25,0.5,1,5,10,50))+ scale_y_reverse(深度(m)) 这是输出: 我正在尝试做的事情是沿着顶部有x轴,并且还包括一个geom_line,就像我添加的那个这里(手动绘制)。 据我所知,很难让ggplot2移动坐标轴,并且我试图在ggvis中重现此图,但我是你才能得到我需要的log10量表。有没有什么办法可以用R来创建我所瞄准的图表? 解决方案 更新解决方案 从 ggplot 2.2.0,可以在面板的顶部(和/或在面板的右侧) ) library(ggplot2) depth = c(1.6,2.6,3.6,4.6,5.6 (6.6,7.6,8.6) ri df = data.frame(depth,ri) m scale_x_log10(Richardson Number,breaks = c(0.1,0.25,0.5,1,5,10,50), $ = b 原始解决方案原始的,经过一点更新到ggplot version2.2.0。 使用 gtable 函数可以移动轴。根据@ Walter的回答调整代码,基本思想是:获取轴(轴文本和刻度标记);反转轴文本和刻度标记;在绘图面板正上方的gtable布局中添加一行;将修改后的轴插入新行。 library(ggplot2) library(gtable) library (电网) 深度= c(1.6,2.6,3.6,4.6,5.6,6.6,7.6,8.6) ri df = data.frame(depth,ri) m scale_x_log10(Richardson Number,breaks = c(0.1,0.25,0.5,1,5,10,50))+ scale_y_reverse(Depth(m))+ geom_path() #获取ggplot grob g1 ##获取图形面板在g1 中的位置pp #标题grobs有边距。 #边距需要交换。 #交换边界的函数 - 从cowplot包取得的#:#https://github.com/wilkelab/cowplot/blob/master/R/switch_axis.R vinvert_title_grob< - function(grob){ heights< - grob $ heights grob $ heights [1]< - heights [3] grob $ heights [3]< - 高度[1] grob $ vp [[1]] $ layout $ heights [1]< - 高度[3] grob $ vp [[1]] $ layout $ heights [3] < - 高度[1] grob $ children [[1]] $ hjust< - 1 - grob $ children [[1]] $ hjust grob $ children [[1 ]] $ vjust< - 1 - grob $ children [[1]] $ vjust grob $ children [[1]] $ y grob } #获取xlab和swap边距索引< - 其中(g1 $ layout $ name ==xlab-b ) xlab xlab #将xlab放在g1 的顶部g1 g1 #获取x轴(轴线,刻度线和刻度标记)索引 xaxis #交换轴刻度和刻度标记刻度< - xaxis $ children [[2]] ticks $ heights< - rev(ticks $ heights) ticks $ grobs< - rev(ticks $ grobs) #移动勾号#获得刻度线长度 plot_theme< - 函数(p){ plyr :: defaults(p $ theme,theme_get())} tml< - plot_theme(m)$ axis.ticks.length#刻度线长度 ticks $ grobs [[2]] $ y #交换标记标记'margin和justify ticks $ grobs [[1]]< - vinvert_title_grob(ticks $ grobs [[1]]) #将勾号和勾号标签放回xaxis xaxis $ children [[2]] #将轴添加到g1 $的顶部b $ b g1 g1< - gtable_add_grob(g1,xaxis,pp $ t + 1,pp $ l,pp $ t + 1,pp $ r,clip =off,name =axis-t) #删除原来的x轴和xlab g1 = g1 [-c(9,10),] #绘制它 grid.newpage() grid.draw (g1) I have created a graph in ggplot2 that looks like this:depth = c(1.6,2.6,3.6, 4.6,5.6,6.6,7.6,8.6) ri <- c(0.790143779,1.485888068,2.682375391,1.728120227,0.948414515,71.43308158,4.416120653,0.125458801)df = data.frame(depth,ri) library(ggplot2)m <- qplot(ri, depth, data=df)m + scale_x_log10("Richardson Number",breaks = c(0.1,0.25,0.5,1,5,10, 50)) + scale_y_reverse("Depth (m)")This is the output:What I am trying to do is have the x-axis along the top, and also include a geom_line like the one I have added here (manually in Paint).I understand that it is difficult to get ggplot2 to move the axes around, and I have tried to reproduce this graph in ggvis but I am unable to get the log10 scale I need. Is there any way I can use R to create the graph I am aiming for? 解决方案 Updated solutionAs of ggplot 2.2.0, axes can be drawn on the top of the panel (and/or on the right of the panel)library(ggplot2)depth = c(1.6,2.6,3.6, 4.6,5.6,6.6,7.6,8.6) ri <- c(0.790143779,1.485888068,2.682375391,1.728120227,0.948414515,71.43308158,4.416120653,0.125458801)df = data.frame(depth,ri) m <- qplot(ri, depth, data=df) + scale_x_log10("Richardson Number",breaks = c(0.1,0.25,0.5,1,5,10, 50), position = "top") + scale_y_reverse("Depth (m)")+ geom_path()Original solution The original, after a little updating to ggplot version2.2.0. Axes can be moved around using gtable functions. Adapting code from @Walter's answer here, the basic idea is to: get the axis (the axis text and the tick marks); reverse the axis text and tick marks; add a new row to the gtable layout immediately above the plot panel; insert the modified axis into the new row.library(ggplot2)library(gtable)library(grid)depth = c(1.6,2.6,3.6, 4.6,5.6,6.6,7.6,8.6) ri <- c(0.790143779,1.485888068,2.682375391,1.728120227,0.948414515,71.43308158,4.416120653,0.125458801)df = data.frame(depth,ri) m <- qplot(ri, depth, data=df) + scale_x_log10("Richardson Number",breaks = c(0.1,0.25,0.5,1,5,10, 50)) + scale_y_reverse("Depth (m)")+ geom_path()# Get ggplot grobg1 <- ggplotGrob(m) ## Get the position of the plot panel in g1pp <- c(subset(g1$layout, name == "panel", se = t:r))# Title grobs have margins. # The margins need to be swapped.# Function to swap margins - # taken from the cowplot package:# https://github.com/wilkelab/cowplot/blob/master/R/switch_axis.Rvinvert_title_grob <- function(grob) { heights <- grob$heights grob$heights[1] <- heights[3] grob$heights[3] <- heights[1] grob$vp[[1]]$layout$heights[1] <- heights[3] grob$vp[[1]]$layout$heights[3] <- heights[1] grob$children[[1]]$hjust <- 1 - grob$children[[1]]$hjust grob$children[[1]]$vjust <- 1 - grob$children[[1]]$vjust grob$children[[1]]$y <- unit(1, "npc") - grob$children[[1]]$y grob}# Get xlab and swap marginsindex <- which(g1$layout$name == "xlab-b")xlab <- g1$grobs[[index]]xlab <- vinvert_title_grob(xlab)# Put xlab at the top of g1g1 <- gtable_add_rows(g1, g1$heights[g1$layout[index, ]$t], pp$t-1)g1 <- gtable_add_grob(g1, xlab, pp$t, pp$l, pp$t, pp$r, clip = "off", name="topxlab")# Get x axis (axis line, tick marks and tick mark labels)index <- which(g1$layout$name == "axis-b")xaxis <- g1$grobs[[index]]# Swap axis ticks and tick mark labelsticks <- xaxis$children[[2]]ticks$heights <- rev(ticks$heights)ticks$grobs <- rev(ticks$grobs)# Move tick marks # Get tick mark length plot_theme <- function(p) { plyr::defaults(p$theme, theme_get()) } tml <- plot_theme(m)$axis.ticks.length # Tick mark lengthticks$grobs[[2]]$y <- ticks$grobs[[2]]$y - unit(1, "npc") + tml# Swap tick mark labels' margins and justificationsticks$grobs[[1]] <- vinvert_title_grob(ticks$grobs[[1]])# Put ticks and tick mark labels back into xaxisxaxis$children[[2]] <- ticks# Add axis to top of g1g1 <- gtable_add_rows(g1, g1$heights[g1$layout[index, ]$t], pp$t)g1 <- gtable_add_grob(g1, xaxis, pp$t+1, pp$l, pp$t+1, pp$r, clip = "off", name = "axis-t")# Remove original x axis and xlabg1 = g1[-c(9,10), ]# Draw itgrid.newpage()grid.draw(g1) 这篇关于Log10顶部和Y轴geom_line上的x轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-25 15:01