- 以下是輸出與時間的示例。
- 我使用持續時間為線條著色。
- 我還想在圖中包含能量水平。如何
geom_rect在繪圖背景中使用矩形面板 ( )。
library(tidyverse)
tbl <- tibble(time = 1:100,
output = - time^2 5*time,
duration = c(rep("start", 30), rep("mid", 40), rep("end", 30)),
energy = c(rep("high", 40), rep("medium", 30), rep("low", 30)))
ggplot(data = tbl,
aes(x = time,
y = output,
color = duration))
geom_line()
theme_bw()
uj5u.com熱心網友回復:
我不能發表評論,但如果你想要 dplyr 版本,一種選擇是
energy= tbl %>% group_by(energy) %>% mutate(first= first(time), last=last(time)) %>%
select(energy, first, last) %>% distinct()
但我喜歡 data.table 版本
uj5u.com熱心網友回復:
我使用 data.table 來創建輔助表能量,我很確定如果你喜歡另一種方法,你可以很容易地轉換它。
setDT(tbl)
library(data.table)
energy <- tbl[, .(first = first(time), last = last(time)), by = energy]
ggplot(data = tbl)
geom_line(aes(x = time, y = output, color = duration), size = 2)
geom_rect(data = energy, aes(NULL, NULL, xmin = first, xmax = last, fill = energy), ymin = -Inf, ymax = Inf, alpha = 0.3)
這將創建下面的圖表

uj5u.com熱心網友回復:
如果您覆寫該y值,您應該能夠將其添加為另一層。通過添加添加透明度alpha = (% transparent)。
ggplot(data = tbl, aes(x = time, y = output, color = duration))
geom_line()
geom_rect(y= anothery, alpha = 0.5)
theme_bw()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/364224.html
下一篇:Y軸沒有排序,ggplot
