数据链接:使用 的数据
我的代码:
ccfsisims <- read.csv(file = "F:/Purdue University/RA_Position/PhD_ResearchandDissert/PhD_Draft/GTAP-CGE/GTAP_NewAggDatabase/NewFiles/GTAP_ConsIndex.csv", header=TRUE, sep=",", na.string="NA", dec=".", strip.white=TRUE)
ccfsirsts <- as.data.frame(ccfsisims)
ccfsirsts[6:24] <- sapply(ccfsirsts[6:24],as.numeric)
ccfsirsts <- droplevels(ccfsirsts)
ccfsirsts <- transform(ccfsirsts,sres=factor(sres,levels=unique(sres)))
library(ggplot2)
#------------------------------------------------------------------------------------------
#### Plot of food security index for Morocco and Turkey by sector
#------------------------------------------------------------------------------------------
#_Code_Begin...
datamortur <- melt(ccfsirsts[ccfsirsts$region %in% c("TUR","MAR"), ]) # Selecting regions of interest
datamortur1 <- datamortur[datamortur$variable %in% c("pFSI2"), ] # Selecting the food security index of interest
datamortur2 <- datamortur1[datamortur1$sector %in% c("wht","gro","VegtFrut","osd","OthCrop","VegtOil","XPrFood"), ] # Selecting food sectors of interest
datamortur3 …Run Code Online (Sandbox Code Playgroud) 我有一个数据帧,我需要根据条件语句添加一个因子列.这是数据.
码:
morstats.agri.f <- moroccostats[c("year","agVA_g","agVA_ppp_g")]
morstats.agri.f
Run Code Online (Sandbox Code Playgroud)
题:
那么,我想在数据框中添加一个"periodframe"列,它有两个条目:"1991之前"和"1991之后",基于"年"栏的条件?
数据框如下所示:
year agVA_g agVA_ppp_g
1 1960 0.00 0.000000000
2 1961 0.00 0.000000000
3 1962 0.00 0.000000000
4 1963 0.00 0.000000000
5 1964 0.00 0.000000000
6 1965 -0.13 -0.160505952
7 1966 0.09 0.065780672
8 1967 0.10 0.075941092
9 1968 -0.04 -0.064963044
10 1969 0.11 0.084530984
11 1970 0.19 0.161963328
12 1971 0.12 0.097397145
13 1972 0.19 0.160263118
14 1973 0.20 0.172040051
15 1974 0.01 -0.012005158
16 1975 0.14 0.111609284
17 1976 …Run Code Online (Sandbox Code Playgroud) 我使用下面的附加数据来生成boxplot.数据链接 https://www.dropbox.com/s/dt1nxnkhq90nea4/GTAP_Sims.csv
到目前为止,我有我正在使用的代码:
# Distribution of EV for all regions under the BASE scenario
evBASE.f <- subset(ccwelfrsts, tradlib =="BASE")
p <- ggplot(data = evBASE.f, aes(factor(region), ev))
p + geom_boxplot() +
theme(axis.text.x = element_text(colour = 'black', angle = 90, size = 16)) +
theme(axis.text.y = element_text(colour = 'black', size = 16))
Run Code Online (Sandbox Code Playgroud)
它再现了一个看起来的情节:绘图文件:/// C:/Users/iouraich/Documents/ggplot_Results.htm
我在这里寻找的是让图中的x轴与csv文件中标题"region"的顺序相匹配.
ggplot中有任何选项可以控制吗?
非常感谢
我想问一下如何修复问题标题中描述的这个bug?就在昨天,代码工作正常,绘图程序产生了所需的图形.今天醒来并尝试添加一些功能,然后收到错误消息.
任何线索,为什么以及如何解决这个问题?
谢谢
数据链接: Data.csv
码:
# Loading data
morStats <- read.csv(file = "F:/Purdue University/RA_Position/PhD_ResearchandDissert/PhD_Draft/DissertationDraft/MoroccoCGE-CC_Stats.csv", header=TRUE, sep=",", na.string="NA", dec=".", strip.white=TRUE)
# Transferring the .csv data into data frames
moroccostats <- as.data.frame(morStats)
# Changing the data in the dataframe to "as.numeric"
moroccostats[3:38] <- sapply(moroccostats[3:38],as.numeric)
moroccostats <- droplevels(moroccostats)
# reorder
moroccostats <- transform(moroccostats,year=factor(year,levels=unique(year)))
# Load packages
library(reshape2)
library(ggplot2)
library(lattice)
library(grid)
library(plyr)
library(gridExtra)
library(scales)
#----------------------------------------------------------------------
# Figure 1: Evolution of population (in absolute terms) 1960-2050
#---------------------------------------------------------------------
#_Code_Begin...
moroccostats.f <- melt(moroccostats, id="year")
morstats.pop <- …Run Code Online (Sandbox Code Playgroud) SpatialPoly数据:SpatialData
产量数据:产量数据
码:
## Loading packages
library(rgdal)
library(plyr)
library(maps)
library(maptools)
library(mapdata)
library(ggplot2)
library(RColorBrewer)
library(foreign)
library(sp)
## Loading shapefiles and .csv files
#Morocco <- readOGR(dsn=".", layer="Morocco_adm0")
MoroccoReg <- readOGR(dsn=".", layer="Morocco_adm1")
MoroccoYield <- read.csv(file = "F:/Purdue University/RA_Position/PhD_ResearchandDissert/PhD_Draft/Country-CGE/RMaps_Morocco/Morocco_Yield.csv", header=TRUE, sep=",", na.string="NA", dec=".", strip.white=TRUE)
## Reorder the data in the shapefile based on the category variable "ID_1" and change to dataframe
MoroccoReg <- MoroccoReg[order(MoroccoReg$ID_1), ]
MoroccoReg.df <- fortify(MoroccoReg)
## Add the yield impacts column to shapefile from the .csv file by "ID_1" …Run Code Online (Sandbox Code Playgroud) 地图数据:InputSpatialData
Yield数据:InputYieldData
Results_using viewport(): 
编辑:使用@rawr建议的"multiplot"函数的结果(参见下面的评论).我确实喜欢新的结果,特别是地图更大.尽管如此,箱形图似乎与地图仍未对齐.是否有更系统的方法来控制居中和放置?

