从OECD.stat生成的ggplot折线图,图表中有不需要的垂直线

时间:2015-04-04 17:28:51

标签: xml r ggplot2 time-series linechart

我正在尝试关注从the post on R-bloggers获取数据的OECD Statistics。我修改了帖子中的代码来获取我想要显示的数据:

require(XML2R)
file <- "http://stats.oecd.org/restsdmx/sdmx.ashx/GetData/HEALTH_STAT/EVIE+EVIEFE00+EVIEFE40+EVIEFE60+EVIEFE65+EVIEFE80+EVIEHO00+EVIEHO40+EVIEHO60+EVIEHO65+EVIEHO80+EVIETOTA.EVIDUREV+EVIFHOEV+EVIHFEEV.POL+GBR+USA/all?startTime=1960&endTime=2013"
obs <- XML2Obs(file)
tables <- collapse_obs(obs)
keys <- tables[["MessageGroup//DataSet//Series//SeriesKey//Value"]]
dates <- tables[["MessageGroup//DataSet//Series//Obs//Time"]]
values <- tables[["MessageGroup//DataSet//Series//Obs//ObsValue"]]
country_list <- keys[keys[,1]== "COU" | keys[,1]== "COUNTRY"]
country_list <- country_list[(length(country_list)*1/3+1):(length(country_list)*2/3)]
dat <- cbind.data.frame(as.numeric(dates[,1]),as.numeric(values[,1]))
colnames(dat) <- c('date', 'value')
dat$country <- c(country_list[1], country_list[cumsum(diff(dat$date) <= 0) + 1])
dat$value2 <- signif(dat$value,2)
head(dat)
dat <- dat[complete.cases(dat),]
library(ggplot2)
ggplot(dat) +
    geom_line(aes(x = date,y = value, colour = country)) +
    theme(axis.title = element_text(colour = 'black', face = 'bold', size = 12,
                                    family = 'sans'),
          axis.text = element_text(colour = 'black', size = 12),
          plot.title = element_text(size = 17, face = "bold", colour = "black"),
          panel.background = element_rect(fill = NA),
          panel.grid.major = element_line(colour = 'gray', linetype = 'dotted'),
          strip.background = element_rect(fill = NA, colour = NA),
          strip.text = element_text(colour = 'black', face = 'plain', size = 13),
          plot.background = element_rect(fill = NA, colour = 'black', size = 0.25))

但是,生成的图表包含奇数行: ggplot with unwated lines 我想知道:

  1. 为什么会出现这些线?
  2. 如何删除它们?

0 个答案:

没有答案