How to fix ggplot2 geom_text labels for data points overlapping when exporting as .pdf

344 Views Asked by At

I'm working with a data set that I want to output in a geom_tile plot, see below:

plot114 <- ggplot(data = data1, 
                  aes(x = total, 
                      y = vrc)) +
   geom_step(step, 
             aes(x = x, 
                 y = y), 
             direction = "hv") +
   geom_tile(aes(fill = number)) +
   geom_text(aes(label = number), 
             family = "Arial") +
   theme(panel.background = element_blank(), 
         panel.border = element_rect(colour = "black", 
                                     fill = NA), 
         axis.line = element_line(colour = "black")) +
   ylab("Number of interest") + 
   xlab("Total number") +
   guides(fill = FALSE) +
   coord_fixed(ratio = 1) + 
   scale_fill_distiller(palette = "Reds", 
                        trans = "reverse") +
   scale_y_continuous(breaks = seq(0, 40, 1), 
                      expand = c(0, 0), 
                      limits = c(-0.5, 18.5)) +  
   scale_x_continuous(breaks = seq(0, 40, 1), 
                      expand = c(0, 0), 
                      limits = c(-0.5, 25.5))

The output graphic (also when you export it as image) looks as follows:

https://i.stack.imgur.com/duWXo.jpg

However, when I export it as .pdf (with ggsave for example), the labels shift to the bottom right and they also move over each other if they have more than one digit (any number greater than 9, e.g. 25, becomes a 2 and a 5 shifted over each other to make it unreadable, see dark red squares)

enter image description here

I don't want to use ggrepel and the normal export into a .png or other file type tells me this should be possible with a .pdf file as well.

Can anyone help me/does anyone know what can be done about this?

Thanks!

0

There are 0 best solutions below