gpt4 book ai didi

r - ggline 与 2 美学

转载 作者:行者123 更新时间:2023-12-04 08:50:37 28 4
gpt4 key购买 nike

我想使用具有 2 美学的包 ggpubr 的 ggline。等效项在 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

最佳答案

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

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

关于r - ggline 与 2 美学,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64119847/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com