本文介绍了使用facet_wrap()绘制多个barplot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! DataLink: 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)#在数据框中选择感兴趣变量的数据操作 GDPtradlib1< - melt(ccmacrorsts [ccmacrorsts $ region%in%c(EAsia ,美国,OecdEU,XMidEast,FrmUSSR,EastEU,TUR,MAR)]] GDPtradlib2< - GDPtradlib1 [GDPtradlib1 $ sres%in%c(AVERAGE),] GDPtradlib.f GDPtradlib.f < - 子集(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) 问题:我试图绘制变量 GDP (y_axis)by tradlib 方案(x_axis)为每个区域并使用 facet_wrap()来生成多个条形图。 / b> R消息错误: + plot 错误:意外符号在:plot + geom_bar(stat =identity)+ facet_wrap(〜region ,scales =free_y) plot> plot + geom_bar(stat =identity):二元运算符的非数值参数plot + geom_bar(stat =identity)+ facet_wrap(〜region,scales =free_y) 解决方案您缺少)在 y = value之后。 除此之外,下面的图片: 我只用了 gdata 库去掉了关卡。 DataLink:https://www.dropbox.com/s/rvwq3uw0p14g9c6/GTAP_Macro.csvCode: 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")Question: I am trying to plot the variable GDP (y_axis) by tradlib scenario (x_axis) for each region and using facet_wrap() to produce multiple barplots.R-message error: + plot <- ggplot(data = GDPtradlib.f, aes(x=factor(tradlib), y=value) +Error: unexpected symbol in:"plot + geom_bar(stat="identity") + facet_wrap(~region, scales="free_y")plot"> plot + geom_bar(stat="identity") + facet_wrap(~region, scales="free_y")Error in plot + geom_bar(stat = "identity") : non-numeric argument to binary operator 解决方案 You are missing a ) after y=value).Apart from that your code works, it produced the image below:The only difference from your code and the code I ran is, I used the gdata library to drop the levels. 这篇关于使用facet_wrap()绘制多个barplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-01 23:07