2017-08-18 2 views
1

Je cherche une réponse à cette question depuis un certain temps et je ne peux pas trouver une bonne solution. Je sais que ggplot2 n'a pas une option pour ajouter des graduations mineures sur les axes, mais j'ai trouvé un travail autour d'un autre SO page. Il fournit des graduations, mais il y a 2 problèmes.R ggplot2 ajoute des graduations mineures autour du quadrillage principal

Actuellement, voici mon 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) 

qui produit ce graphique: enter image description here

Ce produit des marques de tiques, mais il y a 2 problèmes. Premièrement, comment puis-je conserver/ajouter des quadrillages majeurs tous les cinq ans? J'ai dû désactiver la grille principale en utilisant panel.grid.major.x = element_blank() car les lignes étaient à chaque pause, ce qui est tous les 1 an.

Deuxièmement, comment centrer les années sous la marque de graduation respective? Actuellement, ils sont à droite des graduations respectives. J'ai joué avec hjust et vjust, mais ni l'un ni l'autre n'ont fait la différence.

Toutes les suggestions seraient appréciées. Je suis resté avec ggplot parce qu'il offre beaucoup d'autres fonctionnalités que j'apprécie, en particulier facet_wrap.

Voici un échantillon de l'ensemble de données:

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") 
+0

https: // stackoverflow .com/questions/12370175/add-grid-lines-to-a-ggplot-independent-of-axis-breaks – Masoud

+1

https://stackoverflow.com/questions/43928779/add-grid-lines-to-minor-breaks -only-ggplot – Masoud

+0

Vous n'êtes pas sûr de pouvoir spécifier des ruptures "mineures" à la place des ruptures majeures, puis d'avoir une grille pour les ruptures majeures. –

Répondre

0

Pour les lignes de quadrillage mineures, une solution consiste à utiliser geom_vline (vous devez tracer d'abord, si c'est montre que l'arrière-plan de la parcelle bar, vous pouvez également ajuster la couleur à une teinte plus claire de gris)

pour la position de l'étiquette, on peut le régler avec le 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)