2015-01-08 10 views

risposta

1

theme(axis.ticks.length)

http://docs.ggplot2.org/0.9.2.1/theme.html dovrebbe essere utile.

Modifica Mi dispiace, leggi la tua domanda con più attenzione. Gli etichettati sono quelli standard major.grid? o etichettato in qualche modo personalizzato?

+0

Sono zecche major.grid, usando 'tema (axis.ticks.length)' definirà la lunghezza di tutti zecche, non solo quelle con etichette. – baisong

8

In base di R, è possibile sopprimere l'asse x con l'iniziale plot chiamata (con xaxt='n'), e quindi utilizzare tcl per controllare lunghezza tick (vedi ?par) con due axis(1, ...) chiamate.

plot(1:10, xaxt='n', ylab='', las=1) 
axis(1, at=1:10, tcl=-0.8) 
axis(1, at=seq(0, 11, 0.2), labels=NA) 

enter image description here

+0

Bello, ma OP ha chiesto ggplot, non la grafica di base R – smci

+2

@smci Non importa, è ancora fantastico. –

+1

@scmi - Ho visto il tag dopo che avevo già postato (è una buona idea includere i requisiti chiave _in post_). Indipendentemente da ciò, non fa male avere una soluzione di base R da quando le persone che vogliono usare la base possono imbattersi in questa domanda. – jbaums

3

Questo è più facile con la grafica di base, ma qui è un tentativo utilizzando ggplot2.

Il metodo è basato su this method, che inserisce etichette vuote nella sequenza di etichette (utilizzando il codice da tale risposta). Una volta che le interruzioni e le etichette sono state posizionate nel ggplot, ho scavato nella struttura del grafico per modificare le lunghezze dei segni di graduazione. (Usare con cura, non sarebbe stato difficile rompere questo.)

library(ggplot2) 
library(grid) 

# Data 
df = data.frame(x = 1:10, y = 1:10) 

# Range of x values 
range = 1:10 

# Major tick marks 
major = 1 

# Minor tick marks 
minor = 0.2 

# Function to insert blank labels 
# Borrowed from https://stackoverflow.com/questions/14490071/adding-minor-tick-marks-to-the-x-axis-in-ggplot2-with-no-labels/14490652#14490652 
insert_minor <- function(major, n_minor) { 
     labs <- c(sapply(major, function(x, y) c(x, rep("", y)), y = round(n_minor))) 
     labs[1:(length(labs) - n_minor)] 
} 

# Getting the 'breaks' and 'labels' for the ggplot 
n_minor = major/minor - 1 
breaks = seq(min(range), max(range), minor) 
labels = insert_minor(seq(min(range), max(range), major), n_minor) 
if(length(breaks) > length(labels)) labels = c(labels, rep("", length(breaks) - length(labels))) 

# The plot 
p <- ggplot(df, aes(x = x, y = y)) + 
    geom_point() + 
    scale_x_continuous(breaks = breaks, labels = labels) + 
    coord_cartesian(xlim = range) + 
    theme_bw() + 
    theme(panel.grid = element_blank(), 
      axis.text.x = element_text(margin = margin(t = 5, unit = "pt"))) 
p 

# Edit the plot: 
# Change the lengths of the major tick marks 

g = ggplotGrob(p) 

# Get the x axis 
xaxis <- g$grobs[[which(g$layout$name == "axis-b")]] 

# Get the tick marks and tick mark labels 
ticks <- xaxis$children[[2]] 

# Get the tick marks 
marks = ticks$grobs[[1]] 

# Edit the y positions of the end points of the tick marks 
# The '6' and the '3' in the code below 
# are the lengths in pts of the major and minor tick marks respectively. 
marks$y = unit.c(unit.c(unit(1, "npc") - unit(6, "pt"), unit(1, "npc"), 
       rep(unit.c(unit(1, "npc") - unit(3, "pt"), unit(1, "npc")), n_minor))) 

# Put the tick marks back into the plot 
ticks$grobs[[1]] = marks 
xaxis$children[[2]] = ticks 
g$grobs[[which(g$layout$name == "axis-b")]] = xaxis 

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

enter image description here

+1

Questo ha funzionato per me. Tranne che altero la riga in cui si ripristinano i propri marchi: 'segna $ y [seq (1, lunghezza (contrassegni $ y), 2)] [- seq (1, lunghezza (contrassegni $ y)/2, n_minor + 1)] <- unit (1, 'npc') - unit (3, 'pt') ' Usando in questo modo, si dovrebbe anche impostare' axis.ticks.length = unit (6, "pt") ' nel tema. – aaiezza

Problemi correlati