[英]R - discrete colours for continuous data in ggplot
我正在嘗試繪制一些時間連續的數據。 我已經應用了一個簡單的算法,為數據的每一行分配一個離散數字( state
)。 它看起來像這樣。
time flow pre state
0.0 0.0 3 1
0.2 0.01 4 1
0.4 0.10 10 2
0.6 -0.11 -2 0 # Set as NA in the example below
現在,我想繪制實例流(使用ggplot
)並讓state
確定顏色。 問題是,如果我這樣做
ggplot(data) +
geom_line(aes(x = time, y = flow, color = state))
該圖的顏色對比度太小,無法輕易區分state
。 但如果我這樣做
ggplot(data) +
geom_line(aes(x = time, y = flow, color = factor(state))) +
scale_color_manual(values = c("red", "green", "blue"))
它分割線條,它們顯示為三條不同的線條。 我想要的是使用連續比例,但添加一個或幾個中間顏色的對比度。 目前的解決方案是這樣
ggplot(data = alg1) +
geom_line(aes(x = time, y = flow, colour = state)) +
scale_color_gradient(low = "#0000FF", high = "#FF0000",
na.value = "#00FF00", guide = "legend")
但是這1)僅適用於三種狀態,其中一種必須是NA
,2)從圖例中排除一種狀態( NA
),3)在圖例中顯示未使用的值。
回答評論:
通過aes(x = time, y = flow, color = factor(state), group = 1)
分組aes(x = time, y = flow, color = factor(state), group = 1)
可防止在將狀態轉換為因子時繪制單獨的線。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.