我有一個包含 20 個點的資料集,分為 2 組(每組 10 個),其中在散點圖中它們的顏色相同,但形狀是封閉的和開放的。但是,我希望 2 條回歸線具有不同的顏色和線條粗細。我找到了很多
我認為這是解決方案,但出現錯誤:
錯誤check_aesthetics():!美學必須是長度 1 或與資料 (20) 相同: x 和 y 運行rlang::last_error()以查看錯誤發生的位置。
my.formula <- y ~ x
LM_plot <- ggplot(df, aes(x=Method1, y=Method2, color=Group, shape=Group))
geom_point(aes(size=Group), inherit.aes=T)
#geom_smooth(method="lm", se = F, fullrange = T)
geom_smooth(aes(x=df$Method1[1:10], y=df$Method2[1:10]), method="lm", se=T, inherit.aes=F, colour="blue", size=4, fullrange = T, formula = my.formula)
geom_smooth(aes(x=df$Method1[11:20], y=df$Method2[11:20]), method="lm", se=T, inherit.aes=F, colour="blue", size=2, fullrange = T, formula = my.formula)
scale_shape_manual(values = c(18, 5))
scale_color_manual(values = c("#66ccfe", "#66ccfe"))
scale_size_manual(values = c(5, 5))
scale_x_continuous("Method1")
scale_y_continuous("Method2")
stat_poly_eq(aes(label = paste0("atop(", ..eq.label.., ",", ..rr.label.., ")")),
formula = my.formula,
parse = TRUE, size = 8)
LM_plot <- LM_plot theme_prism(base_size = 20)
uj5u.com熱心網友回復:
您可以在 aes 之外指定顏色,或者,如果您想將不同的比例映射到相同的審美,請使用 ggnewscale(見下文)。
您可以對自己的尺寸審美做同樣的事情。
library(ggplot2)
df <- data.frame("Group" = c(1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2),
"Subject" = c(1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10),
"Method1" = c(2,5,6,8,9,10,15,20,30,40,3,5,6,8,11,14,18,90,22,23),
"Method2" = c(1,2,3,4,5,6,7,8,9,10,23,33,35,40,45,60,80,90,100,111))
my.formula <- y ~ x
ggplot(df, aes(x=Method1, y=Method2, shape=as.character(Group)))
## use color outside aes
geom_point(aes(size=as.character(Group)), color = "#66ccfe")
## add size outside of aes
geom_smooth(method="lm", se = F, fullrange = T, color = "darkblue", size = 2)
scale_shape_manual(values = c(18, 5))
scale_size_manual(values = c(5, 5))
#> `geom_smooth()` using formula 'y ~ x'

兩個秤具有相同的美感
ggplot(df, aes(x=Method1, y=Method2, shape=as.character(Group)))
# use color outside aes
geom_point(aes(size=as.character(Group), color = I("#66ccfe")))
scale_shape_manual(values = c(18, 5))
scale_size_manual(values = c(5, 5))
ggnewscale::new_scale_color()
ggnewscale::new_scale("size")
# now add color into aes
geom_smooth(aes(color = as.character(Group), size = as.character(Group)),
method="lm", se = F, fullrange = T)
scale_color_brewer(palette = "Set1")
scale_size_manual(values = c(1:2))
#> `geom_smooth()` using formula 'y ~ x'

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/483759.html
上一篇:如何為一組資料創建ggplot圖例,然后為其他水平線添加圖例?
下一篇:使用ggplot繪制二項式GLM
