2016-12-03 48 views
2

這是我的數據集和生成圖的代碼。用ggplot軟件包更改yaxis面的標籤R

library(reshape) 
library(ggplot2) 
set.seed(357) 
myLetters <- function(length.out) { 
a <- rep(letters, length.out = length.out) 
grp <- cumsum(a == "a") 
vapply(seq_along(a), 
    function(x) paste(rep(a[x], grp[x]), collapse = ""), 
    character(1L)) 
} 
name <- myLetters(90) 
x <- data.frame(name = name, before = runif(90,min=-1, max=1),after = runif(90,min=-0.2, max=0.2)) 
x <- x[order(x$before),] 
xnew <- melt(x, id="name") 
adata <- subset(xnew,xnew$variable=="after") 
adata$name1 <- rep(1:90) 
bdata <- subset(xnew,xnew$variable=="before") 
bdata$name1 <- rep(1:90) 
d <- rbind(bdata,adata) 
d$name1 <- factor(d$name1,levels =c(1:90),labels = bdata$name) 
colorname <- c("dark blue","dark red") 
plot <- ggplot(data=d,aes(x=value,y=name1,group=variable,color=variable))+ 
    scale_fill_manual(values=colorname) + 
    scale_colour_manual(values=colorname) + # I ADDED THIS LINE 
    geom_point(size=1)+ 
    geom_path(aes(linetype = variable),size=0.3)+ 
    scale_linetype_manual(values=c("dashed", "solid"))+ 
    geom_vline(xintercept = c(-0.5,0,0.5),color="purple",size=0.5)+ 
    theme(panel.border = element_rect(colour = "black", fill=NA, size=0.5) 
    ) 

plot 

y軸的刻度標記標籤,因爲有90個標籤太靠近彼此。是否有辦法定位y軸刻度標記標籤,使第一個標籤定位爲默認值,第二個標籤反映在y軸上,以此類推全部90個標籤?也許這將有助於使情節更易於閱讀。

enter image description here

回答

2

我不認爲有什麼辦法做到這一點使用常規ggplot2命令。但可以使用grid編輯功能編輯圖表。兩種可能性:第一種錯開y軸標籤,如here;第二,如你所建議的,是反映Y軸上的每一個第二個標籤(和刻度線)。我把它留給你來決定是否比原版更具可讀性。

## Your data and code to generate the unedited plot 

library(reshape) 
library(ggplot2) 
library(grid) 
set.seed(357) 
myLetters <- function(length.out) { 
a <- rep(letters, length.out = length.out) 
grp <- cumsum(a == "a") 
vapply(seq_along(a), 
    function(x) paste(rep(a[x], grp[x]), collapse = ""), 
    character(1L)) 
} 
name <- myLetters(90) 
x <- data.frame(name = name, before = runif(90,min=-1, max=1),after = runif(90,min=-0.2, max=0.2)) 
x <- x[order(x$before),] 
xnew <- melt(x, id="name") 
adata <- subset(xnew,xnew$variable=="after") 
adata$name1 <- rep(1:90) 
bdata <- subset(xnew,xnew$variable=="before") 
bdata$name1 <- rep(1:90) 
d <- rbind(bdata,adata) 
d$name1 <- factor(d$name1,levels =c(1:90),labels = bdata$name) 
colorname <- c("dark blue","dark red") 
plot <- ggplot(data=d,aes(x=value,y=name1,group=variable,color=variable))+ 
    scale_fill_manual(values=colorname) + 
    scale_colour_manual(values=colorname) + # I ADDED THIS LINE 
    geom_point(size=1)+ 
    geom_path(aes(linetype = variable),size=0.3)+ 
    scale_linetype_manual(values=c("dashed", "solid"))+ 
    geom_vline(xintercept = c(-0.5,0,0.5),color="purple",size=0.5)+ 
    theme(panel.border = element_rect(colour = "black", fill=NA, size=0.5) 
    ) + theme_bw() 

plot 

第一種可能性

# Get the ggplot grob 
g = ggplotGrob(plot) 

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

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

瀏覽列表以查找指左軸部分。相關位是:

axis-l.6-3-6-3 
    axis.line.y..zeroGrob.1864 
    axis 
    axis.1-1-1-1 
     GRID.text.1861 
    axis.1-2-1-2 

你需要從「軸-L」設置路徑(見gPatheditGrob()功能),通過「軸」,通過「軸」,通過對「網格。文本'。標籤的x座標當前爲1npc。我所做的只是將每一個標籤都移動到0npc。

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

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

enter image description here

第二種可能性 - 需要更多的工作

# Get the ggplot grob 
g = ggplotGrob(plot) 

# Get a hierarchical list of component grobs. 
# Need a path to the labels as before, 
# and a path to the tick marks. 
grid.ls(grid.force(g)) 

# Get info about the plot: tick mark length, right margin of the tick mark labels, and 
# number of labels 
plot_theme <- function(p) { 
    plyr::defaults(p$theme, theme_get()) 
} 

tickMarkLength <- plot_theme(plot)$axis.ticks.length 
textRightMargin <- plot_theme(plot)$strip.text.y$margin[2] 
numberOfLabels <- length(unique(ggplot_build(plot)$data[[1]]$y)) 

## The edits: 
# Edit the x positions of every second label 
g = editGrob(grid.force(g), 
     gPath("axis-l", "axis", "axis", "GRID.text"), 
     x = unit.c(unit(1, "npc"), unit(1, "npc") + 2*tickMarkLength + textRightMargin - unit(1, "pt")), 
     hjust = c(1, 0), 
     grep = TRUE) 

# Edit the x coordinates of the tick marks 
# Need to check for even or odd number of labels 
xcoord = rep(unit.c(unit(1, "npc") - tickMarkLength, unit(1, "npc"), 
       unit(1, "npc") + tickMarkLength, unit(1, "npc")), numberOfLabels/2) 
if((numberOfLabels %% 2) == 0) { 
     xcoord = xcoord 
    } else { 
     xcoord = unit.c(xcoord, unit.c(unit(1, "npc") - tickMarkLength, unit(1, "npc"))) 
    } 
g = editGrob(grid.force(g), 
     gPath("axis-l", "axis", "axis.1-2"), 
     x = xcoord, 
     grep = TRUE) 


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

enter image description here

+1

第二個選擇是容易閱讀,真棒 – BIN

+0

我有錯誤錯誤validDetails。 polyline(x):'x'和'y'必須是相同的長度,在我運行這段代碼後g = editGrob(grid.force g), gPath(「axis-1」,「axis」,「axis.1-2」), x = rep(unit.c(unit(1,「npc」) - tickMarkLength,unit(1, npc「), unit(1,」npc「)+ tickMarkLength,unit(1,」npc「)),numberOfLabels/2), grep = TRUE),我覺得這個錯誤是因爲奇數行 – BIN

+1

順便說一下,我知道如何解決它 – BIN