问题
Do any ggplot2 wizards out there know how to make this
look more visually intuitive? Specifically, I'm thinking of annotation at the beginning and end of each time period with the exact date (i.e., for Zhenla the bar would begin with an annotation saying 550 AD, and end with an annotation saying 802AD).
As an aside, I've looked at the annotation documentation and haven't found anything visually appealing. However, knowing ggplot2 and its family of packages as I do, I'm certain there is an attractive option out there.
I'm also curious about any other suggestions you all may have about making this more aesthetically pleasing.
Here's the data:
cambodia = data.frame(Period = c("Funan", "Chenla/Zhenla","Khmer Empire","Dark Ages of Cambodia"),StartDate = c(-500,550,802,1431), EndDate = c(550,802,1431,1863), Color = c("lightblue","lightgreen","lightyellow","pink"))
Note: "Color" does not correspond with the values given- it's more of a placeholder for the graph code:
g2 <- ggplot() +
geom_segment(data=cambodia, aes(x=StartDate, xend=EndDate, y=Period, yend=Period, color=Color), linetype=1, size=2) +
scale_colour_brewer(palette = "Pastel1")+
xlab("Time")+
ylab("Periods of History")+
theme_bw() + theme(panel.grid.minor = element_blank(), panel.grid.major = element_blank()) + theme(aspect.ratio = .2)
g2 + theme(legend.position="none")
回答1:
It's not really a programming question, but I think it's still interesting as it shows how to play with ggplot possibilities. I would put all segments at the same height, and use the x-axis to show the main dates you're interested in (I'm not sure about where to place the text, though):
ggplot(data=cambodia) +
geom_segment(aes(x=StartDate, xend=EndDate, y=0., yend=0., color=Period) , linetype=1, size=4) +
scale_colour_brewer(palette = "Pastel1")+
scale_y_continuous(limits=c(0,0.5))+
scale_x_continuous(limits=c(-500,2000), breaks= c(seq(0,2000,by=1000), cambodia$StartDate, cambodia$EndDate[4]))+
xlab("Time")+
ylab("Periods of History")+
theme_bw() + theme(panel.grid.minor = element_blank(), panel.grid.major = element_blank(), axis.title.y=element_blank(),axis.text.y=element_blank(), axis.ticks.y=element_blank()) +
theme(aspect.ratio = .2)+
theme(legend.position="none") +
geom_text(aes(x=StartDate-100 + (EndDate- StartDate)/2,y=0.05,label=Period,angle=25,hjust=0))
来源:https://stackoverflow.com/questions/43213090/ggplot2-creating-a-visually-intuitive-timeline-in-r