KKL234
KKL234

Reputation: 367

R ggplot2 add minor tick marks around major gridlines

I've been searching for an answer to this question for quite some time, and can't come up with a good solution. I know that ggplot2 doesn't have an option for adding minor tick marks on the axes, but I've found a work around using another SO page. It provides tick marks, but there are 2 problems.

Currently, this is my code:

every_nth <- function(x, nth, empty = TRUE, inverse = FALSE)
{
if (!inverse) {
if(empty) {
x[1:nth == 1] <- ""
x
} else {
x[1:nth != 1]
}
} else {
if(empty) {
x[1:nth != 1] <- ""
x
} else {
x[1:nth == 1]
}
}
}

custom_breaks <- seq(1965, 2015, 1)

    ggplot(PercCanksPerYear_Site, aes(x=Cank_Year)) +
      geom_bar(aes(fill=Site_ID, y=PercTotalCanks), stat="identity", 
               position="dodge", width=0.5) + 
        scale_x_continuous(breaks=custom_breaks, labels=every_nth(custom_breaks,5, inverse=TRUE)) +
      ylab("Percent of Cankers per Year") +
      xlab("Year") + labs(fill="Site")+ 
      ggtitle("Percent of Total Cankers per Site that Occur each Year") +
      theme_bw() + 
      theme(plot.title=element_text(hjust = 0.5, face="bold", size=12), 
            axis.title=element_text(size=12), axis.text.x=element_text(size=10, angle=90, hjust=1), 
            legend.position = "None", panel.grid.minor.x = element_blank(), panel.grid.major.x = element_blank()) +  
      facet_wrap(~Site_ID)

Which produces this graph: enter image description here

This produces tick marks, but there are 2 problems. First, how do I keep/add major gridlines at every 5th year? I had to turn off major gridlines using panel.grid.major.x=element_blank() because the lines were at every break, which are every 1 year.

Second, how do I center the years under the respective tick mark? Currently they are off to the right of the respective tick marks. I've played with hjust and vjust, but neither have made a difference.

Any suggestions would be appreciated. I've stuck with ggplot because it offers a lot of other features I appreciate, particularly facet_wrap.

Here is a sample of the dataset:

