nicholasehamilton / ggtern

Extension to ggplot2 for plotting ternary diagrams
www.ggtern.com
55 stars 14 forks source link

Issue with long labels #20

Closed gilleschapron closed 9 years ago

gilleschapron commented 9 years ago

Hi

First, I would like to thank you for your very useful work on this package.

I have a little issue with the labels I would like to print on my plot. With this code, everything is ok:

# Data
granu <- data.frame(T = 0.2,
                    L = 0.3,
                    R = 0.5)
# Legend
type.sol <- data.frame(T = c(0.6, 0.35, 0.33, 0.25, 0.075,0.125,
                             0.125, 0.075, 0.2),
                       L = c(0.15, 0.42, 0.15, 0.65, 0.8, 0.52,
                             0.33, 0.13, 0.1),
                       R = c(0.15, 0.23, 0.52, 0.1, 0.125, 0.355,
                             0.545, 0.795, 0.7),
                       txt = c('Argile', 'Argile\nlimoneuse',
                               'Argile sableuse', 'Limon\nargileux',
                               'Limon', 'Limon\nsableux',
                               'Sable\nlimoneux', 'Sable',
                               'Sable\nargileux'))

# Limites
lim.demolon <- data.frame(x = c(0.5, 0.4, 0.4, 0.4, 0.25, 0.15, 0,
                                0.25, 0.15, 0.15),
                          y = c(0.5, 0.3, 0.3, 0.6, 0.5, 0.85, 0.75,
                                0.2, 0.2, 0.2),
                          z = c(0, 0.3, 0.3, 0, 0.25, 0, 0.25,
                                0.55, 0.65, 0.65),
                          xend = c(0.4, 0.4, 0, 0.25, 0.25, 0.15, 0.25,
                                   0.15, 0.15, 0),
                          yend = c(0.3, 0, 0.5, 0.5, 0, 0.6, 0.5,
                                   0.2, 0, 0.35),
                          zend = c(0.3, 0.6, 0.5, 0.25, 0.75, 0.25, 0.25,
                                   0.65, 0.85, 0.65))
#Build
plot <- ggtern(data = granu, aes(y = T, x = L, z = R)) +
        geom_segment(data = lim.demolon,
                     aes(x = y, y = x, z = z,
                         xend = yend, yend = xend, zend = zend),
                     size = 0.5) +
        geom_text(data = type.sol, aes(x = L, y = T, z = R, label = txt),
                  size = 4) +
        geom_point(color = 'red') +
        labs(title = "Classification de Demolon",
             T = "Argile",
             L = "Limon",
             R = "Sable") +
        theme_showarrows() +
        theme_arrowlong() +
        theme_hidetitles()
plot

But when I want to print:

plot <- ggtern(data = granu, aes(y = T, x = L, z = R)) +
        geom_segment(data = lim.demolon,
                     aes(x = y, y = x, z = z,
                         xend = yend, yend = xend, zend = zend),
                     size = 0.5) +
        geom_text(data = type.sol, aes(x = L, y = T, z = R, label = txt),
                  size = 4) +
        geom_point(color = 'red') +
        labs(title = "Classification de Demolon",
             T = "Argile(< 2µm)",
             L = "Limon (2 à 20µm)",
             R = "Sable (> 20 µm)") +
        theme_showarrows() +
        theme_arrowlong() +
        theme_hidetitles()
plot

Nothing is printed...

Do I make something wrong ?

Regards Gilles

nicholasehamilton commented 9 years ago

Gilles,

Thanks very much for bringing this to my attention, I am not sure what is causing the labels to get suppressed, but it appears the presence of 'Numbers' in the text is causing it somehow -- if you remove the numbers (2 and 20) then they print ok.

As a workaround, you can use 'expressions' in the label fields, like this:

plot <- ggtern(data = granu, aes(y = T, x = L, z = R)) +
  geom_segment(data = lim.demolon,
               aes(x = y, y = x, z = z,
                   xend = yend, yend = xend, zend = zend),
               size = 0.5) +
  geom_text(data = type.sol, aes(x = L, y = T, z = R, label = txt),
            size = 4) +
  geom_point(color = 'red') +
  labs(title = "Classification de Demolon",
       T = expression("Argile (< "*2*"µm)"),
       L = expression("Limon ("*2*" à "*20*"µm)"),
       R = expression("Sable (> "*20*"µm)")) +
  theme_showarrows() +
  theme_arrowlong() +
  theme_hidetitles()
plot

I know it is a little more tedious, but it should get you out of the mud.

Cheers,

NH

gilleschapron commented 9 years ago

Thanks for your quick answer, I have not thinking about using "expression" but the result is just perfect.