Question

I don't know if you have seen some unwanted bold-face font like picture below:

enter image description here

As you see the third line is bold-faced, while the others are not. This happens to me when I try to use ggplot() with lapply() or specially mclapply(), to make the same chart template based on different data, and put all the results as different charts in a single PDF file.

One solution is to avoid using lapply(x, f) when f() is a function that returns a ggplot() plot, but I have to do so for combining charts (i.e. as input for grid.arrange()) in some situation.

Sorry not able to provide you reproducible example, I tried really hard but was not successful because the size of code and data is too big with several nested functions and when I reduced complexity to make a reproducible example, the problem did not happen.

I asked the question because I guessed maybe someone has faced the same experience and know how to solve it.

Was it helpful?

Solution

My intuition is that it's not actually being printed in bold, but rather double-printed for some reason, which then looks bold. This would explain why it doesn't come up with a simpler example. Especially given your mention of nested functions and probably other complicated structures where it's easy to get an off-by-one or similar error, I would try doing something where you can see exactly what's being plotted -- perhaps by examining the length() of the return value from apply().

Changing the order of elements of the vector, so that the order of the elements in the key is different, may also help. If you consistently get the bold-face on the last element, that also tells you a little bit more about where something is going wrong.

As @Dinre also mentioned, it could also be related to your plotting device. You can try out changing your plotting device. I have my doubts about this though, seeing as it's not a consistent problem. You could also try changing the position of the key, which depending on your plotting device and settings, may move you in or out of a compression block, thus changing which artifacts crop up.

OTHER TIPS

Reproducible example and a solution may be as follows:

library(ggplot2)

d <- data.frame(x=1:10, y=1:10)
ggplot(data = d, aes(x=x, y=y)) +
    geom_point() +
    geom_text(aes(3,7,label = 'some text 10 times')) +
    geom_text(data = data.frame(x=1,y=1), 
              aes(7,3, label = 'some text one time'))

When we try to add a label by geom_text() manually inserting x and y do not shorten the data. Then same label happen to be printed as many times as the number of rows our data has. Data length may be forced to 1 by replacing data within geom_text().

plot

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top