ggplot2 with expression: conditional superscript the tick labels in scale_*_discrete

191 views
Skip to first unread message

Jakhongir Alidjanov

unread,
Jul 7, 2020, 11:17:14 AM7/7/20
to ggplot2
Hi everyone!

I am trying to label the ticks on the x-axis using the following line of code:
scale_x_discrete("Results of the urine culture", labels=c(expression("<"~10^4~"CFU/ml"), expression(10^4~"-"~10^5~"CFU/ml"), expression(">"~10^6~"CFU/ml")))
The graph returns with the same value (the first in the expression, i.e. "<10^4 CFU/ml") for all three ticks (see the graph below). I have tried to find an answer using search but did not find the description of this problem. Could you please point out what is wrong with this code? Thanks in advance.

Yours truly,
Jakhongir

Jakhongir Alidjanov

unread,
Jul 9, 2020, 11:22:02 AM7/9/20
to ggplot2

Update
Dear all problem was in the line

facet_grid(~count, switch="y", space = "free_x", scales = "free_x")
But if I remove it, I lose the number of cases in each group, that I would like to avoid.
https://i.stack.imgur.com/UEw0I.png
Well, I have found the solution. I have removed the line with facet_grid and added some modifications to keep these numbers at the top of the graph. First, I tried to duplicate the x-axis using sec_axis to add the numbers of patients in each group on the secondary axis. But doesn't work because the x-axis was discrete and sec_axis works only with continuous axes. Then, I have tried simple geom_text, adding some modifications with coord_cartesian and plot.margin. So, the added modifications are:
+geom_text(aes(label=count, y=Inf), vjust=-0.5, size=5)+
coord_cartesian(clip = "off")+
theme(plot.margin = margin(20,15,15,15, unit = "pt"))
https://i.stack.imgur.com/1hXDh.png

вторник, 7 июля 2020 г., 17:17:14 UTC+2 пользователь Jakhongir Alidjanov написал:

Loveland, Adam

unread,
Jul 9, 2020, 12:55:45 PM7/9/20
to Jakhongir Alidjanov, ggplot2

Hi,

 

This will help you get an answer:

 

http://adv-r.had.co.nz/Reproducibility.html

 

From: ggp...@googlegroups.com [mailto:ggp...@googlegroups.com] On Behalf Of Jakhongir Alidjanov
Sent: Thursday, July 09, 2020 11:22 AM
To: ggplot2 <ggp...@googlegroups.com>
Subject: EXTERNAL: Re: ggplot2 with expression: conditional superscript the tick labels in scale_*_discrete

 

** This email has been received from outside the organization – Think before clicking on links, opening attachments, or responding. **

 

--
--
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/3d0d9de2-f2bf-4545-a8f5-44373182e96fo%40googlegroups.com.


The contents of this email are the property of PNC. If it was not addressed to you, you have no legal right to read it. If you think you received it in error, please notify the sender. Do not forward or copy without permission of the sender. This message may be considered a commercial electronic message under Canadian law or this message may contain an advertisement of a product or service and thus may constitute a commercial electronic mail message under US law. You may unsubscribe at any time from receiving commercial electronic messages from PNC at http://pages.e.pnc.com/globalunsub/
PNC, 249 Fifth Avenue, Pittsburgh, PA 15222; pnc.com

Reply all
Reply to author
Forward
0 new messages