我的问题:有没有办法控制箱线图的大小,使其接近大小并以上方的地图为中心?
FullCode:
## Loading packages
library(rgdal)
library(plyr)
library(maps)
library(maptools)
library(mapdata)
library(ggplot2)
library(RColorBrewer)
library(foreign)
library(sp)
library(ggsubplot)
library(reshape)
library(gridExtra)
## get.centroids: function to extract polygon ID and centroid from shapefile
get.centroids = function(x){
poly = wmap@polygons[[x]]
ID = poly@ID
centroid = as.numeric(poly@labpt)
return(c(id=ID, long=centroid[1], lat=centroid[2]))
}
## read input files (shapefile and .csv file)
wmap <- readOGR(dsn=".", layer="ne_110m_admin_0_countries")
wyield <- read.csv(file = "F:/Purdue University/RA_Position/PhD_ResearchandDissert/PhD_Draft/GTAP-CGE/GTAP_Sims&Rests/NewFiles/RMaps_GTAP/AllWorldCountries_CCShocksGTAP.csv", header=TRUE, sep=",", na.string="NA", dec=".", strip.white=TRUE)
wyield$ID_1 <- substr(wyield$ID_1,3,10) # Eliminate the …Run Code Online (Sandbox Code Playgroud) 数据:数据
码:
#function that calculates ‘the average of the sample marginal effects’.
mfxboot <- function(modform,dist,data,boot=1000,digits=3){
x <- glm(modform, family=binomial(link=dist),data)
# get marginal effects
pdf <- ifelse(dist=="probit",
mean(dnorm(predict(x, type = "link"))),
mean(dlogis(predict(x, type = "link"))))
marginal.effects <- pdf*coef(x)
# start bootstrap
bootvals <- matrix(rep(NA,boot*length(coef(x))), nrow=boot)
set.seed(1111)
for(i in 1:boot){
samp1 <- data[sample(1:dim(data)[1],replace=T,dim(data)[1]),]
x1 <- glm(modform, family=binomial(link=dist),samp1)
pdf1 <- ifelse(dist=="probit",
mean(dnorm(predict(x, type = "link"))),
mean(dlogis(predict(x, type = "link"))))
bootvals[i,] <- pdf1*coef(x1)
}
res <- cbind(marginal.effects,apply(bootvals,2,sd),marginal.effects/apply(bootvals,2,sd))
if(names(x$coefficients[1])=="(Intercept)"){
res1 <- res[2:nrow(res),]
res2 <- …Run Code Online (Sandbox Code Playgroud) 数据链接:https: //www.dropbox.com/s/rvwq3uw0p14g9c6/GTAP_Macro.csv
码:
ccmacrosims <- read.csv(file = "F:/Purdue University/RA_Position/PhD_ResearchandDissert/PhD_Draft/GTAP-CGE/GTAP_NewAggDatabase/NewFiles/GTAP_Macro.csv", header=TRUE, sep=",", na.string="NA", dec=".", strip.white=TRUE)
ccmacrorsts <- as.data.frame(ccmacrosims)
ccmacrorsts[6:10] <- sapply(ccmacrorsts[6:10],as.numeric)
ccmacrorsts <- droplevels(ccmacrorsts)
ccmacrorsts <- transform(ccmacrorsts,region=factor(region,levels=unique(region)))
library(ggplot2)
#Data manipulations to select variables of interest within the dataframe
GDPtradlib1 <- melt(ccmacrorsts[ccmacrorsts$region %in% c("EAsia","USA","OecdEU","XMidEast","FrmUSSR","EastEU","TUR","MAR"), ])
GDPtradlib2 <- GDPtradlib1[GDPtradlib1$sres %in% c("AVERAGE"), ]
GDPtradlib.f <- GDPtradlib2[GDPtradlib2$variable %in% c("GDP"), ]
GDPtradlib.f <- subset(GDPtradlib.f, tradlib != "BASEDATA")
GDPtradlib.f[1:20,]
#Plotting
plot <- ggplot(data = GDPtradlib.f, aes(x=factor(tradlib), y=value) +
plot + geom_bar(stat="identity") + facet_wrap(~region, scales="free_y")
Run Code Online (Sandbox Code Playgroud)
问题:我正在尝试 …
该数据
代码
#
# This is code for mapping of CGE_Morocco results
#
# rm(list = ls(all = TRUE)) # don't use this in code that others will copy/paste
## Loading packages
library(rgdal)
library(plyr)
library(maps)
library(maptools)
library(mapdata)
library(ggplot2)
library(RColorBrewer)
## Loading shape files administrative coordinates for Morocco maps
#Morocco <- readOGR(dsn=".", layer="Morocco_adm0")
MoroccoReg <- readOGR(dsn=".", layer="Morocco_adm1")
## Reorder the data in the shapefile based on the regional order
MoroccoReg <- MoroccoReg[order(MoroccoReg$ID_1), ]
## Add the yield impacts column to …Run Code Online (Sandbox Code Playgroud) 数据链接:
码:
distevyield <- read.csv(file = "F:/Purdue University/RA_Position/PhD_ResearchandDissert/PhD_Draft/GTAP-CGE/GTAP_NewAggDatabase/NewFiles/GTAP_DistEVYield.csv", header=TRUE, sep=",", na.string="NA", dec=".", strip.white=TRUE)
str(distevyield)
distevyield <- as.data.frame(distevyield)
distevyield[5:6] <- sapply(distevyield[5:6],as.numeric)
distevyield <- droplevels(distevyield)
distevyield <- transform(distevyield,region=factor(region,levels=unique(region)))
library(ggplot2)
distevyield.f <- melt(subset(distevyield, region !="World"))
Figure3 <- ggplot(data = distevyield.f, aes(factor(variable), value))
Figure3 + geom_boxplot() +
theme(axis.text.x = element_text(colour = 'black', angle = 90, size = 15, hjust = 1, vjust = 0.5),axis.title.x = element_blank()) +
theme(axis.text.y = element_text(colour = 'black', size = 15, hjust = 0.5, vjust = 0.5), axis.title.y = …Run Code Online (Sandbox Code Playgroud) 我想将两个.shp文件转换为一个允许我一起绘制地图的数据库.
另外,有没有办法将.shp文件转换为.csv文件?我想能够个性化和添加一些数据,这对我来说更容易.csv格式.如果要在地图上添加叠加产量数据和降水数据,我会想到什么.
用于绘制两个文件的代码:
# This is code for mapping of CGE_Morocco results
# Loading administrative coordinates for Morocco maps
library(sp)
library(maptools)
library(mapdata)
# Loading shape files
Mor <- readShapeSpatial("F:/Purdue University/RA_Position/PhD_ResearchandDissert/PhD_Draft/Country-CGE/MAR_adm1.shp")
Sah <- readShapeSpatial("F:/Purdue University/RA_Position/PhD_ResearchandDissert/PhD_Draft/Country-CGE/ESH_adm1.shp")
# Ploting the maps (raw)
png("Morocco.png")
Morocco <- readShapePoly("F:/Purdue University/RA_Position/PhD_ResearchandDissert/PhD_Draft/Country-CGE/MAR_adm1.shp")
plot(Morocco)
dev.off()
png("WesternSahara.png")
WesternSahara <- readShapePoly("F:/Purdue University/RA_Position/PhD_ResearchandDissert/PhD_Draft/Country-CGE/ESH_adm1.shp")
plot(WesternSahara)
dev.off()
Run Code Online (Sandbox Code Playgroud)

在查看@AriBFriedman和@PaulHiemstra的建议并随后弄清楚如何合并.shp文件后,我设法使用以下代码和数据生成以下映射(对于.shp数据,参见上面的链接)
码:
# Merging Mor and Sah .shp files into one .shp file
MoroccoData <- rbind(Mor@data,Sah@data) # First, 'stack' the attribute list …Run Code Online (Sandbox Code Playgroud)