用ggplot2绘制不同长度的矢量 [英] Plot vectors of different length with ggplot2
本文介绍了用ggplot2绘制不同长度的矢量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有8个不同长度的数据向量(MAP分数)(从80到500不等的文档数量)。我如何读取它们到R中并在ggplot2中将它们绘制成相同长度?考虑他们不同数量的数据点,范围从0到1.它们应该缩小/放大,以便它们适合相同的图形。并添加一个更流畅的图片。
举例来说,我得到了矢量
I've got 8 data vectors (MAP scores) of different length (different amount of documents rated), from 80 to 500. How do I read them to R and plot them to the same length in ggplot2? Consider them a different amount of datapoints ranging from 0 to 1. They should be scaled down/up so they fit into the same graph. And add a smoother to the picture. The scores range from 0 to 1. As example, I've got the vectors
vec1 = [1,0.8,0.6,0.8,0.6,0.6] # => +
vec2 = [1,0.8,0.6,0.4] # => *
和情节应该如下所示:
+
+* +
+ *+ +
*
但带有行。
推荐答案
$ b
Here you go.
dat <- list(
vec1 = c(1,0.8,0.6,0.8,0.6,0.6), # => +
vec2 = c(1,0.8,0.6,0.4) # => *)
)
addX01 <- function(x, label="A"){
n <- length(x) - 1
data.frame(x=seq(0, 1, by=(1/n)), y=x, label=label)
}
raggedListToDf <- function(x, labels=LETTERS[seq_along(x)]){
do.call(rbind, lapply(seq_along(x), function(i) addX01(x[[i]], label=labels[i])))
}
plotData <- raggedListToDf(dat, labels=c("*", "+"))
ggplot(plotData, aes(x, y, label=label, group=label)) + geom_text()
这篇关于用ggplot2绘制不同长度的矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文