交错轴标签,ggplot2中的新功能 [英] Stagger axis labels, new feature in ggplot2

查看:158
本文介绍了交错轴标签,ggplot2中的新功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好:我需要绘制一个具有不同频率计数的81个不同类别的因子。每个因素名称都是一个4个字母的类别。它看起来像这样。正如你所看到的,阅读因子标签非常困难。我想根据

另一种选择是通过结构找到你想要的方式,把相关的grob到ma编辑。

 #获取grob 
g< - ggplotGrob(out.plot)

#获得y轴
index< - 其中(g $ layout $ name ==axis-l)#其中grob
yaxis< - g $ grobs [[index]]

#获取标记(标记和标记)
标记< - yaxis $ children [[2]]

#获取标记
ticksL < - 滴答$ grobs [[1]]

#编辑
ticksL $ children [[1]] $ x< - rep(unit.c(unit(c(1,0, -1),npc)),27)

#把编辑后的标签放回到图中
ticks $ grobs [[1]] < - ticksL
yaxis $ children [[2]]< - ticks
g $ grobs [[index]]< - yaxis

#使相关列稍宽一些
g $ widths [3 ] < - unit(2.5,cm)

#绘制绘图
grid.newpage()
grid.draw(g)


Hi there: I need to plot a factor with 81 different categories with different frequency counts each. Each factor name is a 4-letter category. It looks like this. As you can see, it is pretty tough to read the factor labels. I'd like to stagger the y-axis according to this suggestion. However, this issue on github suggests that something has changed in ggplot2 and that the hjust and vjust options no longer work. Does anyone have any suggestions to make this plot look better, in particular to make the factor levels readable.

#libraries
# install.packages('stringi')
library(ggplot2)
library(stringi)
#fake data
var<-stri_rand_strings(81, 4, pattern='[HrhEgeIdiFtf]')
var1<-rnorm(81, mean=175, sd=75)
#data frame
out<-data.frame(var, var1)

#set levels for plotting
out$var<-factor(out$var, levels=out$var[order(out$var1, decreasing=FALSE)])
#PLot
out.plot<-out %>%
ggplot(., aes(x=var, y=var1))+geom_point()+coord_flip()
#Add staggered axis option
out.plot+theme(axis.text.y = element_text(hjust = grid::unit(c(-2, 0, 2),     "points")))

解决方案

To stagger the labels, you could add spaces to the labels in the dataframe.

# Libraries
library(ggplot2)
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)

# Add spacing, and set levels for plotting
out = out[order(out$var1), ]
out$var = paste0(out$var, c("", "       ", "            "))
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

Alternatively, draw the original plot, then edit. Here, I use the grid function, editGrob() to do the editing.

# 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)

# Set levels for plotting
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()

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

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

Look through the list to find the section referring to the left axis. The relevant bit is:

   axis-l.6-3-6-3   
     axis.line.y..zeroGrob.232   
     axis   
       axis.1-1-1-1   
         GRID.text.229   
       axis.1-2-1-2   

You will need to set up path from 'axis-l', through 'axis', through 'axis', though to 'GRID.text'.

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

# The edit
g = editGrob(grid.force(g), 
      gPath("axis-l", "axis", "axis", "GRID.text"), 
      x = unit(c(-1, 0, 1), "npc"), 
      grep = TRUE)

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

Another option is to find your way through the structure to the relevant grob to make the edit.

# 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)

这篇关于交错轴标签,ggplot2中的新功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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