[R] Weighted violin chart

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


It seems that using weight in ggplot has no effect. Also, using weight parameter in geom_violin results in an error.


> mydata
     V1 V2 V3
P1 73.6  5  R
P2 75.2  5  R
P3  6.5 60  R
P4 41.4 15  C
P5  5.4  5  C
P6 18.8 10  C
> p <- ggplot(mydata, aes(x=V3, y=V1)) + geom_violin(trim=FALSE)
> p
> p <- ggplot(mydata, aes(x=V3, y=V1), weight=V2) + geom_violin(trim=FALSE)
> p

In the code above, there is no difference between the two charts.
Also, the following code ends up in an error

> p <- ggplot(mydata, aes(x=V3, y=V1)) + geom_violin(trim=FALSE, weight=mydata$V2)
> p
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.
> mydata$V2
[1] 50 20  5 10 10  5


Any idea to fix that?


Regards,
Mahmood


________________________________

>>________________________________
>>From: Jeff Newmiller <jdnewmil using dcn.davis.ca.us>
>>Sent: Thursday, April 15, 2021 4:13:10 PM
>>To: r-help using r-project.org; Mahmood Naderan-Tahan; r-help using r-project.org
>>Subject: Re: [R] Weighted violin chart
>>
>>Not strictly on topic on this list (ggplot2 is a contributed package)
>>but...
>>
>>ggplot(mydata, aes(x=V3, y=V1, weight=V2 )) + geom_violin(trim=FALSE)
>>
>>If you want to refer to variables in the data, they have to be listed
>>in the mapping.
>>
>>On April 15, 2021 7:01:45 AM PDT, Mahmood Naderan-Tahan
>><mahmood.naderan using ugent.be> wrote:
>>>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]]
>>>
>>>______________________________________________
>>>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.
>>
>>--
>>Sent from my phone. Please excuse my brevity.
>
>--
>Sent from my phone. Please excuse my brevity.

--
Sent from my phone. Please excuse my brevity.

	[[alternative HTML version deleted]]



More information about the R-help mailing list