R 使用geom_point()绘制峰值标记
资料 代码 我希望使用geom_point将点标记覆盖在镶嵌面的每个峰值上。峰值数据来自另一个函数,该函数的输出如下所示 峰值数据 峰值点尝试 下面是我试图添加的代码,用于处理这些数据,以绘制峰值上的点R 使用geom_point()绘制峰值标记,r,ggplot2,R,Ggplot2,资料 代码 我希望使用geom_point将点标记覆盖在镶嵌面的每个峰值上。峰值数据来自另一个函数,该函数的输出如下所示 峰值数据 峰值点尝试 下面是我试图添加的代码,用于处理这些数据,以绘制峰值上的点 plots=ggplot(test_melt, aes(x =Var1 , y = value)) + geom_line() + facet_wrap(~Var2, nrow = 12) + geom_point(pks_test(aes x= )) theme(strip.t
plots=ggplot(test_melt, aes(x =Var1 , y = value)) +
geom_line() +
facet_wrap(~Var2, nrow = 12) +
geom_point(pks_test(aes x= ))
theme(strip.text = element_blank())+
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.title.y=element_blank())
for (i in 1:length(pks_test)){
if (!is.null(pks_test[[i]]))
tempdf = as.data.frame(pks_test[[i]])
plots = plots + geom_point(data=tempdf,mapping=aes(x=V2,y=V1))
}
但我有一些错误:
In if (pks_test[[i]] != is.null(pks_test[[i]])) tempdf = as.data.frame(pks_test[[i]]) :
the condition has length > 1 and only the first element will be used
有什么想法吗?使用不同的数据集来绘制这样的点。 我不知道为什么我需要把p$放在geom_point层的变量前面
我想也许你只是想知道!is.nullpks_测试[[i]]]。。。2不要通过aes中的$INDER引用列。只有x=V2和y=V1。这似乎有帮助!当我使用ggsave进行绘图或保存到png时,我得到了一个错误:美学必须是长度1或与数据28:x相同,yFix this:geom_pointpks_测试样本代码中的x=,以便它实际上是可复制的并运行的,也许我可以提供更多帮助。这并不能真正回答我的问题。我使用的函数findpeaks,输出data.frame列中峰值的矩阵列表。因此,我需要一些东西来检查findpeaks列表中的下一个矩阵是否不为NULL,使用前两列在相应的刻面上绘制点。好的,我明白了。我认为您已经掌握了这两个数据,从中生成峰值数据集不会有问题。我看到您试图通过循环添加点,这与ggplot不兼容,这是主要问题。
dput(pks_test)
structure(list(C3 = structure(c(1.847, 1.838, 1.838, 1.805, 1.806,
1.852, 1.817, 66, 154, 243, 314, 386, 481, 552, 61, 146, 235,
310, 379, 475, 546, 93, 176, 260, 330, 408, 506, 577), .Dim = c(7L,
4L)), C4 = structure(c(1.971, 1.943, 1.936, 1.942, 1.898, 1.911,
77, 186, 279, 390, 472, 556, 72, 180, 274, 385, 465, 549, 107,
218, 305, 411, 492, 576), .Dim = c(6L, 4L)), C5 = structure(c(1.35,
1.286, 1.302, 1.328, 1.218, 186, 272, 373, 507, 575, 180, 265,
367, 499, 569, 216, 305, 393, 531, 597), .Dim = c(5L, 4L)), C6 = structure(c(2.005,
1.922, 1.799, 1.766, 1.958, 1.986, 36, 121, 176, 231, 344, 465,
29, 114, 171, 225, 336, 458, 69, 147, 199, 254, 368, 494), .Dim = c(6L,
4L))), .Names = c("C3", "C4", "C5", "C6"))
plots=ggplot(test_melt, aes(x =Var1 , y = value)) +
geom_line() +
facet_wrap(~Var2, nrow = 12) +
geom_point(pks_test(aes x= ))
theme(strip.text = element_blank())+
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.title.y=element_blank())
for (i in 1:length(pks_test)){
if (!is.null(pks_test[[i]]))
tempdf = as.data.frame(pks_test[[i]])
plots = plots + geom_point(data=tempdf,mapping=aes(x=V2,y=V1))
}
In if (pks_test[[i]] != is.null(pks_test[[i]])) tempdf = as.data.frame(pks_test[[i]]) :
the condition has length > 1 and only the first element will be used
x <- data.frame(t <- seq(1:100)
,var1 <- rnorm(100)
,peak <- rep(c(rep(0,4),1),20)
)
p <- x[x$peak==1,]
ggplot(x, aes(x = t, y = var1)) + geom_line() + geom_point(data = p, aes(x = p$t, y = p$var1))