24

I have made a barplot using ggplot2 and the Journal I need to submit to requires that the axis ticks face inwards.

This is the my data

Mean.Inc.melt <- data.frame(
  Var1 = factor(rep(c("Harvest", "Pre-Harvest"), 3)),
  Var2 = factor(rep(c("Dip A", "Trip A", "Trip B"), each = 2L)),
  value = c(2, 34, 1, 36, 3, 46)
)

Including the standard error

SEM.Inc.melt <- data.frame(
  Var1 = factor(rep(c("Harvest", "Pre-Harvest"), 3)),
  Var2 = factor(rep(c("Dip A", "Trip A", "Trip B"), each = 2L)),
  value = c(1, 12, 1, 2, 1, 6)
)

This is the script I have used so far to create the plot:

ggplot(Mean.Inc.melt,aes(x=Var2,y=value,fill=Var1))+
  geom_bar(stat='identity',position=position_dodge(),colour='black')+
  scale_fill_manual(values=c('#000000','#FFFFFF'))+
  geom_errorbar(aes(ymin=Mean.Inc.melt$value-SEM.Inc.melt$value,
                    ymax=Mean.Inc.melt$value+SEM.Inc.melt$value),width=.1,
                    position=position_dodge(.9))+
  xlab('Treatment')+
  ylab('Percentage Incidence (%)')+
  ylim(0,60)+
  scale_y_continuous(expand=c(0,0),limits=c(0,60))+
  scale_x_discrete(expand=c(0,0))+
  theme_bw()+
  theme(axis.line=element_line(colour='black'),panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(),panel.border=element_blank(),
        panel.background=element_blank())+
  geom_vline(xintercept=0)+theme(legend.position='none')

I guess the point is - does anyone know if there a way I can get my axis to face inwards?

moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
Mismedolee
  • 343
  • 1
  • 2
  • 6

1 Answers1

24

While I don't understand journals' desire to have tick marks on the inside, it's quite straightforward to achieve this with ggplot.

The axis.ticks.length argument to theme allows you to set the length of tick marks. If this is set to a negative value, tick marks will be plotted inwards. For example (reposting a solution by Dennis Murphy here):

library(ggplot2)
library(grid)
ggplot(mtcars, aes(disp, mpg)) + geom_point() + 
  theme(axis.ticks.length=unit(-0.25, "cm"))

enter image description here

Note that the value should be passed as a unit object, which requires that the grid package (pre-installed with R) is loaded.

jbaums
  • 27,115
  • 5
  • 79
  • 119
  • 2
    In ggplot 2.0.0 having a `Warning message: 'axis.ticks.margin' is deprecated. Please set 'margin' property of 'axis.text' instead` -- and it seems `axis.text = element_text(margin=5)` does not help... I will post back if the solutions googles up. – xealits Apr 27 '16 at 17:23
  • 10
    So, according to test-and-try method, this is how you do it now: `theme(axis.ticks.length=unit(-0.25, "cm"), axis.text.x = element_text(margin=unit(c(0.5,0.5,0.5,0.5), "cm")), axis.text.y = element_text(margin=unit(c(0.5,0.5,0.5,0.5), "cm")) )` -- somehow it didn't inherit the setting from `axis.text`, and the error messages prompt for margin to be a vector of lengths 4. – xealits Apr 27 '16 at 17:31