使用ggplot2更改单个图形的刻度线的位置 [英] change position of tick marks of a single graph, using ggplot2

查看:651
本文介绍了使用ggplot2更改单个图形的刻度线的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都知道如何使用ggplot2在R中的单个绘图的顶部绘制X轴刻度标记?我一直在寻找教程,邮件列表和Rhelp而没有成功。



预先感谢。
Agus

解决方案使用 position ='top'

  library(ggplot2)

#生成一些数据
df = data.frame(x = 1:10,y = 1:10)

#x轴休息
休息= 1:10

#底图
p < - ggplot(df,aes(x,y))+ geom_point()+
scale_x_continuous(breaks = break,position ='top')+
scale_y_continuous(limits = c(0,11),expand = c(0,0))+
theme_bw()

原始解决方案可以使用 gtable (有些更新到ggplot 2.1.0)。请参阅此处

 库(ggplot2)
库(gtable)
库(网格)

#生成一些数据
df = data.frame(x = 1:10,y = 1:10)

#x轴休息
休息= 1 :10

#底图
p < - ggplot(df,aes(x,y))+ geom_point()+
scale_x_continuous(休息=休息)+
scale_y_continuous(limits = c(0,11),expand = c(0,0))+
theme_bw()


#获取ggplot grob
g1 < - ggplotGrob(p)

##获取绘图面板在g1
pp <-c(subset(g1 $ layout,name ==panel,se = t:r))

#标题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 < - unit(1,npc) - grob $ children [[ 1]] $ y
grob
}

#获取xlab和swap边距
索引< - 其中(g1 $ layout $ name ==xlab-b )
xlab <-g1 $ grobs [[index]]
xlab < - vinvert_title_grob(xlab)

#将xlab放在g1
的顶部g1 g1 < - gtable_add_grob(g1,xlab,pp $ t,pp $ l ,pp $ t,pp $ r,剪辑=off,name =topxlab)

#获取x轴(轴线,刻度线和刻度标记)
索引< - 其中(g1 $ layout $ name = =axis-b)
xaxis < - g1 $ grobs [[index]]

#交换轴刻度和刻度标记
刻度< - 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(p)$ axis.ticks.length#刻度线长度

ticks $ grobs [[2]] $ y < - ticks $ grobs [[2]] $ y - unit(1 ,npc)+ tml

#交换标记标记'margin和justify
ticks $ grobs [[1]]< - vinvert_title_grob(ticks $ grobs [[1]])

#将勾号和勾号标签放回xaxis
xaxis $ children [[2]] < - 勾号

#将轴添加到g1 $的顶部b $ b g1 < - 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)

#删除原来的x轴和xlab
g1 = g1 [-c(9,10),]

#绘制它
grid.newpage()
grid.draw (g1)


Anybody knows how to draw the X-axis tickmarks at the top of a single plot in R, using ggplot2? I ve been looking at tutorials and mail lists and Rhelp without success.

Thanks in advance. Agus

解决方案

Use position = 'top'

library(ggplot2)

# Generate some data
df = data.frame(x = 1:10, y = 1:10)

# x-axis breaks
breaks = 1:10

# base plot
p <- ggplot(df, aes(x,y)) + geom_point() +
     scale_x_continuous(breaks = breaks, position = 'top') +
     scale_y_continuous(limits = c(0, 11), expand = c(0,0)) +
     theme_bw()

Original solution available using gtable (with some updating to ggplot 2.1.0). See here

library(ggplot2)
library(gtable)
library(grid)

# Generate some data
df = data.frame(x = 1:10, y = 1:10)

# x-axis breaks
breaks = 1:10

# base plot
p <- ggplot(df, aes(x,y)) + geom_point() +
     scale_x_continuous(breaks = breaks) +
     scale_y_continuous(limits = c(0, 11), expand = c(0,0)) +
     theme_bw() 


# Get ggplot grob
g1 <- ggplotGrob(p)  

## Get the position of the plot panel in g1
pp <- 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.R
vinvert_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 margins
index <- which(g1$layout$name == "xlab-b")
xlab <- g1$grobs[[index]]
xlab <- vinvert_title_grob(xlab)

# Put xlab at the top of g1
g1 <- 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 labels
ticks <- 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(p)$axis.ticks.length   # Tick mark length

ticks$grobs[[2]]$y <- ticks$grobs[[2]]$y - unit(1, "npc") + tml

# Swap tick mark labels' margins and justifications
ticks$grobs[[1]] <- vinvert_title_grob(ticks$grobs[[1]])

# Put ticks and tick mark labels back into xaxis
xaxis$children[[2]] <- ticks

# Add axis to top of g1
g1 <- 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 xlab
g1 = g1[-c(9,10), ]

# Draw it
grid.newpage()
grid.draw(g1)

这篇关于使用ggplot2更改单个图形的刻度线的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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