【问题标题】:ggline with 2 aesthetics具有2美学的ggline
【发布时间】:2021-01-15 01:53:48
【问题描述】:

我想使用 ggpubr 包的 ggline 与 2 美学。等效项在 geom_line 中完全有效,但在 ggline 中无效。假设我有这个数据集

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'))

现在我可以使用 geom_line 来获取所有的情节。

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

不使用同样的东西 ggline

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

产生这个错误

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

【问题讨论】:

    标签: r ggplot2 ggpubr


    【解决方案1】:

    在我看来,ggpubr 不欣赏linetypecolor 的两种不同美学。它将使用单变量解决方案运行。

    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")
    

    【讨论】:

      猜你喜欢
      • 2022-12-07
      • 2019-06-15
      • 2020-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-31
      相关资源
      最近更新 更多