1

「ペンワールドテーブル」からの(マクロ経済の)年間データがあります。日付ラベルに問題があります。以下に示すように、日付は 10 進数で表されます。私はそれを修正するために何度か試みましたが、何度も失敗しました: 私はあなたに助けを求めます.

ここに画像の説明を入力

これは、「日付」(2000、2001 などの整数) が としてnumericではなく として扱われるためだと思いますdatesしたがって、私の主な問題は、データフレーム内の日付形式を修正して、簡単にプロットできるようにすることです。

pwt がデータフレームの名前を示し、 year が「日付」を格納する列を示している場合、これは私が試したものですが、成功しませんでした:

pwt$year <- strptime(pwt$year, format = "%Y")
pwt$year <- as.Date(as.character(pwt$year), format("%Y"), origin = "1970-01-01")
pwt$year <- as.Date(pwt$year, format='%Y-01-01', origin = "1970-01-01")
pwt$year <- as.yearmon(pwt$year) # requires zoo package

再現可能なコード

ではデータを紹介します。データを再作成する必要がある手順を示します。

### Define directories
 if(.Platform$OS.type == "windows"){
   currentdir <- "c:/R/pwt"
 } else {
 currentdir <- "~/R/pwt"}
 setwd(currentdir)

# download and save data in current directory
download.file("http://www.rug.nl/research/GGDC/data/pwt/V80/pwt80.xlsx", "pwt80.xlsx", mode="wb")
# **Edit** binary mode "wb" needed!

# convert and save the data sheet in csv format
library(gdata)
installXLSXsupport() # support for xlsx format
DataSheet <- read.xls("pwt80.xlsx", sheet="Data") # load the Data sheet only
write.csv(DataSheet, file=paste("pwt80", "csv", sep="."), row.names=FALSE)

# read pwt80.csv data stored in current directory
pwt80 <- read.csv(paste(currentdir, "pwt80.csv", sep="/"))

# use -subset- to get specifc countries and variables.
countries <- c("ESP", "ITA")
variables <- c("country", "countrycode", "year", "rgdpo", "pop")
pwt <- subset(#
  pwt80
  , countrycode %in% countries
  , select = variables
)#

上記の国のサブサンプルについて、1 人あたりの GDP をプロットすることに関心があります。したがって、これを行うためのコードがいくつかあります。

# Plot data with qplot
library(ggplot2)
qp <- qplot(#
  year
  , rgdpo/pop
  , data = subset(pwt80, countrycode %in% countries)
  , geom = "line"
  , group = countrycode
  , color = as.factor(countrycode)
)#
qp <- qp + 
  xlab("") + 
  ylab("Real GDP Per Capita (international $, 2005 prices, chain)") + 
  theme(legend.title = element_blank()) + 
  coord_trans(y = "log10")

この時点では日付は問題ないように見えますが、xlim と ylim で「ズーム」すると問題が発生し始めます。

qp <- qp + xlim(2000,2010) + ylim(22000,35000)
qp

qplot の代わりに ggplot を使用すると、同じ問題が発生します。

# Plot data with ggplot
ggp <- ggplot(pwt,aes(x=year,y=rgdpo/pop,color=as.factor(countrycode),group=countrycode)) + 
  geom_line()  
ggp <- ggp + 
  xlab("") + 
  ylab("Real GDP Per Capita (international $, 2005 prices, chain)") + 
  theme(legend.title = element_blank()) + 
  coord_trans(y = "log10")
ggp

ggp <- ggp + xlim(2000,2010) + ylim(22000,35000)
ggp

編集:xtsオブジェクトに関連する質問を削除しました。dput()短くする質問を削除しました。

4

1 に答える 1