5

I would like to have Unicode characters appear in my plot, as a part of a mixed string, for example ρ=0.84 where the 0.84 is saved in a variable, let's say cor.value.

This practically excludes solutions such as expression or bquote (suggested here). I don't know if they would work alone, but trying to combine them with a variable's value via paste is a problem. The only way I could got it to work was by actually using the unicode symbol in my source. It even showed up on the plot in R Studio.

But when I tried to print, I only got .. instead. The only suggestion I have found so far is to use Cairo, but Cairo is mangling the remainder of my plot, so I want to stick with the original pdf device.

Is there any chance, by using Unicode or something else, to get that string printed within the plot, but not having to use Cairo?


Here is an example of what Cairo does (this is supposed to be a corner of the graph, with a grey border):

screenshot from Cairo

The partial dot is cut off, not clipped. So when it is overlayed with the border, the border is slightly offset, and the dots "show through" the border (here you only see it on the bottom, elsewhere it is more visible). And the bottom and right border are thicker than the left and top border. The very pale grey band behind the dot is broader in Cairo. These problems are not present in the standard PDF device.

Screenshot from standard

I am currently working on a Fedora 21 system, but this document has to also compile on Windows 8 or 10, and preferably also on other Linux flavors.


Here is the code for the plot (intended to be viewed at smallish sizes, around 2x1.5 inch). The current plan is to have the text in the c.a variable and place it where an axis label would be, but I want to keep my options open to make it an annotation later:

 offset.plotdata <- data.frame(understandability=c(2.95, 0.85, 0.75, 1.15, 2.25, 2.05, 1.95, 2.15, 2.25, 0.75, 2.05, 0.85), lowercase.pseudonym=c("A", "B", "C", "D", "E", "F", "A", "D", "E", "C", "F", "B"), questionnaire=c(rep("pre", 6), rep("post", 6)))
offset.plotdata$decorative.points <- c(1,2,3,4,5,NA,1,2,3,4,5,NA)
middle.blue <- "#2186D9"
variable.name <- "understandability"
cor.value <- 0.84
c.a <- "rho = 0.84"
ggplot(offset.plotdata, 
       aes_string(x="questionnaire", y=variable.name, 
                  group="lowercase.pseudonym"))+
  geom_line(colour=middle.blue)+
  ylim(c(5,0.5))+
  theme_bw()+
  theme(panel.grid.major.y = element_line( size=5, color="#f8f8f8"),
        panel.grid.minor.y=element_blank(),
        panel.grid.major.x=element_blank(),
        panel.grid.minor.x=element_blank(),
        text = element_text(size=9), 
        axis.text=element_text(size=8),
        axis.ticks.y=element_blank(),
        plot.margin = unit(c(0,5,0,0), "mm")
        #axis.title.y=element_text(margin=margin(0,20,0,0))
  )+
  labs(y="Angekreuzte Option", x=as.character(c.a))+
  scale_x_discrete(expand = c(0,0), label=c("Vorher", "Nachher"))+
  #ggtitle("gepaarte Antworten")+
  geom_point(data=offset.plotdata, 
             aes(x=questionnaire,y=decorative.points), 
             fill=middle.blue, 
             colour=middle.blue, 
             size=5)
Community
  • 1
  • 1
rumtscho
  • 2,474
  • 5
  • 28
  • 42
  • don't know, but can you add a bit more on how Cairo is mangling the remainder of your plot? Perhaps that part of the problem is fixable. It would probably also be helpful to add information about your platform (OS and version), as some of these sorts of issues are platform-specific. – Ben Bolker Jan 15 '17 at 04:01
  • @BenBolker updated. – rumtscho Jan 15 '17 at 04:15
  • and why does wanting to have a mixed string containing information stored in a variable rule out using `expression()`/plotmath? e.g `cor.value <- 0.84; substitute(expression(rho=x),list(x=cor.value))` – Ben Bolker Jan 15 '17 at 04:17
  • a fully reproducible example would help a lot ... – Ben Bolker Jan 15 '17 at 04:18
  • @BenBolker I didn't know about substitute. Here is the plot in its current state, with the necessary data to run. – rumtscho Jan 15 '17 at 04:35

1 Answers1

2

I don't know why expression(...) doesn't work (i.e. it does print the Greek letters but it still prints "expression" as well) and the shortcut of specifying an expression with a tilde (~) does work, but this works for me:

g0 <- [... the rest of the graph ...]

cor.value <- 0.84
c.a <- substitute(~rho == x, list(x=cor.value))
g0 + labs(y="Angekreuzte Option", x=c.a)
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • For me, that prints `expression(ρ = 0.84)` into the plot. The console also gives that back when I simply call `c.a.` I tried coaxing this sting out of it and using `substring` on it, but neiter `cat` nor `as.character` give that representation. – rumtscho Jan 15 '17 at 13:07
  • Yes, copy-pasted from here. If I use `as.character`, nothing at all gets printed on the plot. – rumtscho Jan 15 '17 at 14:33
  • 2.2.0 here, it was what came through packages.install (on R 3.2.2) – rumtscho Jan 15 '17 at 15:47
  • @BenBolker's suggestion of `substitute(~rho == x, list(x=cor.value))` works for me even with older ggplot2 version 2.0.0, so I doubt it's a difference between 2.2.0 and 2.2.1 at play here. – drammock Jan 16 '17 at 00:56