问题
I want to plot a timeline with R where the periods are easily identifiable, in which I could personalize the visualization of:
- periods
- colors of periods 'boxes'
- lines (color, position)
- position of the text and fit it into the 'boxes'
- axis (size, color, chose the ones to put emphasis)
- dates with the events
- etc
I use timeline library, however I couldn't find how to personalized it. Any suggestions or other libraries?
The output looks like this:
My R code is this:
require(timeline)
f <- "~/Documents/periods.csv"
crono <- read.delim(f, header=TRUE)
f <- "~/Documents/events.csv"
events <- read.delim(f, header=TRUE)
draw <- function() {
timeline(crono, events,
text.size = 8,
text.color = "black",
num.label.steps = 2,
event.label.method = 1,
event.text.size = 7,
event.label = '',
event.line = TRUE,
event.above = FALSE)
}
png("~/Documents/Timeline.png", width = 1200,
height = 800, units = "px", bg = "transparent", res = NA)
draw()
dev.off()
Here there is my data. Series of periods of time:
Name Group Start_year End_year
First long period long 1800 1899
Second period short 1870 1910
Another long period long 1900 1990
More events on period time short 1965 1985
and some events during the same time:
Event year
Person 1 was born 1870
Person 1 first novel 1895
Build the new building 1905
Death person 1 1930
renovation building 1950
collection 1970
回答1:
using package vistime
, you could personalize colors of the boxes (if you add "color" column in your data frame and tell vistime
with colors='yourColourColumnName'
, you can add tooltips and distribute into groups (groups=
). Since the result is a plotly
-Object, you can manipulate all the attributes using plotly_build(...)
.
install.packages("vistime")
library(vistime)
crono <- read.csv(text="Name,Group,start_year,end_year
First long period,long,1800-01-01,1899-12-31
Second period,short,1870-01-01,1910-12-31
Another long period,long,1900-01-01,1990-12-31
More events on period time,short,1965-01-01,1985-12-31")
events <- read.csv(text="Name,start_year
Person 1 was born,1870-01-01
Person 1 first novel,1895-01-01
Build the new building,1905-01-01
Death person 1,1930-01-01
renovation building,1950-01-01
collection,1970-01-01")
events$end_year <- NA
events$Group <- "Events"
vistime(rbind(crono, events), start="start_year", end="end_year", events="Name", groups="Group")
回答2:
Plotting and personalizing using timeline is easy because the timeline library is built using ggplot2 mechanics. So, if for instance you wanted to make your timeline have the Economist theme, you could do the following (using the code you provided):
library(ggthemes)
library(ggplot2)
p <- timeline(crono, events,
text.size = 8,
text.color = "black",
num.label.steps = 2,
event.label.method = 1,
event.text.size = 7,
event.label = '',
event.line = TRUE,
event.above = FALSE)
p + + theme_wsj() + scale_colour_wsj("colors6", "")
All other ggplot2 codes should work as well.
来源:https://stackoverflow.com/questions/32447483/how-to-personalize-a-timeline-with-r