gpt4 book ai didi

r - ggplot2:如何标记事件发生的日期

转载 作者:行者123 更新时间:2023-12-03 01:58:15 25 4
gpt4 key购买 nike

我想从第二个图中获取第一个图中的信息。

Plot 1

第二个图表示事件发生的天数。它看起来更宽,因为它没有图例,但它是相同的时间尺度。我选择在第一个图中手动分配颜色。

我想将第二个绘图点/符号覆盖在第一个绘图上(即组合它们),以便可以在一个绘图中看到事件。理想情况下,这将是 Y 上高度为 0.33 的一行点,并且在右端将有字母 F 标记点 - 高度为 0.33。你能帮忙吗?数据和绘图代码如下:
Plot 2

图 1:

ggplot(dfSub, aes(x=Date)) + 
geom_smooth(aes(y=P,col='P'),span=0.5,se=F) +
geom_smooth(aes(y=A,col='A'),span=0.5, se=F) +
geom_smooth(aes(y=DEP,col='DEP'),span=0.5, se=F) +
geom_bar(aes(y=R,col='R'),stat="identity",alpha=0.5) +
scale_color_manual("",
values=c('P'='red','A'='purple','DEP'='blue',R='grey')) +
ylab("Scale 0 to 4")

图 2:

ggplot(dfSub, aes(x=Date)) + 
geom_point(data=subset(dfSub, !is.na(F)), aes(y=F))

数据:

dfSub <- structure(list(Date = structure(c(17539, 17540, 17541, 17542, 
17543, 17544, 17545, 17546, 17547, 17548, 17549, 17550, 17551,
17552, 17553, 17554, 17555, 17556, 17557, 17558, 17559, 17560,
17561, 17562, 17563, 17564, 17565, 17566, 17567, 17568, 17569,
17570, 17571, 17572, 17573, 17574, 17575, 17576, 17577, 17578,
17579, 17580, 17581, 17582, 17583, 17584, 17585, 17586, 17587,
17588, 17589, 17590, 17591, 17592, 17593, 17594, 17595, 17596,
17597, 17598, 17599, 17600, 17601, 17602, 17603, 17604, 17605,
17606, 17607, 17608, 17609, 17610, 17611, 17612, 17613, 17614,
17615, 17616, 17617, 17618, 17619, 17620, 17621, 17622, 17623,
17624, 17625, 17626, 17627, 17628, 17629), class = "Date"), R = c(1,
2, NA, 1, 1, 2, NA, 1, 1, NA, 1, NA, 1, 1, 1, NA, NA, NA, 1,
NA, NA, NA, 1, 1, NA, 1, NA, NA, NA, NA, NA, NA, 2, NA, NA, 0,
1, 1, 1, 0, 1, 0, NA, NA, NA, 1, 1, 1, NA, NA, NA, NA, 1, NA,
1, 2, 1, 1, 3, 1, NA, 2, NA, NA, 1, 2, 1, 2, NA, NA, 1, NA, NA,
2, 1, NA, 1, 2, 1, NA, 1, 2, 1, NA, 1, 1, NA, 1, NA, NA, NA),
P = c(2, 4, 1, 1, 1, 2, 4, 0, 4, 1, 1, 0, 0, 2, 4, 4, 2,
2, 2, 4, 2, 1, 3, 2, 1, 3, 1, 0, 1, NA, 2, NA, 4, 4, 1, 3,
4, 4, 3, 2, 4, 4, 3, 0, 0, 2, 1, 3, 2, 0, 0, 0, 0, 1, 2,
4, 4, 3, 3, 3, 0, 2, 2, NA, 1, 2, 1, 1, 1, 1, 1, 1, 1, 3,
1, 1, 2, 4, 2, 0, 1, 4, 3, 1, 3, 1, 0, 0, 0, 0, 0), F = structure(c(NA,
NA, 1L, 1L, 1L, 1L, 1L, 1L, NA, 1L, 1L, 1L, 1L, NA, 1L, 1L,
NA, NA, NA, NA, 1L, NA, 1L, NA, 1L, 1L, 1L, 1L, 1L, NA, 1L,
1L, 1L, NA, NA, NA, NA, 1L, NA, 1L, NA, 1L, 1L, 1L, NA, NA,
NA, NA, 1L, 1L, NA, 1L, NA, 1L, NA, 1L, NA, NA, NA, NA, NA,
NA, NA, NA, NA, 1L, NA, NA, NA, NA, 1L, 1L, NA, NA, NA, 1L,
1L, NA, NA, NA, NA, NA, NA, NA, NA, 1L, 1L, NA, NA, NA, NA
), .Label = "X", class = "factor"), DEP = c(NA, NA, 1.5,
NA, NA, NA, NA, 1.5, NA, 1.5, 2, 2, 2, 2, 1.5, 1, 1.5, 1.5,
1, 1, 1.5, 1, 2, 2, 1.5, 1, 1, 0, 1, 0.5, 1, 0.5, 1, 1, 1,
1, 0, 1.5, 1, 1, 1.5, 0.5, 1, 2, 1.5, 1.5, 1.5, 1.5, 1.5,
1.5, 1.5, 2, 2, 2, 2, 1.5, 1.5, 2, 2.5, 2, 1.5, 1, 1.5, 1,
NA, 2, 2.5, 2, 1.5, 1.5, 1, 1.5, 1.5, 1, 1.5, 2, 2, 1, 1.5,
2, 1, 1.5, 2, 2, 1.5, 2, 2, 1.5, 1, 2, 2), A = c(NA, NA,
NA, NA, NA, NA, NA, NA, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2,
1, 1, 1, NA, 2, 1, 1, 1, 1, 1, 1, NA, 1, 1, 2, 1, 1, 1, 1,
1, 1, 1, 1, 1, NA, 1, 1, 1, 1, 1, 1, 1, 1.5, 1, 1, 1, 1,
2, 2, 1, 2, 3, 1, 2, 1, 1, 1, 1, 1, 1, 1.5, 1, 1, 2, 1, 1,
1, 1, 1, 1, 1, 1, 1, 3, 2, 2, 2, 1, 1, 1, 1, 1), FAS = structure(c(NA,
NA, 1L, 1L, NA, NA, NA, 1L, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA
), .Label = "X", class = "factor")), .Names = c("Date", "R",
"P", "F", "DEP", "A", "FAS"), class = "data.frame", row.names = c(NA,
-91L))

