tôi có giá trị của một năm của dữ liệu mà trông như thế này:Xây dựng một âm mưu Sparkline nhị phân trong R với ggplot2 barplot
datetime, key, value
1/1/15, 7k Steps, 1
1/1/15, Ate Poorly, 1
1/1/15, Audiobook, 1
1/1/15, Befriend, 1
1/1/15, Called Mom, 1
1/1/15, Code, 1
1/1/15, Create, 1
1/1/15, Critical, 1
1/1/15, Emailed Friend, 1
1/2/15, 10k Steps, 1
1/2/15, Ate Poorly, 1
1/2/15, Audiobook, 1
1/2/15, Befriend, 1
1/2/15, Called Mom, 1
1/2/15, Create, 1
1/2/15, Emailed Friend, 1
1/2/15, Exercise, 1
1/2/15, Friend Contact, 1
1/2/15, Great Day, 1
1/2/15, Write, 1
1/3/15, 7k Steps, 1
1/3/15, Ate Poorly, 1
1/3/15, Befriend, 1
1/3/15, Create, 1
1/3/15, Emailed Friend, 1
1/3/15, Friend Contact, 1
1/3/15, Great Day, 1
1/3/15, Happiness, 1
1/3/15, Health, 1
1/3/15, Videogame, 1
1/3/15, Walked With Michelle, 1
1/3/15, Write, 1
1/4/15, 7k Steps, 1
1/4/15, Ate Poorly, 1
1/4/15, Audiobook, 1
1/4/15, Great Day, 1
1/4/15, Happiness, 1
1/4/15, Health, 1
1/4/15, Impatient, 1
1/4/15, Love, 1
1/4/15, Movie With Michelle, 1
Tôi muốn tạo ra một âm mưu có hiển thị một hàng cho mỗi phím với thanh cho mỗi ngày có 1 cho khóa đó. Dưới đây là một ví dụ về đầu ra tôi mong muốn:
Đó là một trong tôi đã đau đớn render bằng Python và Matplotlib.
Tôi đang tìm cách tốt nhất và đơn giản nhất để hiển thị một âm mưu như thế này trong R với, có lẽ, ggplot2. Tôi đã lên kế hoạch sử dụng một cốt truyện thanh trong ggplot2 với một vòng lặp cho mỗi khóa. Dưới đây là ví dụ về mã của tôi:
library(ggplot2)
library(reshape)
#library(ggtheme)
# 2015 Lifedata Processing
d <- read.csv("lifedata_2015.csv")
d$datetime <- as.Date(d$datetime, "%m/%d/%Y")
# Create a new dataframe with a subset of keys
r <- d[d$key %in% c("Read", "Audiobook"), ]
# Put 1s in all values.
r$value <- 1
# Generate a data frame for each day with a value of 1 and a key of "alldates"
mydates <- data.frame("datetime" = seq(as.Date("2015/1/1"), as.Date("2015/12/31"), "days"), "key" = "alldates", "value" = 1)
# combine two data frames, one after the other
n <- rbind(r, mydates)
# Transform into a wide data frame based on datetime and key with mean as the value.
c <- cast(n, datetime~key, mean)
# Turn NaNs into 0
c[is.na(c)] = 0
for(name in c("Read", "Audiobook")){
plt <- c(plt, ggplot(data=c, aes_string(x="datetime", y=name)) +
geom_bar(stat="Identity", width=1))
print(plot)
}
svg("~/Desktop/tagplot.svg")
grid.arrange(plt, ncol = 1, main = "Read")
dev.off()
Kỹ thuật này dường như không hoạt động.
Cách tốt hơn để vẽ dữ liệu sự kiện như tôi có ở trên trong ví dụ là gì?
[Đáng xem] (https://github.com/htmlwidgets/sparkline)? Bạn không chắc chắn nó mạnh như thế nào, nhưng nó nằm trong danh sách "thử" của tôi. – alistaire