[R] Two geom_bar with counts to put in the same plot

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Thu Dec 5 16:36:50 CET 2019


Hello,

A ggplot graph follows almost exactly my previous code. The *only* 
difference is in facet_wrap(). See below.


library(ggplot2)

idv <- grep("part", names(DB)[-(3:4)], ignore.case = TRUE, value = TRUE)
dblong <- reshape2::melt(DB[-(3:4)], id.vars = idv)
dblong <- reshape2::melt(dblong, id.vars = c("variable", "value"))
names(dblong) <- c("deltaVar", "delta", "participationVar", "participation")
dblong <- dblong[complete.cases(dblong),]

ggplot(dblong, aes(x = delta, fill = deltaVar)) +
   geom_density(aes(alpha = 0.2)) +
   scale_alpha_continuous(guide = "none") +
   facet_wrap( ~ participation)


Hope this helps,

Rui Barradas

Às 10:19 de 05/12/19, Francesca escreveu:
> This is a consolation, because I cannot get it in ggplot either!
> Thanks for the code!
> 
> F.--------------------------------
> 
>> Il giorno 5 dic 2019, alle ore 11:17, Jim Lemon <drjimlemon using gmail.com> ha scritto:
>>
>> Sorry it's not ggplot, I couldn't work that one out.
>>
>> # using the data frame structure that Rui kindly added
>> # and perhaps Rui can work out how to do this in ggplot
>> DBcomplete<-DB[complete.cases(DB),]
>> library(plotrix)
>> png("fp.png")
>> par(mfrow=c(1,2))
>> density11_0<-density(DBcomplete$delta11_L[DBcomplete$participation1==0])
>> density2_0<-density(DBcomplete$delta2_L[DBcomplete$participation1==0])
>> plot(0,xlim=c(-30,50),ylim=c(0,max(density11_0$y)),type="n",
>> xlab="delta",ylab="density",main="participation == 0")
>> plot_bg("lightgray")
>> grid(col="white")
>> polygon(density11_0,col="#ff773344")
>> polygon(density2_0,col="#3377ff44")
>> density11_1<-density(DBcomplete$delta11_L[DBcomplete$participation1==1])
>> density2_1<-density(DBcomplete$delta2_L[DBcomplete$participation1==1])
>> plot(0,xlim=c(-30,50),ylim=c(0,max(density11_1$y)),type="n",
>> xlab="delta",ylab="density",main="participation == 1")
>> plot_bg("lightgray")
>> grid(col="white")
>> polygon(density11_1,col="#ff773344")
>> polygon(density2_1,col="#3377ff44")
>> par(cex=0.9)
>> legend(5,0.11,c("delta11_L","delta2_L"),fill=c("#ff773344","#3377ff44"))
>> dev.off()
>>
>> Jim
>>
>> On Thu, Dec 5, 2019 at 9:14 PM Francesca <francesca.pancotto using gmail.com> wrote:
>>>
>>> Exactly. I was trying to remelt data in the right way, but I could not get there yet. Can you suggest me this code?
>>> Thanks a lot
>>> F.
>>>
>>> ----------------------------------
>>>
>>> Il giorno 5 dic 2019, alle ore 11:11, Jim Lemon <drjimlemon using gmail.com> ha scritto:
>>>
>>> Hi Francesca,
>>> Do you want something like this?
>>>
>>> Jim
>>>
>>> On Thu, Dec 5, 2019 at 6:58 PM Francesca <francesca.pancotto using gmail.com> wrote:
>>>
>>>
>>> Hi, sorry for bothering again.
>>> I was wondering how I can reshape the data, if in your code,
>>> I would like to have only two panels, where in the panel with Participation =0, I represent delta11_L of participation1==0
>>> and delta2_L of participation2==0, and in the right panel, I want Participation=1, but representing together
>>> delta11_L of participation1==1, and delta2_L of participation2==1.
>>>
>>> I get messed up with the joint melting of participation, which determines the facet, but then I cannot assign the proper fill to the density plots which depend on it, and on the other hand I would like to have in the same plot with mixed participation.
>>>
>>> I hope it is clear.
>>> Nonetheless, the previous plot is useful to understand something I had not thought about.
>>> Thanks again for your time.
>>> F.
>>> ----------------------------------
>>>
>>> <fp.png>
>>>
>>>
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list