質問

DataLink: https://www.dropbox.com/s/ql5jw7eng3plrso/GTAP_MacroValueChange.csv

Code:

    library(ggplot2)
    library(grid)

    #Upload data
    ccmacrosims2 <- read.csv(file = "F:/Purdue University/RA_Position/PhD_ResearchandDissert/PhD_Draft/GTAP-CGE/GTAP_NewAggDatabase/NewFiles/GTAP_MacroValueChange.csv", header=TRUE, sep=",", na.string="NA", dec=".", strip.white=TRUE)

    #Data manipulation for analysis
    ccmacrorsts2 <- as.data.frame(ccmacrosims2)
    ccmacrorsts2[6:10] <- sapply(ccmacrorsts2[6:10],as.numeric)
    ccmacrorsts2 <- droplevels(ccmacrorsts2)
    ccmacrorsts2 <- transform(ccmacrorsts2,region=factor(region,levels=unique(region)))

    #Selecting data of interest
    GDPDecomp1 <- melt(ccmacrorsts2[ccmacrorsts2$region %in% c("TUR","MAR"), ]) 
    GDPDecomp2 <- GDPDecomp1[GDPDecomp1$sres %in% c("AVERAGE"), ]
    GDPDecomp.f <- subset(GDPDecomp2, variable !="GDP")

    #Ploting
    GDPDecompPlot <- ggplot(data = GDPDecomp.f, aes(factor(region),value,  fill=variable))
    GDPDecompPlot + geom_bar(stat="identity", position="stack") + facet_wrap(~tradlib, scales="free_y") +
    theme(axis.text.x = element_text(colour = 'black', angle = 90, size = 12, hjust = 0.5, vjust = 0.5),axis.title.x=element_blank()) + 
    ylab("GDP (Change in $US million)") + theme(axis.text.y = element_text(colour = 'black', size = 12), axis.title.y = element_text(size = 12)) + 
    theme(strip.text.x = element_text(size = 12, hjust = 0.5, vjust = 0.5, face = 'bold'))

Issue at hand: The barplot using ggplot (see below) ggplot_barplot

excel_barplot: excel_barplot_sample

It seems that the plot does not really represent correctly the values of the data. What I am looking for is something like what we get from excel_barplot_sample. For instance, comparing the panel "TRLIBEU" under ggplot with its counterpart using excel, one can clearly notice that the ggplot does not capture the values in the data correctly when stacking.

Any help on how to correct for the discrepancy?

Thanks in advance

役に立ちましたか?

解決

If you need to stack bars in ggplot2 with negative values then for better result you should make two new data frames - one for positive values and second for negative values.

GDPDecomp.f.pos<-GDPDecomp.f[GDPDecomp.f$value>0,]
GDPDecomp.f.neg<-GDPDecomp.f[GDPDecomp.f$value<0,]

Then use each data frame in its own geom_bar() call.

ggplot()+
  geom_bar(data=GDPDecomp.f.pos,aes(x=factor(region),y=value,fill=variable),stat="identity")+
  geom_bar(data=GDPDecomp.f.neg,aes(x=factor(region),y=value,fill=variable),stat="identity")+
  facet_wrap(~tradlib, scales="free_y") + 
  theme(axis.text.x = element_text(colour = 'black', angle = 90, size = 12, hjust = 0.5, vjust = 0.5),axis.title.x=element_blank()) + 
  ylab("GDP (Change in $US million)") + theme(axis.text.y = element_text(colour = 'black', size = 12), axis.title.y = element_text(size = 12)) + 
  theme(strip.text.x = element_text(size = 12, hjust = 0.5, vjust = 0.5, face = 'bold'))

enter image description here

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top