Using "weight" in violin chart

130 views
Skip to first unread message

Mahmood Naderan

unread,
Apr 15, 2021, 12:17:39 PM4/15/21
to ggplot2

Hi
I tried to use "weight" parameter to plot a weighted violin chart. However, in the following code, there is not difference between having "weight=V2" and not having that.

> library(ggplot2)
> mydata <- read.csv('test.csv', header=T,row.names=1)
> mydata
     V1 V2 V3
P1 73.6 50  R
P2 75.2 20  R
P3  6.5  5  R
P4 41.4 10  C
P5  5.4 10  C
P6 18.8  5  C
> p <- ggplot(mydata, aes(x=V3, y=V1)) + geom_violin(trim=FALSE)
> p + geom_dotplot(binaxis='y', stackdir='center', dotsize=1)
`stat_bindot()` using `bins = 30`. Pick better value with `binwidth`.
> p <- ggplot(mydata, aes(x=V3, y=V1), weight=V2) + geom_violin(trim=FALSE)
> p + geom_dotplot(binaxis='y', stackdir='center', dotsize=1)
`stat_bindot()` using `bins = 30`. Pick better value with `binwidth`.

Did I miss something? It seems that "weight" has no effect.

Regards,
Mahmood

Oscar de Lama

unread,
Apr 17, 2021, 8:55:20 PM4/17/21
to ggp...@googlegroups.com
Hi Mahmood.

 You have to put the weight setting inside the call to `aes(...., weight=V2)`. This is because, with aes(), you map aesthetics with data variables like V2. It should raise an error for your code, like when you put `color=V2` outside of aes. Weirdly enough, it doesn't. I think that is a bug.

Cheers,

Oscar
--
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/ggplot2/290496b1-8bc6-409b-a987-fd5980089f6an%40googlegroups.com.

Mahmood Naderan

unread,
Apr 18, 2021, 4:02:19 PM4/18/21
to Oscar de Lama, ggp...@googlegroups.com
Hi Oscar,
Thanks for that. You are right.

Regards,
Mahmood





Reply all
Reply to author
Forward
0 new messages