Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot unicode characters without Cairo?

Tags:

r

unicode

ggplot2

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)
like image 616
rumtscho Avatar asked Oct 29 '22 13:10

rumtscho


1 Answers

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)
like image 156
Ben Bolker Avatar answered Nov 15 '22 05:11

Ben Bolker