微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ggline与2美学

如何解决ggline与2美学

我想将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)

enter image description here

不使用同一行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

解决方法

在我看来,ggpubrlinetypecolor并没有两种不同的审美观。它将使用单个变量解决方案运行。

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。