ggline with 2 aesthetics

284 views Asked by At

I want to use the ggline of the package ggpubr with 2 aesthetics. The quivalent perfectly works in geom_line but not in ggline. Let's say I have this dataset

data <- data.frame(x = seq(0,1,length.out = 100)) %>% 
  mutate(a = x^2, b = x^3, c = (x+1)^-1, d = (x + 1)^-2) %>% 
  pivot_longer(cols = c(a,b,c,d), names_to = 'var',values_to = 'val') %>% 
  mutate(type = ifelse(var %in% c('a','b'), 'poly','inv'), 
         order = ifelse(var %in% c('a','c'), 'low','high'))

Now I can use geom_line to get the plot of all.

data %>% ggplot() + geom_line(aes(x = x, y = val, linetype = type, color = order)

enter image description here

No using the same thing ggline

data %>% ggline(x = "x", y = "val", linetype = "type", color = "order")

produces this error

Error: Aesthetics must be either length 1 or the same as the data (400): group
In addition: Warning message:
In if (is_parsable_aes(x)) { :
  the condition has length > 1 and only the first element will be used
1

There are 1 answers

0
Magnus Nordmo On

It seems to me that ggpubr doesn't appreciate two different aesthetics on linetype and color. It will run with a single variable solution.

library(tidyverse)
library(ggpubr)

data <- data.frame(x = seq(0,1,length.out = 100)) %>% 
  mutate(a = x^2, b = x^3, c = (x+1)^-1, d = (x + 1)^-2) %>% 
  pivot_longer(cols = c(a,b,c,d), names_to = 'var',values_to = 'val') %>% 
  mutate(type = ifelse(var %in% c('a','b'), 'poly','inv'), 
         order = ifelse(var %in% c('a','c'), 'low','high'))

data


data %>% ggplot() + geom_line(aes(x = x, y = val, linetype = type, color = order))

data <- data %>% mutate(new = paste(type,order))

data %>% ggline(x = "x", y = "val",color = "new",linetype = "new")