Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make my axis ticks face Inwards in ggplot2

Tags:

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 text representation of my data (dput)

Mean.Inc.melt<-structure(list(Var1 = structure(c(1L, 2L, 1L, 2L, 1L, 2L), .Label = c("Harvest","Pre-Harvest"), class = "factor"), Var2 = structure(c(1L, 1L, 2L, 2L, 3L, 3L), .Label =c("Dip A", "Trip A", "Trip B"), class = "factor"), value = c(2, 34, 1, 36, 3, 46)), .Names =c("Var1", "Var2", "value"), row.names = c(NA, -6L), class = "data.frame")

Including the standard error

SEM.Inc.melt<-structure(list(Var1 = structure(c(1L, 2L, 1L, 2L, 1L, 2L), .Label = c("Harvest", "Pre-Harvest"), class = "factor"), Var2 = structure(c(1L, 1L, 2L, 2L, 3L, 3L), .Label = c("Dip A", "Trip A", "Trip B"), class = "factor"), value = c(1, 12, 1, 2, 1, 6)), .Names = c("Var1", "Var2", "value"), row.names = c(NA, -6L), class = "data.frame")

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?

like image 366
Mismedolee Avatar asked Oct 14 '14 18:10

Mismedolee


People also ask

How do you change the axis of a tick in R?

Set xaxt = "n" and yaxt = "n" to remove the tick labels of the plot and add the new labels with the axis function. Note that the at argument sets where to show the tick marks. Option 2. Set axes = FALSE inside your plotting function to remove the plot box and add the new axes with the axis function.

How do you add a tick mark in Axis?

Add tick marks on an axisClick Add Chart Element > Axes > More Axis Options. On the Format Axis pane, expand Tick Marks, and then click options for major and minor tick mark types. After you add tick marks, you can change the intervals between the tick marks by changing the value in the Interval between marks box.

How do I add a minor tick in ggplot2?

Adding minor ticks to graphs is very simple. There are two mains ways, using the continuous scale functions such as scale_x_continuous() or using the guides() function, both from ggplot2 . Note that guide_prism_minor() does not work with discrete axes as they do not have minor breaks.

What is a tick mark on an axis?

A tick is a short line on an axis. For category axes, ticks separate each category. For value axes, ticks mark the major divisions and show the exact point on an axis that the axis label defines. Ticks are always the same color and line style as the axis. Ticks come in two types: major and minor.


1 Answers

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.

like image 59
jbaums Avatar answered Oct 02 '22 23:10

jbaums