【发布时间】:2017-08-18 18:28:18
【问题描述】:
我一直在寻找这个问题的答案很长一段时间,但无法提出一个好的解决方案。我知道 ggplot2 没有在轴上添加次要刻度线的选项,但我找到了使用另一个 SO page 的解决方法。它提供了刻度线,但有两个问题。
目前,这是我的代码:
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)
这会产生刻度线,但有两个问题。首先,我如何在每 5 年保留/添加主要网格线?我不得不使用 panel.grid.major.x=element_blank() 关闭主要的网格线,因为这些线是在每个中断处,每 1 年一次。
其次,我如何在相应的刻度线下居中年份?目前,它们位于相应刻度线的右侧。我玩过 hjust 和 vjust,但都没有什么不同。
任何建议将不胜感激。我一直坚持使用 ggplot,因为它提供了许多我欣赏的其他功能,尤其是 facet_wrap。
这是数据集的一个示例:
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")
【问题讨论】:
-
不确定是否可以指定“次要”中断来代替主要中断,然后为主要中断设置网格。
-
@Masoud 感谢您的第一个建议。我尝试将 minor_breaks 添加到我的代码中,但它只添加了次要网格线,而不是我专门寻找的刻度线。有什么我想念的吗?
-
我正在考虑每 5 年将所有刻度添加为带有标签的主要刻度,并添加次要网格线。只是一个想法。