我想手動控制線圖中誤差線的顏色:
col_1<-c(1:4, 1:4)
col_2<-c(25, 30, 28, 28, 35, 36, 39, 40)
er_bar<-c(3, 3, 2, 2, 2,4,4,3)
condition<-c("A","A","A","A", "B","B","B","B")
example<-data.frame(col_1, col_2, er_bar, condition)
example
ggplot(example, aes(col_1, col_2, color=condition)) geom_line(size=0.1)
geom_errorbar(aes(ymin=col_2-er_bar, ymax=col_2 er_bar), width=.1, color="blue") scale_color_manual(values=c("#FF0000", "254117"))
給我一個帶有所有錯誤條的圖,顏色為藍色,如指定。如何為每條線選擇單獨的顏色?
uj5u.com熱心網友回復:
為了將錯誤欄的顏色映射到資料,您只需指定該幾何函式color的引數即可aes。
library(ggplot2)
col_1<-c(1:4, 1:4)
col_2<-c(25, 30, 28, 28, 35, 36, 39, 40)
er_bar<-c(3, 3, 2, 2, 2,4,4,3)
condition<-c("A","A","A","A", "B","B","B","B")
example<-data.frame(col_1, col_2, er_bar, condition)
example
#> col_1 col_2 er_bar condition
#> 1 1 25 3 A
#> 2 2 30 3 A
#> 3 3 28 2 A
#> 4 4 28 2 A
#> 5 1 35 2 B
#> 6 2 36 4 B
#> 7 3 39 4 B
#> 8 4 40 3 B
ggplot(example, aes(col_1, col_2, color=condition))
geom_line(size=0.1)
geom_errorbar(aes(ymin = col_2-er_bar, ymax = col_2 er_bar, color=condition), width=.1)
scale_color_manual(values=c("#FF0000", "254117"))

由reprex 包(v2.0.1)于 2021 年 11 月 8 日創建
uj5u.com熱心網友回復:
這是一個選擇,但我歡迎一種更清潔的方法:
example2<-example %>%
mutate(testcol = case_when(
endsWith(condition, "A") ~ "#F88158",
endsWith(condition, "B") ~ "#0041C2"
))
errorbar_colors<-example2$testcol
ggplot(example, aes(col_1, col_2, color=condition)) geom_line(size=0.1)
geom_errorbar(aes(ymin=col_2-er_bar, ymax=col_2 er_bar), width=.1, color=errorbar_colors)
scale_color_manual(values=c("#FF0000", "254117"))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/354417.html
上一篇:為什么標題按鈕onPress的行為與常規按鈕onPress不同?
下一篇:在R中繪制鏡像直方圖并處理NA
