轴文本的奇怪辩护

时间:2011-11-10 19:00:22

标签: r ggplot2

我正在使用ggplot2绘制一个pointrange()图,并且x和y标签与它们各自的刻度的末尾奇怪地分开。这个特定脚本中的所有图都不会发生这种情况,只有少数几个,包括这个(基于可用数据的子集,但显示问题):

Example of odd plotting behavior

正如您所看到的,y轴标签基本上向左偏移,x轴标签基本上偏离了刻度线,直到它们在轴标签上方过度绘制。

我在制作此图之前对theme_bw()进行的唯一修改是设置theme_set(theme_bw(base_size = 8)) - 在绘图代码之前没有故意对文本对齐进行任何更改。

这是下面绘制的子集的dput():

TestData:
structure(list(State = c("AL", "AZ", "CA", "CO", "CT", "DC", 
"DE", "FL", "GA", "IL"), Year = c(2008L, 2008L, 2008L, 2008L, 
2008L, 2008L, 2008L, 2008L, 2008L, 2008L), N = c(22L, 42L, 286L, 
99L, 30L, 14L, 20L, 173L, 78L, 29L), Polarization = c(0.352923743188869, 
0.505918664112271, 0.445768659699068, 0.555930347461176, -0.0133878043740006, 
-0.380342319255035, -0.450998867087007, 0.385507917713463, 0.368070478718073, 
0.23368733390603), PolarizationSE = c(0.16021790292877, 0.0650610761652209, 
0.0100695976668952, 0.0270310803233059, 0.127526745827604, 0.296328985544823, 
0.179653490097689, 0.0180113004747975, 0.0372516250664796, 0.112905812606479
), IDPol = c(0.198743353462518, 0.0416441096132583, 0.0551808637190376, 
0.110549247724351, 0.302497569072991, -0.0343523165297017, -0.00367975496702999, 
0.0520660142625065, 0.0762126127715774, 0.0936515057040723), 
IDPolSE = c(0.102763798140243, 0.0523842634480865, 0.00789292373693809, 
0.023425554880421, 0.0918856966184178, 0.184867986813743, 
0.122339223641891, 0.0137386656250425, 0.0285951418531372, 
0.0896433805255375), Estimate = c(0.00932965761458826, -0.000412018017715892, 
0.00315002626133457, 0.00823125148777124, 0.000741919819714724, 
-0.0211994171332907, -0.0218353390160545, 0.00290805283382581, 
0.00406489584624635, 0.00604261698709428), Std..Error = c(0.00398420082222495, 
0.00483343236746232, 0.00186579338568264, 0.0032167092866312, 
0.00379995092553099, 0.0128981988697743, 0.0122846784163747, 
0.00220581166165486, 0.00335683359383524, 0.00240425995025825
), StateN = structure(c(13L, 25L, 18L, 27L, 4L, 2L, 1L, 15L, 
14L, 7L), .Label = c("DE (20)", "DC (14)", "WA (23)", "CT (30)", 
"TX (365)", "NY (123)", "IL (29)", "PA (36)", "MI (114)", 
"KS (28)", "OK (36)", "NJ (23)", "AL (22)", "GA (78)", "FL (173)", 
"SC (69)", "MN (25)", "CA (286)", "OH (85)", "VA (34)", "IN (55)", 
"OR (27)", "WI (22)", "NM (64)", "AZ (42)", "TN (77)", "CO (99)", 
"LA (83)", "MA (22)", "NC (65)", "MS (63)"), class = "factor")), .Names = c("State", 
"Year", "N", "Polarization", "PolarizationSE", "IDPol", "IDPolSE", 
"Estimate", "Std..Error", "StateN"), row.names = c(1401L, 1403L, 
1404L, 1405L, 1406L, 1407L, 1408L, 1409L, 1410L, 1414L), class = "data.frame")

以下是用于制作情节的代码:

TestData$StateN <- paste(TestData$State, " (", TestData$N, ")", sep = "")
TestData$StateN <- factor(TestData$StateN, levels = TestData$StateN[order(TestData$Polarization)])

ZP17Test <- ggplot(TestData,
 aes(x = StateN, y = Polarization,
 ymin = Polarization - 1.96 * PolarizationSE, ymax = Polarization + 1.96 * PolarizationSE))
ZP17Test <- ZP17Test + geom_hline(yintercept = 0, colour = I(MyPalette(5)[3]), alpha = I(7/12), size = I(1/3))
ZP17Test <- ZP17Test + geom_pointrange(size = I(1/3))
ZP17Test <- ZP17Test + scale_x_discrete("State (Number of Respondents)")
ZP17Test <- ZP17Test + opts(title = "State Polarization Levels in 2008",
 axis.text.x = theme_text(angle=45, hjust=1, size = 7))
print(ZP17Test)
ggsave(plot = ZP17Test, "Analysis/Stack_Overflow.png", h = 4, w = 6)

提前感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:2)

有角度的轴文字标签总是与我的头脑混在一起。你想要的是这个:

axis.text.x = theme_text(hjust = 1,vjust = 1,angle=45, size = 7)

根据我的经验,您指定它们的顺序会有所不同。我总是不得不摆弄它,直到我说得恰到好处。聪明的人可能会记住它的系统。