Home > Enterprise >  How to add a custom legend to multiple geom_function()?
How to add a custom legend to multiple geom_function()?

Time:12-09

library(tidyverse)
ggplot(data = data.frame(x = c(0, 1)), aes(x))  
        geom_function(fun = dnorm, n = 10001,
                      args = list(mean = .5, sd = .125),
                      show.legend = T)  
        geom_function(fun = dbeta, n = 10001,
                      args = list(shape1 = 10, shape2 = 8),
                      linetype = 5, show.legend = T)  
        geom_function(fun = dbeta, n = 10001,
                      args = list(shape1 = 15, shape2 = 8),
                      linetype = 2, show.legend = T)  
        geom_function(fun = dbeta, n = 10001,
                      args = list(shape1 = 20, shape2 = 8),
                      linetype = 3, show.legend = T)  
        ylab("f(θ)")  
        xlab("θ")  
        scale_linetype_manual(
                values = c("a" = 1,
                           "b" = 5,
                           "c" = 2,
                           "d" = 3)
                ) 
        theme_test(base_size = 20)

If you plot this, the legend will not take the correct linetypes. It will always show the linetype "1" or "solid".

How do I show different correct linetypes in scale_linetype_manual()?

CodePudding user response:

If you want to have a legend you have to map on aesthetics, i.e. instead of setting the linetype as parameter set it inside aes() and use the labels you used in scale_linetype_manual:

Note: Doing we could get rid of show.legend=T as ggplot will automatically add a legend.

library(ggplot2)

ggplot(data = data.frame(x = c(0, 1)), aes(x))  
  geom_function(aes(linetype = "a"), fun = dnorm, n = 10001,
                args = list(mean = .5, sd = .125))  
  geom_function(aes(linetype = "b"), fun = dbeta, n = 10001,
                args = list(shape1 = 10, shape2 = 8))  
  geom_function(aes(linetype = "c"), fun = dbeta, n = 10001,
                args = list(shape1 = 15, shape2 = 8))  
  geom_function(aes(linetype = "d"), fun = dbeta, n = 10001,
                args = list(shape1 = 20, shape2 = 8))  
  ylab("f(θ)")  
  xlab("θ")  
  scale_linetype_manual(
    values = c("a" = 1,
               "b" = 5,
               "c" = 2,
               "d" = 3)
  ) 
  theme_test(base_size = 20)

EDIT Instead of mapping on the linetype aesthetic another option would be to set your desired linetype via the override.aes argument of guide_legend. This could also be used to set different colors or ... . But be aware that doing so you have to set the linetypes in the order the categories appear in the legend:

Note: The assignment of linetype via the scale does not work. For this we have to map on aesthetics.

library(ggplot2)

ggplot(data = data.frame(x = c(0, 1)), aes(x))  
  geom_function(fun = dnorm, n = 10001,
                args = list(mean = .5, sd = .125),
                show.legend = T, linetype = 3)  
  geom_function(fun = dbeta, n = 10001,
                args = list(shape1 = 10, shape2 = 8),
                linetype = 5)  
  geom_function(fun = dbeta, n = 10001,
                args = list(shape1 = 15, shape2 = 8),
                linetype = 2)  
  geom_function(fun = dbeta, n = 10001,
                args = list(shape1 = 20, shape2 = 8),
                linetype = 3)  
  ylab("f(θ)")  
  xlab("θ")  
  scale_linetype_manual(
    values = c("a" = 1,
               "b" = 2,
               "c" = 2,
               "d" = 3)
  ) 
  theme_test(base_size = 20)  
  guides(linetype = guide_legend(override.aes = list(linetype = c(1, 5, 2, 3), color = c(1, 5, 2, 3))))

  • Related