1

我有一个图表,其中标签位于绘图区域之外。我想让情节区域更小,并允许标签在它之外休息。这是生成绘图的代码——您可以看到我设法更改了 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 不会这样做。下面是我所说的“分层轴”的图片。注意月份如何高于年份等等。

在此处输入图像描述

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

这是使其可重现的数据:

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,但正在被挤压。我该怎么做?

在此处输入图像描述

4

1 回答 1

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)

这会禁用剪辑!不幸的是,我还不能让分层轴工作。

于 2015-03-24T23:15:36.793 回答