2016-05-02 8 views
3

こんにちは、私は、周波数カウントがそれぞれ異なる81種類のカテゴリの因子をプロットする必要があります。各因子名は4文字のカテゴリです。それはこのように見えます。ご覧のように、因子ラベルを読むのはかなり難しいです。私はthis提案に従ってy軸をずらしたいと思います。しかし、githubのthisでは、ggplot2で何かが変更され、hjustオプションとvjustオプションが機能しなくなることが示唆されています。特に、因子レベルを読みやすくするために、このプロットをよりよく見せるための提案がありますか?ggplot2の新機能

#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"))) 
+0

ラベルを90度回転させることで回避できます。 – TheComeOnMan

+0

私はそのことを知っていましたが、別の解決策を望んでいました。 – spindoctor

答えて

4

ラベルをずらすには、データフレーム内のラベルにスペースを追加します。

# 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 

また、元のプロットを描画して編集します。ここでは、grid関数editGrob()を使用して編集します。

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

左軸を参照してセクションを検索してください。関連するビットは次のとおりです。

 
    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 

あなたは「GRID.text」にかかわらず、「軸」を介して、「軸」を介して、「軸-L」からのパスを設定する必要があります。

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

別のオプションは、編集を行うために、関連するグロブに構造を通してあなたの方法を見つけることです。

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