Questions tagged [plotmath]

plotmath provides mathematical expression plotting in the R language.

Filter by
Sorted by
Tagged with
0 votes
1 answer
32 views

Parse an annotation across multiple lines in ggplot

Closest I've come is here: Annotate ggplot plot with a multiline expression with objects? but I cannot seem to get the solution working. I cannot use the ggpmisc package because I am forcing the ...
  • 672
0 votes
1 answer
38 views

How do I use plotmath to create subscript/superscript in a string from a list?

I am creating multiple plots with parameter names in the ggtitle. Some of these have subscripts, superscripts or other plotmath characters. My parameters are stored in a param-list and are presented ...
0 votes
1 answer
23 views

atop with multiple linebreaks and subscripts in geom_label

I have a dataframe for which I am plotting data using ggplot. My objective is to add an annotation using geom_label, with words that contain subscripts and linebreaks. In particular two linebreaks. ...
3 votes
1 answer
92 views

bgroup does not render properly on ggplot

The results of bgroup from plotmath do not seem to render properly as a ggplot2 annotation. For example ... library(ggplot2) ggplot(mtcars, aes(wt, mpg)) + annotate("text", x=2.5, y=25, ...
  • 337
1 vote
0 answers
50 views

Is it possible to use ggtext to plot symbols on top of each other?

I try to use geom_richtext of the "ggtext" package to create my labels and I want to plot a number and a letter on top of each other. But I don't know whether this is even possible with &...
  • 239
1 vote
1 answer
96 views

Line break in expression of table header?

I have seen a number of similar questions relating to line breaks in expressions, but I'm still not able to get a table with an expression and line break in the header. I would like it to say Model R2 ...
  • 55
1 vote
1 answer
97 views

Using plotmath with geom_label_repel to have subscripts within the labels

I am trying to have subscripts in my geom_label. e.g. Maine Apo (km/h) = 9 Qt (m/s) = 90 I am aware of using [x] to get subscripts but I am not sure how to achieve that when I want to get the ...
  • 24.2k
1 vote
2 answers
89 views

Is it possible to use == twice in R mathematical notation?

I am trying to use R mathematical notation of plotmath in a plot label. In the label, I am trying to use to equality signs, e.g. ggplot(data=NULL) + geom_point(aes(x=0.5, y=0)) + geom_text(aes(x=0....
  • 303
0 votes
1 answer
547 views

Why is ggplot annotation created with expression and bquote in bold font?

Using ggplot2 I need a multi-line annotation that incorporates variable values, commas, a Greek letter, and a superscript. I played with a lot of different ideas I found in this forum for doing this ...
  • 189
0 votes
1 answer
58 views

How to put plotmath labels in ggplot facets

We often want individual regression equations in ggplot facets. The best way to do this is build the labels in a dataframe and then add them manually. But what if the labels contain plotmath, e.g., ...
0 votes
1 answer
110 views

Reduce spacing of frac in R expression for ggplot legend

I am trying to reduce the space between the numerator, denominator, and fraction line in R expression. Here is my MWE generating a very ugly legend text for the fraction: tibble( t = seq(0,60,.1), ...
  • 1,767
7 votes
2 answers
801 views

Italic letters in generated axis tick labels

I have an issue creating axis tick labels that combine italic letters and input of variables. Simply said, I want to call variables and insert text such as n = 1 below each label. Here's an example ...
  • 157
4 votes
2 answers
143 views

How to write much less than symbol (<<) in plotmath in R?

I'm trying to write the << symbol in R but I'm not succeeding. Any tips? plot(expression(alpha<1)) plot(expression(alpha<<1)) Error: unexpected input in "plot(expression(alpha<...
0 votes
1 answer
142 views

Using paste() and formatC() together in plot label in R

I want to use a subscript and a number in a geom_text() layer of a ggplot2 plot. The number will come from an indexed dataframe so that I can use it in a flexible way within a custom function, but ...
1 vote
1 answer
115 views

How can I strip a leading zero from an expression rendered by plotmath?

Due to a journal's requirements, I need to format p values as capital, italic P and the values themselves without leading zero, for example, P = .01. I would like to include some p value labels in ...
  • 3,681
1 vote
1 answer
44 views

Poorly typeset μ symbol in plotmath

When using the default font (sans) in base R, symbols like μ are poorly typeset: plot(1, xlab = bquote("I want sans font here, but serif font for:" ~ (mu * g))) The g looks larger than the ...
1 vote
1 answer
806 views

How to format p-values in ggplot2 `annotate()`—APA style?

I would like to add a p-value to a scatter-plot, while respecting APA style. This entails two elements: (a) an italicized p, and (b) stripping the leading zero (but also: formatting values smaller ...
  • 699
1 vote
1 answer
285 views

Functions to format text for base R plotting

Specifying text in a base R plot() with formatting such as italics / bold font / newline usually involves one or more of the following functions: paste() expression() atop() substitute() italic() Is ...
0 votes
2 answers
145 views

R plotmath expression to show range of values in ggplot

I am struggling to get expression to communicate something like 10<=VarName<120. Why does the following code fail? starwars %>% filter(between(birth_year, 10, 120)) %>% ggplot(aes(x=...
0 votes
1 answer
198 views

Two-line annotations using plotmath expressions in ggplot

I want to italicise a single character in the second line of a annotation in ggplot Here's the plot iris %>% ggplot(aes(x = Sepal.Length, y = Species, fill = Species)) + ...
  • 2,837
0 votes
1 answer
2k views

How to add a title with greek letters to a Plotly graph with R?

library(plotly) f <- plot_ly(alpha = 0.5) %>% add_histogram(x=rnorm(1000), name="Observed", marker=list(color="...
  • 1,222
0 votes
1 answer
616 views

How do I bold and underline part of a ggplot2 annotation?

I am trying to use the bold() and underline() functions from grDevices within paste() to create an annotation that features a stylized, hardcoded 'title' with a line break followed by a string that ...
1 vote
1 answer
348 views

ggplot2 axis text formatting won't work with exponents

It is easy enough to change the axis text of a ggplot to some sort of expression (in this case 10^.x). However, when the text is transformed, it loses bold formatting. I was wondering if there was a ...
  • 53
0 votes
2 answers
381 views

boldface of labels containing an expression with lower or equal symbol

I need to render in boldface the labels of the legend of a graph. One of the labels is an expression containing a "lower or equal" sign. This is where I started from: library(ggplot2) df &...
  • 1,165
3 votes
1 answer
264 views

How to include the results of a statistical test as a plotmath expression in ggplot2 facet

I'm looking to include the results of multiple statistical tests in a faceted ggplot chart. I've found lots of excellent examples (like this) on how to include something similar in a title or ...
9 votes
3 answers
2k views

How to fix degree symbol not showing correctly in R on Linux/Fedora 31

Any map I make with: ggplot() + geom_sf() produces the expected map, but does not show the degree sign correctly, as appears from the following picture. The answer given in this answer on SO - ...
  • 2,843
2 votes
1 answer
845 views

Inserting math symbols in legends of R plots

I am using survminer to produce a survival plot with 2 survival curves, one for patients who had received 4 Packed Red Blood Cells (PRBC) units during surgery and one for patients who received more ...
0 votes
0 answers
524 views

How to use italic in label_bquote for strip text lables

There have been many proposed solutions to many slightly different problems, but none quite catches my specific problem. What I want is the following: I want to mix normal subfig labels (a, b, c, ...) ...
  • 318
2 votes
2 answers
93 views

How to place an expression on top of another expression using plotmath

If I want to generate a multi-line expression I can do: over = 'OVER' below = 'BELOW' x_lab_title = bquote(atop(.(over), .(below))) ggplot(data.frame()) + xlab(x_lab_title) However, I would like ...
  • 12.6k
2 votes
4 answers
261 views

using `plotmath` to display combination of subscript and `[ ]`

I want to create a plot where I want to display a mean value and confidence intervals for this mean value. To do so, I am using plotmath. Here is something I have done that works- library(ggplot2) ...
7 votes
3 answers
2k views

degree symbol incorrect in map axis labels

If I create maps using geom_sf, the axis labels have the wrong symbol for degrees. I get degree symbols that are vertically centred in the text, rather than raised like superscipts. For example, ...
  • 29.6k
4 votes
1 answer
2k views

Make row labels italic in heatmap.2

I'm trying to make my row labels italic using the R function heatmap.2. There's no default option and I can't figure out a work around by setting par(font=3) for example. How can I set my row labels ...
  • 123
1 vote
0 answers
206 views

bquote, parsing, expression to get multiple lines labels in ggplot with greek letters and variables as subscripts

Let's say I have paste0("Year = ",index,"\nN = ",length((dfGBD %>% filter(year==index))[[vbl]]), " Bandwidth = ",round(stats::bw.nrd(log((dfGBD %>% filter(year ...
  • 2,928
4 votes
1 answer
752 views

Fully Left-Justify When Using atop()

How can I fully left-justify text when using atop()? library(tidyverse) mtcars %>% mutate(cyl = str_c("atop(bold(", cyl, "~long~text~on~the~top),(shorttext))")) %>% ggplot(aes(hp, wt)) + ...
  • 1,486
2 votes
0 answers
194 views

Irregular bolding in plotmath expression in ggplot2

I want to be able to pass an argument from a function into a label on a ggplot graph and for that label to be in bold. Unicode won't work because many unicode symbols don't render to pdf. Plotmath ...
  • 2,837
1 vote
1 answer
811 views

Use dataframe variable names in plot titles

I have a dataframe with several variables that I wish to label and then use in several ggplots. I have applied labels using the labeller package with the following code. library(tidyverse) library(...
  • 79
1 vote
2 answers
283 views

including conditional elements in plotmath expression for ggplot2 subtitle

I am trying to write a custom function where I want to display an effect size estimate and its confidence intervals in ggplot2 plot subtitle. I am using plotmath to properly display Greek letters and ...
4 votes
1 answer
272 views

How to concatenate two expressions?

How can two expressions (expr and alpha~"is in ") be combined so that they are shown as one expression (in a plot label or via mtext() or so)? expr <- expression(bgroup("(",frac(1,3)*", "*frac(1,2)...
3 votes
1 answer
199 views

removing space between ggrepel label made using plotmath

I am creating a plot where I want to display labels using ggrepel. I am showing a minimal example below that illustrates how the label has two components separated by a comma - first related to the ...
5 votes
2 answers
183 views

preventing centering multilayered caption in ggplot2

This is part-2 to my previous question (getting constant text size while using atop function in r). Now the issue relates to how I can prevent plotmath from centering the text to avoid the extra ...
5 votes
1 answer
544 views

getting constant text size while using atop function in r

Below is a much simpler example of a complicated custom function I have written. In the full-length form of this function, "layer1"corresponds to caption entered by the user, "layer2" corresponds ...
0 votes
1 answer
46 views

Use plotmath to write superscript inside exponentiated brace, e.g., a Poisson-like multiplicative equation

How might I use plotmath capabilities to add a multiplicative Poisson-like equation to my plot? Or, given that I'm really close, how can I write this equation without the . in the middle of the ...
  • 101
4 votes
1 answer
226 views

Use ampersand in ggplot facet strip label

How can I use an ampersand in a facet strip label? I have tried as is, and using unicode with the same result. MWE library(ggplot2) mtcars$am2 <- factor(mtcars$am, labels=c("N[one]==1 \U0026 N[...
  • 2,403
2 votes
2 answers
713 views

Italics within plotmath expression cannot be rendered bold in ggplot

Based on previous posts I worked out how to use expression() to get a string within a string italicised while the rest of the string remains un-italicised. The problem is that element_text(face = "...
  • 2,837
4 votes
2 answers
9k views

Error when exporting ggplot graphs to pdf: invalid font type

I'm having trouble getting my plots to save using ggsave(). I keep getting this error: Error in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, : invalid font type I don't understand ...
  • 631
4 votes
2 answers
2k views

How to use bquote in combination with ggplot2 geom_label?

I've read the following article: https://trinkerrstuff.wordpress.com/2018/03/15/2246/ Now I'm trying to use the suggested approach with bquote in my plot. However I can't get it to work. I have the ...
  • 3,071
12 votes
2 answers
5k views

properly formatting a two-line caption in ggplot2

I am working on a custom function that can add two-line captions to plots and I want the caption to be formatted properly no matter what the user might choose to enter ("character" or "expression"). I ...
0 votes
0 answers
84 views

R: "plotmath" produces extra white space when using Computer Modern fonts

More specifically, the "juxtapose" feature [i.e. "*" or "paste"] produces extra white space when using the "CM Roman" font family. With other families [e.g. "CenturySch"] it works fine. An issue of R [...
1 vote
1 answer
116 views

In R, how can a string function argument be expanded to mathematical symbols using plotmath?

Here is an example: PMEx <- function(t) { y <- (1:10)^2 plot(1:10,y,type="l") text(2,90,label="This is NOT what I want -->",adj=0) text(2,80,label="This IS what I want -->",...
  • 11
1 vote
1 answer
129 views

creating a new column containing plotmath expression using `dplyr::case_when`

I would like to create a new column containing plotmath expressions that I later plan to use somewhere else in the analysis pipeline. Here is a minimal example along with I tried. For example, here I ...