如何按日期绘制频率?表的问题

时间:2017-09-26 12:02:41

标签: r date ggplot2 time

我有一张桌子

    library(RISmed)
search_topic <- "BID"
search_query <- EUtilsSummary(search_topic, mindate = 2016, maxdate=2018)
summary(search_query)
QueryId(search_query)
records <- EUtilsGet(search_query)
y <- data.frame(cbind("year"= YearPubmed(records), "month"= MonthPubmed(records)))
date()
count<-table(y)
count

month
year    1  2  3  4  5  6  7  8  9 10 11 12
  2016 49 54 49 59 45 54 43 44 40 47 42 42
  2017 52 35 52 48 30 37 43 42 25  0  0  0

我创建了一个带有计数的数据框,但是一旦我尝试将它变成as.Date,就转换为NA。

我想按日期绘图,我也可以得到这一天,但对我来说并不重要。 但我不断收到错误。字符串不是模糊的格式。 像这样:

Graph of Input(BID)

我似乎无法做到这一点。

我想绘制频率随时间的变化(过去2年)。

这是我制作的一个闪亮的应用程序,但输出正在杀死我。

有什么建议吗?

我感谢任何反馈。

2 个答案:

答案 0 :(得分:2)

试试这个:

library(RISmed)
library(dplyr)
library(ggplot2)
search_topic <- "BID"
search_query <- EUtilsSummary(search_topic, mindate = 2016, maxdate=2018)
summary(search_query)
QueryId(search_query)
records <- EUtilsGet(search_query)
y <- data.frame(cbind("year"= YearPubmed(records), "month"= MonthPubmed(records)))
date()
count<-table(y)
count

y$date <- as.Date(strptime(paste(y$year, y$month, "01", sep="-"), "%Y-%m-%d", tz = "UTC"), origin="1970-01-01")

y %>% group_by(date) %>% summarise(n.citation = length(date)) %>%
  ggplot(aes(x=date, y = n.citation)) + geom_point()][1]][1]

enter image description here

HTH

詹姆斯

答案 1 :(得分:1)

table

开头
tbl <- structure(c(49L, 52L, 54L, 35L, 49L, 52L, 59L, 48L, 45L, 30L, 
54L, 37L, 43L, 43L, 44L, 42L, 40L, 25L, 47L, 0L, 42L, 0L, 42L, 
0L), .Dim = c(2L, 12L), .Dimnames = structure(list(Year = c("2016", 
"2017"), variable = c("1", "2", "3", "4", "5", "6", "7", "8", 
"9", "10", "11", "12")), .Names = c("Year", "variable")), class = c("xtabs", 
"table"), call = xtabs(formula = value ~ Year + variable, data = melted))

您可以组合Year-Months

Combs <- expand.grid(attributes(tbl)$dimnames$Year, attributes(tbl)$dimnames$variable)

# or you can use
# Combs <- expand.grid(c("2016","2017"), 1:12))

library(lubridate)
preDates <- apply(Combs, 1, function(x) paste0(x, collapse="-"))
sortedDates <- sort(parse_date_time(preDates, "y-m"))

newdf <- data.frame(Date = sortedDates, Value = c(tbl))
plot(Value ~ Date, data=newdf)