Melt + ggplot2 Problem !!!

27 views
Skip to first unread message

nserdar

unread,
Mar 9, 2013, 10:20:38 AM3/9/13
to ggp...@googlegroups.com

Hi,

I need to help about melt function !!!

My data set includes 18 different Countries, divided  into 2 Groups

Developed:"France","Germany","Italy","Japan","Norway","Sweden","Switzerland","UK","USA",
Emerging: "Brazil","Chile","India","Korea","Malaysia","Mexico","Poland","Russia","SAfrica"

Goal: Boxplot plots of 18 Countries based on 2 Groups and only 1 plot, Like attachment doc!!!

 After using Melt function, the following result obtained as;

> str(datam)

'data.frame': 9396 obs. of  4 variables:
 $ Date  : Date, format: "2002-07-24" "2002-07-31" "2002-08-07" "2002-08-14" ...
 $ Market: Factor w/ 18 levels "France","Germany",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ Beta  : num  1.676 1.536 1.32 0.728 1.516 ...
 $ Group : Factor w/ 2 levels "Developed Market",..: 1 1 1 1 1 1 1 1 1 1 ...

My code:

mr<-matrix(0,522,18)


i=1


while (i<=18){


mr[,i]<-(lmr[,((i*2))])


i<-i+1


}


n<-length(mr)

sep<-as.Date(sp[2:523,1],format="%d/%m/%Y")

group<-rep(c("Developed Market","Emerging Market"),c(4698,4698))

datam<-data.frame(mr,sep)

colnames(datam)<-c("France","Germany","Italy","Japan","Norway","Sweden","Switzerland","UK","USA","Brazil","Chile","India","Korea","Malaysia","Mexico","Poland","Russia","SAfrica","Date")

datam<-melt(datam,id=c("Date"))

datam<-cbind(datam,group)

colnames(datam)<-c("Date","Market","Beta","Group")


qplot(Market,Beta,data=datam,geom="boxplot",colour=Market,main="KFMR",ylab=expression(beta),facets=~Group)+theme(axis.text.x=element_text(angle=90,size=8),axis.title.x=element_text(size=12))+theme(legend.text=element_text(colour="black",size=6),legend.position="none")


Please let know how to omit emerging countries ("Brazil","Chile","India","Korea","Malaysia","Mexico","Poland","Russia","SAfrica") from

developed group plot or vice versa.


Regards,

Ser


Rplot.pdf

Ista Zahn

unread,
Mar 9, 2013, 10:38:52 AM3/9/13
to nserdar, ggp...@googlegroups.com
Hi Ser,

When I try to reproduce your graph I get

Error: object 'lmr' not found

Please make a reproducible example so we can investigate your issue
more easily. See
https://github.com/hadley/devtools/wiki/Reproducibility if you need
help making your example reproducible.

Best,
Ista
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Dennis Murphy

unread,
Mar 9, 2013, 11:57:37 AM3/9/13
to nserdar, ggp...@googlegroups.com
Hi:

Your problem has nothing to do with melt() per se; rather, it has to
do with your choice of x scale within each facet. Here's a simple toy
example that uses ggplot() rather than qplot():

library(ggplot2)
DF <- data.frame(country = rep(LETTERS[1:4], each = 50),
region = rep(1:2, each = 100),
y = rnorm(200))
# ...what you apparently want
ggplot(DF, aes(x = country, y = y)) + geom_boxplot() +
facet_wrap(~ region, scales = "free_x")
# ...as opposed to what you got
ggplot(DF, aes(x = country, y = y)) + geom_boxplot() +
facet_wrap(~ region)

I don't use qplot() very often, but I imagine you'd have to declare it
as a separate layer rather than within the qplot() call in order to
specify the scales = argument.

Dennis
Reply all
Reply to author
Forward
0 new messages