Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R x轴标签的后续操作:交错轴标签,ggplot2中的新功能_R_Plot_Ggplot2 - Fatal编程技术网

R x轴标签的后续操作:交错轴标签,ggplot2中的新功能

R x轴标签的后续操作:交错轴标签,ggplot2中的新功能,r,plot,ggplot2,R,Plot,Ggplot2,我是R的新手,从@Sandy Muspratt的答案到@spindoctor的答案有点让人望而生畏。答案对于y轴标签非常有用。我试过编辑。例如,我改变了: index <- which(g$layout$name == "axis-l") # Which grob 我尝试识别相关的x、y轴值,但导航此结构对我来说有点陌生。任何避免浪费时间猜测的建议、评论或资源都将不胜感激。在这种情况下,您只需更改高度而不是宽度。同样,在editGrob中,您需要通过y插槽而不是x,因为您正在改变x轴上

我是R的新手,从@Sandy Muspratt的答案到@spindoctor的答案有点让人望而生畏。答案对于y轴标签非常有用。我试过编辑。例如,我改变了:

index <- which(g$layout$name == "axis-l")  # Which grob
我尝试识别相关的x、y轴值,但导航此结构对我来说有点陌生。任何避免浪费时间猜测的建议、评论或资源都将不胜感激。

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

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

# 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)
#库
图书馆(GG2)
图书馆(gtable)
图书馆(网格)
图书馆(stringi)
#假数据
种子集(12345)

var@Sandy Muspratt可能对此有意见,所以我试图通知他们。如果有更好的方法,请告诉我。也可能@spindoctor已经想出了如何做到这一点。太棒了!我把g$heights[4]=unit(1,“cm”)改为g$heights[4]=unit(2.5,“cm”),效果非常好!我还使用了y=单位(c(1,0),“npc”)##
# 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)
>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]
# 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)