iouraich
iouraich

Reputation: 3034

plotting multiple barplot using facet_wrap ()

DataLink: https://www.dropbox.com/s/rvwq3uw0p14g9c6/GTAP_Macro.csv

Code:

     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

Upvotes: 3

Views: 7824

Answers (1)

Harpal
Harpal

Reputation: 12577

You are missing a ) after y=value).

Apart from that your code works, it produced the image below:enter image description here

The only difference from your code and the code I ran is, I used the gdata library to drop the levels.

Upvotes: 5

Related Questions