簡體   English   中英

ggline 與 2 美學

[英]ggline with 2 aesthetics

我想使用具有 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)

在此處輸入圖片說明

不使用相同的東西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並不領情在兩個不同的美學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")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM