new variables for geom bar plot

60 views
Skip to first unread message

j0hnCathey

unread,
Sep 1, 2020, 2:09:34 AM9/1/20
to ggplot2

I have two variables, ABO compatibility = yes or no (coded 1 and 2), Rh compatibility = yes or no  (coded 1 and 2). I need to create variables of the four possible combinations ABO yes + Rh yes, ABO yes + Rh no, ABO no + Rh yes, ABO no + Rh no, so that I can do a bar plot showing these four categories by another variable. Using dplyr mutate I can only get logicals, TRUE FALSE, which I can’t plot. I’ve searched but I don’t see any other way to make these new variables. 


Roman Luštrik

unread,
Sep 1, 2020, 3:01:26 AM9/1/20
to j0hnCathey, ggplot2
Hey John,

would recoding the Rhesus factor compatibility to yes no using `ifelse` work? How are you creating this new variable?

Cheers,
Roman

On Tue, Sep 1, 2020 at 8:09 AM j0hnCathey <jtca...@gmail.com> wrote:

I have two variables, ABO compatibility = yes or no (coded 1 and 2), Rh compatibility = yes or no  (coded 1 and 2). I need to create variables of the four possible combinations ABO yes + Rh yes, ABO yes + Rh no, ABO no + Rh yes, ABO no + Rh no, so that I can do a bar plot showing these four categories by another variable. Using dplyr mutate I can only get logicals, TRUE FALSE, which I can’t plot. I’ve searched but I don’t see any other way to make these new variables. 


--
--
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/ad276bb5-870c-4f7f-8576-48aae3635836n%40googlegroups.com.


--
In God we trust, all others bring data.

Ron Crump

unread,
Sep 1, 2020, 4:07:43 AM9/1/20
to j0hnCathey, ggplot2
Hi,
Assuming a tibble with columns abo and rh, how about:

TBL <- TBL %>% mutate(cabo = ifelse(abo == 1, 'ABO yes', 'ABO no'),
crh = ifelse(rh == 1, 'Rh yes', 'Rh no'),
abo_rh = paste(cabo, crh, sep= ' + ')) %>% select(-cabo, -crh)

nb, I've just typed that there without testing it at all, so it
might be broken.

Ron.

jcred...@gmail.com

unread,
Sep 1, 2020, 12:38:01 PM9/1/20
to Ron Crump, j0hnCathey, ggplot2
I have a colleague that uses the most simple way of recoding for two levels: 
newvar<- var1 + 10*var2

Very quick and useful. Afterward, you only need to change the labels for newvar.

Regards,

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

Graeme Prentice-Mott

unread,
Sep 1, 2020, 12:45:07 PM9/1/20
to jcred...@gmail.com, Ron Crump, j0hnCathey, ggplot2
There’s also:

interaction(var1, var2, sep=‘-‘)

Best,
Graeme

On Sep 1, 2020, at 12:37 PM, jcred...@gmail.com wrote:


Reply all
Reply to author
Forward
0 new messages