1

In the following example (please notice differences in y-axis labels) I use a variable to fill in an axis label in ggplot2. Interestingly ~ produces much larger spaces, and extra spaces show up around an enlarged -.

enter image description here

library(ggplot2)

#LabelY <- "Miles per Gallon-Car"
LabelY <- parse(text="Miles~per~Gallon-Car")

a <- ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point() +
        ggtitle("Fuel Efficiency of 32 Cars") +
        xlab(LabelY) + ylab(LabelY) +
        theme(text=element_text(size=16))
print(a)

I am using parse because it allows me to use more complex examples including atop and greek letters.

Is there a way I can make use of parse to import complex strings while also preserving the desired "less spread out" appearance of the content?

EngBIRD
  • 1,915
  • 3
  • 18
  • 22
  • What would it take to convince you against Comic Sans font? – Roman Luštrik Dec 30 '15 at 19:13
  • What sort of "complex strings" do you want to "import"? – Spacedman Dec 30 '15 at 19:15
  • @RomanLuštrik I am not actually using it, this was just the most applicable MWE I could readily modify. My last question was trying to get fonts working, and I needed something that was visibly noticeable demonstrating a successful change. – EngBIRD Dec 30 '15 at 19:16
  • @Spacedman Overall they are simple but like I mentioned, they contain `atop` and greek characters e.g. `atop(Release of TNF-{alpha},(Treatment/Control))`. Other examples may contain sub and superscripts, but with parse it's not actually the assembly of the string I am having problems with so I get the example simpler. By import I mean that this string is saved in a database and retrieved, stored, and parsed in variable format. – EngBIRD Dec 30 '15 at 19:18
  • So is all the extrafont stuff relevant to your problem? Or does it show with the default fonts? And could you redo the example to put the text on the X axis because then I won't crick my neck so much (and the spacing problem might be more obvious). – Spacedman Dec 30 '15 at 20:24
  • @Spacedman Problem persists with all fonts I have tested. I reduced the MWE, and duplicated the axis labels on both X and Y, and the problem is common so it indicates its not an artifact of the vertical rotation (A problem I see all the time in MS text boxes). – EngBIRD Dec 30 '15 at 20:27
  • So you want to replace spaces in your strings with ~ so the spacing is tighter? – Spacedman Dec 30 '15 at 20:30
  • @Spacedman Priority 1) I need to have a shorter hyphen 2) I want to decrease the spaces around the hyphens (for consistency I want all spaces the same size, so ultimately that means I may need to control any mechanism that inserts spaces) – EngBIRD Dec 30 '15 at 20:34
  • See if ``parse(text="Miles~per~`Gallon-Car`")`` does what you want. It looks like it keeps a hyphen instead of a dash. – aosmith Dec 30 '15 at 21:14
  • @aosmith Thanks for the idea, but I get the following error (using single and double quotes): `:1:20: unexpected string constant` Probably because the string is not evaluated with `"` (as it is in any hardcoded string) in it's definition (database field feeding the content). When you hardocde `"` surrounding the text in the database it doesn't work with `parse` – EngBIRD Dec 30 '15 at 22:02
  • Those are back ticks, not single quotes. – aosmith Dec 30 '15 at 22:10
  • @aosmith Thanks, back ticks, awesome. Just for note as well, if I surround the hyphen encapsulated in single quotes in curly braces it also seemed to work as a suitable escape mechanism. – EngBIRD Dec 30 '15 at 22:12
  • Great, sounds like you can go ahead and answer your own question, then. – aosmith Dec 30 '15 at 23:15
  • That's a relief, I was afraid you are using it for publication/presentation. :) – Roman Luštrik Dec 31 '15 at 08:58

1 Answers1

3

It looks like enclosing the hyphenated term with backticks will allow you to keep the hyphen instead of turning it in a dash.

Here I put the new hyphenated version of the axis label on the x axis and leave the y axis as the original for comparison.

LabelY <- parse(text="Miles~per~Gallon-Car")
LabelY2 <- parse(text="Miles~per~`Gallon-Car`")

ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point() +
    ggtitle("Fuel Efficiency of 32 Cars") +
    xlab(parse(text = LabelY2)) + ylab(LabelY) +
    theme(text=element_text(size=16))

enter image description here

As you pointed out in the comments, you can also use a curly bracket and single quote combination around the hyphenated term to get the same effect.

parse(text="Miles~per~{'Gallon-Car'}")

Community
  • 1
  • 1
aosmith
  • 34,856
  • 9
  • 84
  • 118
  • Thanks! Just for posterity though, I actually got the same effect from `parse(text="Miles~per~Gallon{'-'}Car")`, no real difference though. Thanks again! – EngBIRD Dec 30 '15 at 23:46
  • @EngBIRD Interesting. I get a parse error if I try that so thought I must have misunderstood your previous comment. I am using R 3.2.3, but don't know if that's the difference. – aosmith Dec 30 '15 at 23:56
  • Sorry I think I had a typo when I copied and pasted your last line there. It should have been pairs of braces on the quotes, not only 1 open close set as it's the quotes that my externally pulled value struggled to parse. Don't remember for sure, because I went with your simpler more condensed approach with the ticks. – EngBIRD Dec 31 '15 at 00:04