首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >跟踪x轴标签:参差轴标签,ggplot2中的新特性

跟踪x轴标签:参差轴标签,ggplot2中的新特性
EN

Stack Overflow用户
提问于 2016-09-22 23:24:28
回答 1查看 439关注 0票数 1

Stagger axis labels, new feature in ggplot2

我是R公司的新手,跟着桑迪·穆斯普拉特对“脊椎医生”的回答有点让人望而生畏。这个答案对y轴标签非常有用。我试过编辑。例如,我改变了:

代码语言:javascript
运行
复制
index <- which(g$layout$name == "axis-l")  # Which grob

至:

代码语言:javascript
运行
复制
index <- which(g$layout$name == "axis-b")  # Which grob

但是x轴的标签仍然保持原样,没有错位。请说明如何修改代码,使其同样适用于x轴标签吗?

代码语言:javascript
运行
复制
# Get the grob
g <- ggplotGrob(out.plot)

# Get the y axis
index <- which(g$layout$name == "axis-l")  # Which grob
yaxis <- g$grobs[[index]]   

# Get the ticks (labels and marks)
ticks <- yaxis$children[[2]]

# Get the labels
ticksL <- ticks$grobs[[1]]

# Make the edit
ticksL$children[[1]]$x <- rep(unit.c(unit(c(1,0,-1),"npc")), 27)

# Put the edited labels back into the plot
ticks$grobs[[1]] <- ticksL
yaxis$children[[2]] <- ticks
g$grobs[[index]] <- yaxis

# Make the relevant column a little wider
g$widths[3] <- unit(2.5, "cm")

# Draw the plot
grid.newpage()
grid.draw(g)

TableGrob的输出如下:

代码语言:javascript
运行
复制
>g
TableGrob (6 x 5) "layout": 8 grobs
  z     cells       name                                   grob
1 0 (1-6,1-5) background        rect[plot.background..rect.507]
2 3 (3-3,3-3)     axis-l    absoluteGrob[GRID.absoluteGrob.498]
3 1 (4-4,3-3)     spacer                         zeroGrob[NULL]
4 2 (3-3,4-4)      panel                  gTree[GRID.gTree.484]
5 4 (4-4,4-4)     axis-b    absoluteGrob[GRID.absoluteGrob.491]
6 5 (5-5,4-4)       xlab titleGrob[axis.title.x..titleGrob.501]
7 6 (3-3,2-2)       ylab titleGrob[axis.title.y..titleGrob.504]
8 7 (2-2,4-4)      title     zeroGrob[plot.title..zeroGrob.505]

我试着识别相关的x,y轴值,但是导航这个结构对我来说有点陌生。任何建议或意见或资源,以避免浪费时间猜测,将受到极大的赞赏。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-23 00:59:43

在这种情况下,你只需要改变高度而不是宽度。同样在editGrob中,您需要传递y插槽,而不是x,因为您正在更改坐标wrt,y轴不是从左到右。

我保持一切不变,但注释掉了我更改的内容,并将我的更改直接放在下面。

代码语言:javascript
运行
复制
# Libraries
library(ggplot2)
library(gtable)
library(grid)
library(stringi)

# fake data
set.seed(12345)
var <- stri_rand_strings(81, 4, pattern = '[HrhEgeIdiFtf]')
var1 <- rnorm(81, mean = 175, sd = 75)
out <- data.frame(var, var1)
out$var <- factor(out$var, levels = out$var[order(out$var1, decreasing = FALSE)])

# Plot
# out.plot <- ggplot(out, aes(x = var, y = var1)) + geom_point() + coord_flip()
out.plot <- ggplot(out, aes(x = var1, y = var)) + geom_point() + coord_flip()

# Get the ggplot grob
g = ggplotGrob(out.plot)

# Get a hierarchical list of component grobs
grid.ls(grid.force(g))

# make the relevant column a little wider
# g$widths[3] = unit(2.5, "cm")
g$heights[4] = unit(1, "cm")

# The edit
g = editGrob(grid.force(g),
             gPath("axis-b", "axis", "axis", "GRID.text"),
             # x = unit(c(-1, 0, 1), "npc"), 
             y = unit(c(1, 0, -1), "npc"), ## or c(-1,0,1) etc to change order
             grep = TRUE)

# Draw the plot
grid.newpage()
grid.draw(g)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39650313

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档