延长X轴范围

时间:2014-07-29 21:43:10

标签: r ggplot2 time-series

我正在使用ggplot来绘制一个时间序列,这个时间序列遇到了扩展x轴范围的问题。我开发了以下代码以提供可简化的示例。

#Dummy Data
Dates <- data.frame(Date = c("1992-11-21","1993-10-26","1995-05-12","1996-03-03","1999-05-22","2008-04-13"))
Volume <- data.frame(Volume = c("28947.548","29947.262","30842.333","27192.588","30209.414","24439.897"))
Errors <- data.frame(Errors = c("4118.903","1974.606","1843.382","1920.362","1905.469","1977.074"))
ID <- data.frame(ID = c("a","a","a","b","b","b"))
Merge_Data <- data.frame(Dates,Volume,Errors,ID)

#convert Dates to native format in R
Merge_Data$Date <- as.Date(Merge_Data$Date,"%Y-%m-%d")

#Convert Areas to numbers
Merge_Data$Volume <- as.numeric(as.character(Merge_Data$Volume))
Merge_Data$Errors <- as.numeric(as.character(Merge_Data$Errors))

#Plot the Data                                                            
ggplot(Merge_Data, aes(x = Date, y = Volume, color = ID)) + 
  scale_color_manual(values = c("#000000", "#0000BB")) +
  geom_errorbar(aes(ymin=Volume-Errors,ymax=Volume+Errors), width=100,size=0.1) +
  geom_point(size = 2) + 
  geom_line(size = 0.5)+
  scale_x_date(labels = date_format("%Y"), breaks = date_breaks("2 year"))+
  xlab("Date")+
  ylab("Volume, in cubic meters")+ 
  ylim(0,max(Merge_Data$Volume)+20000)+
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_rect(colour="black",fill = "white",),
        axis.text = element_text(colour = "black"),
        legend.background = element_rect(colour = "black"),
        legend.key = element_rect(color=NA, fill="white"),
        legend.title = element_blank(),
        legend.position=c(0.9,0.9))

我需要将x轴的范围扩展到1990年开始并在2014年结束。我已尝试在limits行中使用scale_x_Date表达式,但没有任何运气。

提前致谢,

dubbbdan

1 个答案:

答案 0 :(得分:0)

我明白了!!

您只需更改scale_x_date行即可包含lim=表达式。

#Dummy Data
Dates <- data.frame(Date = c("1992-11-21","1993-10-26","1995-05-12","1996-03-03","1999-05-22","2008-04-13"))
Volume <- data.frame(Volume = c("28947.548","29947.262","30842.333","27192.588","30209.414","24439.897"))
Errors <- data.frame(Errors = c("4118.903","1974.606","1843.382","1920.362","1905.469","1977.074"))
ID <- data.frame(ID = c("a","a","a","b","b","b"))
Merge_Data <- data.frame(Dates,Volume,Errors,ID)

#convert Dates to native format in R
Merge_Data$Date <- as.Date(Merge_Data$Date,"%Y-%m-%d")

#Convert Areas to numbers
Merge_Data$Volume <- as.numeric(as.character(Merge_Data$Volume))
Merge_Data$Errors <- as.numeric(as.character(Merge_Data$Errors))

#Plot the Data                                                            
ggplot(Merge_Data, aes(x = Date, y = Volume, color = ID)) + 
  scale_color_manual(values = c("#000000", "#0000BB")) +
  geom_errorbar(aes(ymin=Volume-Errors,ymax=Volume+Errors), width=100,size=0.1) +
  geom_point(size = 2) + 
  geom_line(size = 0.5)+
  scale_x_date(lim = c(as.Date("1990-1-1"), as.Date("2014-1-1")),labels = date_format("%Y"), breaks = date_breaks("2 year"))+
  xlab("Date")+
  ylab("Volume, in cubic meters")+ 
  ylim(0,max(Merge_Data$Volume)+20000)+
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_rect(colour="black",fill = "white",),
        axis.text = element_text(colour = "black"),
        legend.background = element_rect(colour = "black"),
        legend.key = element_rect(color=NA, fill="white"),
        legend.title = element_blank(),
        legend.position=c(0.9,0.9))