[R] Weighted violin chart

Mahmood Naderan-Tahan m@hmood@n@der@n @end|ng |rom ugent@be
Thu Apr 15 16:01:45 CEST 2021


Hi again,

As a follow up, does anybody know how to fix the following error?

It seems that I can use a "weight" parameter in geom_violin, but I am able to  figure out what is the problem with the following code.



> 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, weight=V2)
Error in layer(data = data, mapping = mapping, stat = stat, geom = GeomViolin,  :
  object 'V2' not found
>
>
> wg <- as.factor(mydata$V2)
> p <- ggplot(mydata, aes(x=V3, y=V1)) + geom_violin(trim=FALSE, weight=wg)
> p + geom_dotplot(binaxis='y', stackdir='center', dotsize=1)
`stat_bindot()` using `bins = 30`. Pick better value with `binwidth`.
Error: Aesthetics must be either length 1 or the same as the data (1024): weight
Run `rlang::last_error()` to see where the error occurred.





Regards,
Mahmood


________________________________
From: Mahmood Naderan-Tahan
Sent: Monday, April 12, 2021 10:01 PM
To: r-help using r-project.org
Subject: Weighted violin chart


Hi,

I would like to know if it is possible to plot a weighted violin chart with R. Currently, I have


> 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)

I would like to use V2 as the weight vector. Any idea about that?



Regards,
Mahmood

	[[alternative HTML version deleted]]



More information about the R-help mailing list