r - ggplot2 - How to boxplot two variables with different scales on same plot using a primary and secondary y-axis? -


datalink:

data

code:

    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 = element_blank()) +      theme(strip.text.x = element_text(size = 14, hjust = 0.5, vjust = 0.5, face = 'bold')) +     facet_wrap(~region, scales="free_y") 

outcome: plot result

question:

my 2 variables plotted, yield (%change) , ev (in million us$) have different units. there way add secondary y-axis 1 of variable display each variable boxplot representing original units, within same plot? feature exist in ggplot2 begin with?

thanks!

with ggplot2 can't make 2 y axis (see explanation/discussion).

to represent different axis values ev , yield have plot them in separate plots. 1 way achieve use facet_wrap() , combine region , variable values. facet_wrap() both plots , able control number of columns show.

ggplot(data = distevyield.f, aes(factor(1), value)) +   geom_boxplot() +  facet_wrap(region~variable,scales="free",ncol=8)+   theme(axis.text.x=element_blank(),         axis.title.x=element_blank(),          axis.title.y = element_blank()) 

enter image description here


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -