ggplot2:在R中创建视觉直观的时间线 [英] ggplot2: Creating a visually intuitive timeline in R

查看:163
本文介绍了ggplot2:在R中创建视觉直观的时间线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何ggplot2向导都知道如何让这个看起来更直观?具体来说,我想在每个时间段的开始和结束时注意确切的日期(例如,对于Zhenla,酒吧会以注释说550公元开头,最后注明说明802AD)。



顺便说一下,我查看了注释文档,没有发现任何视觉上的吸引力。然而,我知道ggplot2及其系列软件包,我肯定有一个有吸引力的选择。



我也很好奇你的任何其他建议所有可能都有关于使这更美观。



以下是数据:

  cambodia = data.frame(Period = c(Funan,Chenla / Zhenla,Khmer帝国,柬埔寨黑暗时代),StartDate = c(-500,550,802,1431),EndDate = c(550,802,1431,1863),Color = c (lightblue,lightgreen,lightyellow,pink))

颜色不符合给定的值 - 它是图表代码的更多占位符:

  g2 < -  ggplot( )+ 
geom_segment(data = cambodia,aes(x = StartDate,xend = EndDate,y = Period,yend = Period,color = Color),linetype = 1,size = 2)+
scale_colour_brewer ())+
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)
pre>

解决方案

这不是一个真正的编程问题,但我认为它仍然是inte因为它展示了如何玩ggplot的可能性。我会把所有的段放在相同的高度,并使用x轴来显示你感兴趣的主要日期(但我不确定放置文本的位置,但是):



  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(历史时期)+
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())+
主题(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))


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")

解决方案

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))

这篇关于ggplot2:在R中创建视觉直观的时间线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