最佳答案

首先,我认为如果将数据重新调整为长格式而不是宽格式,您会更轻松。 ggplot 需要长形数据。这样您就可以将美学(在您的例子中是颜色)映射到变量,然后调用 geom_smooth 一次而不是多次。

同样作为一种简化,geom_col() 相当于 geom_bar(stat = "identity")

@Gregor 的上述建议很好:如果您只需要一两段文本,则 annotate 调用非常简单。在本例中,我使用 x = max(df_long$Date), y = 0.33 完成此操作,但您可以将其放置在您想要的任何日期。

如果有任何不清楚的地方,请告诉我。

library(tidyverse)
# deleted long line of reading in data frame
df_long <- dfSub %>%
select(-FAS) %>%
mutate(`F` = as.numeric(`F`)) %>%
gather(key = group, value = value, -Date) %>%
filter(!is.na(value))

ggplot(df_long, aes(x = Date, group = group)) +
geom_smooth(aes(y = value, color = group), data = . %>% filter(group %in% c("P", "A", "DEP")), span = 0.5, se = F) +
geom_col(aes(y = value), data = . %>% filter(group == "R"), alpha = 0.5, fill = "grey") +
geom_point(aes(color = "F"), y = 0.33, data = . %>% filter(group == "F")) +
annotate("text", x = max(df_long$Date), y = 0.33, label = "F") +
scale_color_manual("", values = c("P" = "red", "A" = "purple", "DEP" = "blue", "F" = "gray30")) +
ylab("Scale 0 to 4")
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'

reprex package于2018年4月23日创建(v0.2.0)。

添加 4/29:对于点,这里有一个使用数据的示例,其中 y 值不像原始帖子那样只是硬编码。

set.seed(1234)
dots <- data.frame(
value = rep(c(0.5, 1, 1.5), each = 20),
Date = sample(unique(df_long$Date), size = 60, replace = T)
)


ggplot() +
geom_col(
aes(x = Date, y = value),
data = df_long %>% filter(group == "R"),
fill = "gray60"
) +
geom_point(aes(x = Date, y = value), data = dots)

关于r - ggplot2:如何标记事件发生的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49988676/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com