本文介绍了绘制一个国家相对于另一个国家的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有几个国家和几年的时间序列数据,比如说意大利,西班牙,美国。我想将一些国家的数据绘制到另一个国家:比如说将意大利和西班牙的实际人均GDP作为美国的百分比进行绘图。 这就是数据的样子: head(pwt )国家isocode年份rgdpo流行 ESP-1950西班牙ESP 1950-01-01 85002.27 27.99278 ESP-1951西班牙ESP 1951-01-01 100241.94 28.22724 ESP-1952西班牙ESP 1952-01-01 105170.11 28.47847 ESP-1953西班牙ESP 1953-01-01 101322.59 28.73209 ESP-1954西班牙ESP 1954-01-01 114573.78 28.98774 ESP-1955西班牙ESP 1955 -01-01 120839.95 29.24542 这里的利息变量人均实际GDP作为 rgdpo / pop 可悲的是我并没有走得太远。我知道如何选择一个整列,例如 pwt ['rgdpo'] 或 pwt $ rgdpo ,但不知道如何限制这个特定国家完全拆除数据帧。 (我会知道如何通过使用子集函数为每个国家创建变量,然后通过划分然后重新创建数据框然后创建相关变量,然后绘图,但是我想要学习在这里做事情的聪明方式)。 p> 我希望这个解决方案对于NAs的存在或缺少的日期有效(缺失的日期可以由NAs取代) 我在我的例子中使用了ggplot2,但我也开放了基础-R解决方案(作者:Hadley Wickham,Winston Chang, http://cran.r-project.org/web/packages/ggplot2/ )。 为了获得可重复的示例,我从pwt8软件包获取数据(作者:Achim Zeileis, http://cran.r-project.org/web/packages/pwt8/ )。 #获取数据#install.packages(pwt8) library(pw t8)数据(pwt8.0)#名称(pwt8.0) #使用-subset-获取特定的国家和变量。 国家变量 pwt< - 子集(pwt8.0,%countries中的isocode%,select =变量) #使用ggplot绘制GDP PER CAPITA library(ggplot2 ) pwt $ year< -as.Date(paste0(pwt $ year, - 01-01),format =%Y-%m-%d)#year作为日期 ggp < - ggplot(pwt,aes(x = year,y = rgdpo / pop,color = as.factor(isocode),group = isocode))+ geom_line() ggp< - ggp + xlab()+ ylab()+ ggtitle(实际人均国内生产总值(2005年国际价格,连锁))+ 主题legend.title = element_blank())+ coord_trans(y =log10) ggp< - ggp + coord_cartesian(xlim = as.Date(c(2000-01-01, yim = c(22000,45000)) ggp 解答:感谢Hong Ooi! require(plyr)$ b $ pwt library(ggplot2) ggp< - ggplot(subset(pwt,isocode == c(ESP,ITA)),aes(x = year,y = gdppc。 usa,color = as.factor(isocode),group = isocode))+ geom_line() ggp< - ggp + xlab()+ ylab )+ ggtitle(实际国内生产总值相对于美国(国际$,2005年价格,连锁))+ 主题(legend.title = element_blank()) ggp 解决方案 $ b (子集(pwt8.0,isocode ==USA),gdppop require(plyr) usa< #发送到ggplot2 dat I have time series data for several countries and several years, say Italy, Spain, USA. I'd like to plot the data for some countries relative to another country: say plot real GDP per capita in Italy and Spain as a percentage of the USA.This is what the data looks like:head(pwt) country isocode year rgdpo popESP-1950 Spain ESP 1950-01-01 85002.27 27.99278ESP-1951 Spain ESP 1951-01-01 100241.94 28.22724ESP-1952 Spain ESP 1952-01-01 105170.11 28.47847ESP-1953 Spain ESP 1953-01-01 101322.59 28.73209ESP-1954 Spain ESP 1954-01-01 114573.78 28.98774ESP-1955 Spain ESP 1955-01-01 120839.95 29.24542The variable of interest here, "Real GDP Per Capita", is obtained as rgdpo/popSadly I didn't get very far. I know how to select a whole column, e.g. pwt['rgdpo'] or pwt$rgdpo, but then not sure how to restrict this to a particular country without completely dismantling the data frame. (I would know how to create variables for each country by using the subset function and then creating the relative variable by dividing and then recreating a dataframe and then plotting, but I would like to learn the smart way to do things here).I'd like the solution to be robust to the presence of NAs or to a missing date (missing dates could be replaced by NAs)I have used ggplot2 in my example, but I'm open minded to a base-R solution too (authors: Hadley Wickham, Winston Chang, http://cran.r-project.org/web/packages/ggplot2/).To obtain a reproducible example, I am getting data from the pwt8 package (author: Achim Zeileis, http://cran.r-project.org/web/packages/pwt8/).# Get data# install.packages("pwt8")library("pwt8")data("pwt8.0")# names(pwt8.0)# use -subset- to get specifc countries and variables.countries <- c("USA", "ESP", "ITA")variables <- c("country", "isocode", "year", "rgdpo", "pop")pwt <- subset(pwt8.0, isocode %in% countries, select = variables)# Plot GDP PER CAPITA with ggplotlibrary("ggplot2")pwt$year<-as.Date(paste0(pwt$year,"-01-01"),format="%Y-%m-%d") # year as Dateggp <- ggplot(pwt,aes(x=year,y=rgdpo/pop,color=as.factor(isocode),group=isocode)) +geom_line()ggp <- ggp +xlab("") +ylab("") +ggtitle("Real GDP Per Capita (international $, 2005 prices, chain)") +theme(legend.title = element_blank() ) +coord_trans(y = "log10")ggp <- ggp + coord_cartesian(xlim=as.Date(c("2000-01-01","2012-01-01")),ylim=c(22000,45000))ggpSolution: thanks to Hong Ooi!require("plyr")pwt <- ddply(pwt, .(country), transform, gdppc.usa=(rgdpo/pop)/within(subset(pwt, isocode=="USA"),gdppc<-rgdpo/pop)$gdppc)library("ggplot2")ggp <- ggplot(subset(pwt,isocode==c("ESP","ITA")),aes(x=year,y=gdppc.usa,color=as.factor(isocode),group=isocode)) +geom_line()ggp <- ggp + xlab("") + ylab("") + ggtitle("Real GDP Per Capita Relative to USA (international $, 2005 prices, chain)") + theme(legend.title = element_blank() )ggp 解决方案 Transform your data before plotting it:require(plyr)usa <- within(subset(pwt8.0, isocode=="USA"), gdppop <- rgdpo/pop)# send this to ggplot2dat <- ddply(pwt8.0, .(country), transform, gdppop_usa=(rgdpo/pop)/usa$gdppop) 这篇关于绘制一个国家相对于另一个国家的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-05 17:06