绘制区域和数据标签

时间:2015-03-23 19:12:26

标签: r ggplot2 facet labeling

我有一个图表,其中标签位于绘图区域之外。我想让绘图区域更小,并允许标签在它之外休息。以下是生成图表的代码 - 您可以看到我设法更改了y轴以使其工作,但是有一些"空"灰色空间。

require(ggplot2)
require(scales)

ggplot(data=df.long[as.character(df.long$locale) %in% "SPOKANE",],
          aes(x=Time, y=RR, colour=locale, group=locale, label=RR))+
  scale_y_continuous(breaks=seq(0, 100, 10))+
  labs(y="Response Rate")+
  coord_cartesian(ylim=c(0, 105))+
  geom_text(vjust=-1, size=4, angle=0, colour="black")+
  ggtitle(paste("i","Response Rates"))+
  theme(plot.title=element_text(size=18, face="bold", vjust=1),
        axis.title=element_text(size=16),
        axis.text.x=element_text(size=10, angle=90),
        legend.position="none",
        panel.background = element_rect(linetype=0),
        panel.grid.major=element_line(linetype=0),
        panel.grid.minor=element_line(linetype=0))+  
  scale_x_date(labels = date_format("%b-%y"), breaks=date_breaks("month"))+
  geom_line(size=.8)+
  facet_grid(~Year, scales="free_x", space="free_x") 

与此同时,我面对这一阴谋,因为我想在这些年间更清晰地划分。在PowerPoint中,我会做分层轴,但GGPlot不会这样做。下面是我所说的"分层轴"的图片。请注意月份是如何超过年份等等。

enter image description here

任何人对如何清洁标签并使背景透明有任何想法?

以下是使其可重现的数据:

df.long <- structure(list(Time = structure(c(15614, 15645, 15675, 15706, 
15737, 15765, 15796, 15826, 15857, 15887, 15918, 15949, 15979, 
16010, 16040, 16071, 16102, 16130, 16161, 16191, 16222, 16252, 
16283, 16314, 16344, 16375, 16405), class = "Date"), Year = c("2012", 
"2012", "2012", "2013", "2013", "2013", "2013", "2013", "2013", 
"2013", "2013", "2013", "2013", "2013", "2013", "2014", "2014", 
"2014", "2014", "2014", "2014", "2014", "2014", "2014", "2014", 
"2014", "2014"), locale = structure(c(31L, 31L, 31L, 31L, 31L, 
31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 
31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L), .Label = c("BALTIMORE", 
"BECKLEY", "FORT.DIX", "FORT.LEE", "HARRISBURG", "PITTSBURGH", 
"PITTSBURGH.1", "CHICAGO", "CLEVELAND", "COLUMBUS", "DES.MOINES", 
"DETROIT", "INDIANAPOLIS", "LANSING", "MILWAUKEE", "MINNEAPOLIS", 
"ALBANY", "BOSTON", "BUFFALO", "NEW.YORK", "PORTLAND..ME", "SPRINGFIELD", 
"SYRACUSE", "ANCHORAGE", "BOISE", "BUTTE", "FARGO", "PORTLAND..OR", 
"SEATTLE", "SIOUX.FALLS", "SPOKANE", "DENVER", "HONOLULU", "LOS.ANGELES", 
"PHOENIX", "SACRAMENTO", "SALT.LAKE.CITY", "SAN.DIEGO", "SAN.JOSE", 
"ATLANTA", "CHARLOTTE", "FORT.JACKSON", "JACKSON", "JACKSONVILLE", 
"KNOXVILLE", "LOUISVILLE", "MEMPHIS", "MIAMI", "MONTGOMERY", 
"NASHVILLE", "RALEIGH", "TAMPA", "ALBUQUERQUE", "AMARILLO", "DALLAS", 
"EL.PASO", "HOUSTON", "KANSAS.CITY", "LITTLE.ROCK", "NEW.ORLEANS", 
"OKLAHOMA.CITY", "OMAHA", "SAN.ANTONIO", "SHREVEPORT", "ST..LOUIS"
), class = "factor"), RR = c(100, 100, 100, 73, 100, 69, 100, 
86, 93, 95, 91, 41, 97, 94, 93, 95, 96, 52, 89, 83, 72, 69, 81, 
100, 93, 88, 78)), .Names = c("Time", "Year", "locale", "RR"), row.names = 811:837, class = "data.frame")

另外,请注意FIRST刻面右侧的标签是如何挤压并消失的 - 应该说是100,但是会变得嘎吱作响。怎么做我修复它?

enter image description here

1 个答案:

答案 0 :(得分:0)

显然使用grid包将允许我们禁用此类剪辑。

所以我绘制了绘图并将其存储在名为p的对象中,然后:

  gt <- ggplot_gtable(ggplot_build(p)) #Takes existing plot and stores into a gtable (programmable structure)
  gt$layout$clip[gt$layout$name == "panel"] <- "off" #Disables clipping
  grid.arrange(gt)

这会禁用裁剪!不幸的是,我无法让分层轴工作。