仅绘制R中列表中的特定项目

时间:2017-06-27 15:23:30

标签: r list dataframe ggplot2 apply

我需要为不同年份的网络中的用户绘制一系列中心值。

我只对绘制一些内容感兴趣,但我无法弄清楚如何做到这一点并不是每年都会出现所有用户。

这是一个数据示例。我希望能够为"jhpedraza"绘制值,例如,甚至更好地绘制users=c("jhpedraza","other user")之类的字符串。请尝试"char_arturo"以重现所有可能的错误。

l=structure(list(`2009` = NULL, `2010` = NULL, `2011` = structure(c(0.0112191199212738, 
0.0119663133080306, 0.0112191199212738), .Names = c("jhpedraza", "didactech", 
"juanmanuelcorzo")), `2012` = structure(c(0.00520863174452703, 0.00543486753203931), 
.Names = c("jhpedraza", "lasillaenvivo")), `2013` = structure(c(0.00457122723603219, 
0.00362782800771276, 0.00342927774646075), .Names = c("jhpedraza", "milobeta", 
"char_arturo"))), split_type = "data.frame", split_labels = structure(list
(`format(Date, "%Y")` = c("2009", "2010", "2011", "2012",  "2013")), .Names = 
"format(Date, \"%Y\")", class = "data.frame", row.names = c(NA, -8L)), .Names = 
c("2009", "2010", "2011", "2012", "2013"))

这是我之后的情节示例。经过大量数据操作后我设法做到了,但我相信有更好的解决方案:enter image description here

3 个答案:

答案 0 :(得分:1)

您可以使用sapply

sapply(l, i="jhpedraza", function(x,i) x[i])

或通过索引:

sapply(l, i=1, function(x,i) x[i])

然后你可以使用plyr包将它们放在一个可用于绘图的data.frame内;

library(plyr)
df1 <- ldply(sapply(l, i=1, function(x,i) x[i]), data.frame)

示例图:

ggplot() + geom_line(aes(x=as.numeric(.id),y=X..i..),
                 data = df1, stat="identity") +
  scale_x_continuous(breaks=as.numeric(df1$.id), labels=as.numeric(df1$.id)) + 
  ggtitle("Example Plot") + labs(x="Year", y="Value") +
        theme(panel.background = element_rect(fill = "#eff0f1",
                                  colour = "#eff0f1"),
        plot.background = element_rect(fill = "#eff0f1"))

            https://i.stack.imgur.com/9kWUN.png

更新:如何处理列表中的缺失值?

正如您所提到的,列表中缺少值和空值,不同的列等,这可能对您有所帮助:

df.completed <- plyr::ldply(lapply(l, Filter, f = Negate(is.null)),rbind)

这将为您提供一个数据框,其中包含每年的行(列表中的每个数据框)和每列。缺少列的值将使用NA填充。

对于您的示例数据集,这将是输出:

# > df.completed

#    .id   jhpedraza  didactech juanmanuelcorzo lasillaenvivo    milobeta char_arturo 
# 1 2011 0.011219120 0.01196631      0.01121912            NA          NA          NA 
# 2 2012 0.005208632         NA              NA   0.005434868          NA          NA 
# 3 2013 0.004571227         NA              NA            NA 0.003627828 0.003429278

您可以绘制这样的任何列(只需将jhpedraza更改为所需的列):

  ggplot() + geom_line(aes(x=as.numeric(.id),y=jhpedraza),
           data = df.completed[complete.cases(df.completed$jhpedraza),], stat="identity") +
    scale_x_continuous(breaks=as.numeric(df1$.id), labels=as.numeric(df1$.id)) + 
    ggtitle("Example Plot") + labs(x="Year", y="jhpedraza") + theme_bw()

            https://i.stack.imgur.com/emNAo.png

答案 1 :(得分:0)

如果你用tidyr和dplyr这样做会更容易:

a <- 1; b <- 2; c <- 3

p2001 <- data.frame(a,b,c, yr = 2001)

a <- 2; b <- 1; c <- 3; e <- 3

p2002 <- data.frame(a,b,c,e, yr = 2002)

a <- 2; c <- 3; e <- 3; f <- 4

p2003 <- data.frame(a,c,e,f, yr = 2003)

library(tidyr)
library(dplyr)
p.years <- bind_rows(p2001,p2002,p2003)

p.years.gathered <- gather(p.years,key = USER,value = VALUE,c(1,2,3,5,6))

现在,您可以对所有用户进行绘图,或将数据框子集化为您感兴趣的用户。您只需要在bind_rows之后查看哪个列具有yr,并相应地调整聚集。

答案 2 :(得分:0)

这是我选择的解决方案,使用列表中另一个SO问题的代码:

ttt <- do.call(rbind, lapply(l, data.frame, stringsAsFactors=FALSE))

ttt$an <- rownames(ttt)

text <- as.character(ttt$an)

p2=as.data.frame(text) %>% separate(text, into = c("Year","User"))

ttt <- cbind(p2,ttt)

names(ttt) <- c("Year","User","PageRank","id") 

ids <- c("jhpedraza","lasillaenvivo") 

qqq <- subset(ttt,User %in% ids)


ggplot(qqq, aes(y = log(PageRank), x = Year, colour = as.factor(User))) +
      geom_point() +
      geom_smooth(aes(group = as.factor(User)),se=FALSE)+ggtitle("Centrality by Year") + labs(x="Year", y="Page Rank (log)") + labs(color='User')

enter image description here