Plot vectors of different length with ggplot2

2020-03-29 07:53发布

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] # => *

and the plot should look like:

+   
  +*  +
    +  *+  +
           *

but with lines.

标签: r ggplot2
1条回答
淡お忘
2楼-- · 2020-03-29 07:57

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()

enter image description here

查看更多
登录 后发表回答