我有以下資料框和散點圖
df <-
setNames(data.frame(
as.POSIXct(
c(
"2022-07-29 00:00:00",
"2022-07-29 00:00:05",
"2022-07-29 00:05:00",
"2022-07-29 00:05:05",
"2022-07-29 00:10:00",
"2022-07-29 00:15:00",
"2022-07-29 00:20:00",
"2022-07-29 00:20:05"
)),
c(1, 2, 3, 4, 5, 6, 7, 8),
c(0.8, 2.1, 2.5, 4.1, 5, 6.1, 6.9, 8.1),
c("a", "a", "b", "b", "b", "b", "b", "c")
),
c("timeStamp", "value1", "value2", "text"))
df %>% ggplot(aes(timeStamp, value1, color =text)) geom_point()
我想在圖表上突出顯示特定文本值與資料位于同一行的區域。例如,如果我們想用 geom_rect 突出顯示 b 值,這是我的嘗試:
df %>% ggplot(aes(timeStamp, value1, color =text)) geom_point()
geom_rect(xmin= -00:00:05, xmax= 00:00:05, ymin=-0.2, ymax=0.2, color = ifelse(text=="b",
"yellow", ""), alpha =0.5)
我知道這不起作用,但似乎找不到解決方案。另外,我知道簡單地更改資料點的大小和資料點的顏色似乎更謹慎,但我需要矩形來進行具有更大資料集的特定分析。謝謝!
uj5u.com熱心網友回復:
你可以試試這個
df %>% ggplot(aes(timeStamp, value1, color =text))
geom_point()
annotate("rect",
xmin= min(df$timeStamp[df$text=="b"]),
xmax= max(df$timeStamp[df$text=="b"]),
ymin= min(df$value1[df$text=="b"]),
ymax= max(df$value1[df$text=="b"]),
color = "yellow",fill="yellow", alpha = 0.2 )
theme_bw()
或使用 geom_rect
df %>% ggplot(aes(timeStamp, value1, color =text))
geom_point() geom_rect(data = subset(df,text=="b"),
xmin= df$timeStamp[df$text=="b"] 60,
xmax= df$timeStamp[df$text=="b"]-60,
ymin= df$value1[df$text=="b"] 1,
ymax= df$value1[df$text=="b"]-1,
color = NA, fill="yellow", alpha = 0.2)
theme_light()
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/517977.html
標籤:rif 语句ggplot2强调