PercCanksPerYear_Site <-
structure(list(Site_ID = c("M3", "M3", "M3", "M3", "M3", "M3", 
"M3", "M3", "M3", "M3", "M3", "M3", "M3", "M3", "M3", "M3", "M3", 
"M3", "M3", "M3", "M3", "M3", "M3", "M3", "M3", "M3", "M3", "M3", 
"M3", "M3", "M3", "M3", "M3", "M3", "N1", "N1", "N1", "N1", "N1", 
"N1", "N1", "N1", "N1", "N1", "N1", "N1", "N1", "N1", "N1", "N1", 
"N1", "N1", "N1", "N1", "N1", "N1", "N1", "N1", "N1", "N1", "N1", 
"N1", "N1", "N2", "N2", "N2", "N2", "N2", "N2", "N2", "N2", "N2", 
"N2", "N2", "N2", "N2", "N2", "N2", "N2", "N2", "N2", "N2", "N2", 
"N2", "N2"), Cank_Year = c(1974, 1977, 1981, 1982, 1983, 1984, 
1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1995, 1996, 1997, 
1998, 1999, 2000, 2001, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 
2010, 2011, 2012, 2013, 2014, 2015, 1968, 1975, 1976, 1982, 1983, 
1988, 1989, 1990, 1991, 1992, 1995, 1997, 1998, 1999, 2000, 2001, 
2002, 2003, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 
2014, 2015, 1989, 1990, 1992, 1993, 1995, 1997, 1998, 1999, 2000, 
2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 
2012, 2013), CankPerYear = c(2L, 1L, 1L, 2L, 2L, 4L, 13L, 3L, 
3L, 1L, 10L, 2L, 6L, 1L, 1L, 3L, 30L, 10L, 11L, 14L, 1L, 4L, 
17L, 20L, 5L, 3L, 5L, 2L, 5L, 6L, 8L, 6L, 3L, 1L, 2L, 3L, 2L, 
6L, 5L, 4L, 1L, 8L, 2L, 1L, 1L, 24L, 4L, 17L, 77L, 42L, 10L, 
1L, 2L, 5L, 18L, 54L, 20L, 6L, 15L, 1L, 1L, 6L, 3L, 2L, 3L, 2L, 
2L, 2L, 6L, 10L, 23L, 32L, 15L, 15L, 6L, 13L, 10L, 5L, 10L, 25L, 
10L, 12L, 13L, 7L, 2L), TotalSiteCanks = c(206L, 206L, 206L, 
206L, 206L, 206L, 206L, 206L, 206L, 206L, 206L, 206L, 206L, 206L, 
206L, 206L, 206L, 206L, 206L, 206L, 206L, 206L, 206L, 206L, 206L, 
206L, 206L, 206L, 206L, 206L, 206L, 206L, 206L, 206L, 341L, 341L, 
341L, 341L, 341L, 341L, 341L, 341L, 341L, 341L, 341L, 341L, 341L, 
341L, 341L, 341L, 341L, 341L, 341L, 341L, 341L, 341L, 341L, 341L, 
341L, 341L, 341L, 341L, 341L, 225L, 225L, 225L, 225L, 225L, 225L, 
225L, 225L, 225L, 225L, 225L, 225L, 225L, 225L, 225L, 225L, 225L, 
225L, 225L, 225L, 225L, 225L), PercTotalCanks = c(0.970873786407767, 
0.485436893203883, 0.485436893203883, 0.970873786407767, 0.970873786407767, 
1.94174757281553, 6.31067961165049, 1.45631067961165, 1.45631067961165, 
0.485436893203883, 4.85436893203883, 0.970873786407767, 2.9126213592233, 
0.485436893203883, 0.485436893203883, 1.45631067961165, 14.5631067961165, 
4.85436893203883, 5.33980582524272, 6.79611650485437, 0.485436893203883, 
1.94174757281553, 8.25242718446602, 9.70873786407767, 2.42718446601942, 
1.45631067961165, 2.42718446601942, 0.970873786407767, 2.42718446601942, 
2.9126213592233, 3.88349514563107, 2.9126213592233, 1.45631067961165, 
0.485436893203883, 0.586510263929619, 0.879765395894428, 0.586510263929619, 
1.75953079178886, 1.46627565982405, 1.17302052785924, 0.293255131964809, 
2.34604105571848, 0.586510263929619, 0.293255131964809, 0.293255131964809, 
7.03812316715543, 1.17302052785924, 4.98533724340176, 22.5806451612903, 
12.316715542522, 2.93255131964809, 0.293255131964809, 0.586510263929619, 
1.46627565982405, 5.27859237536657, 15.8357771260997, 5.86510263929619, 
1.75953079178886, 4.39882697947214, 0.293255131964809, 0.293255131964809, 
1.75953079178886, 0.879765395894428, 0.888888888888889, 1.33333333333333, 
0.888888888888889, 0.888888888888889, 0.888888888888889, 2.66666666666667, 
4.44444444444444, 10.2222222222222, 14.2222222222222, 6.66666666666667, 
6.66666666666667, 2.66666666666667, 5.77777777777778, 4.44444444444444, 
2.22222222222222, 4.44444444444444, 11.1111111111111, 4.44444444444444, 
5.33333333333333, 5.77777777777778, 3.11111111111111, 0.888888888888889
)), .Names = c("Site_ID", "Cank_Year", "CankPerYear", "TotalSiteCanks", 
"PercTotalCanks"), row.names = c(NA, -85L), class = "data.frame") 

Upvotes: 2

Views: 3599

Answers (1)

shaojl7
shaojl7

Reputation: 575

For the minor grid lines, one workaround is to use geom_vline (you should plot it first, so that is shows as the background of the bar plot, you can also adjust the color to a lighter shade of grey)

For the label position, we can adjust it with the vjust

custom_breaks <- seq(1965, 2015, 1)
vline_breaks <-  seq(1965, 2015, 5)
ggplot(PercCanksPerYear_Site) +
  geom_vline(xintercept = vline_breaks, color ="grey")+
  geom_bar(aes(x=Cank_Year, y=PercTotalCanks,fill=Site_ID), stat="identity", 
           position="dodge", width=0.5)+
  scale_x_continuous(breaks=custom_breaks, labels=every_nth(custom_breaks,5, inverse=TRUE)) +
  ylab("Percent of Cankers per Year") +
  xlab("Year") + labs(fill="Site")+ 
  ggtitle("Percent of Total Cankers per Site that Occur each Year") +
  theme_bw() + 
  theme(plot.title=element_text(hjust = 0.5, face="bold", size=12), 
        axis.title=element_text(size=12), axis.text.x=element_text(size=10, angle=90, hjust=1, vjust= 2), 
        legend.position = "None", panel.grid.minor.x = element_blank(), panel.grid.major.x = element_blank()) +  
  facet_wrap(~Site_ID)

Upvotes: 0

Related Questions