这是我在StackOverflow的第一个问题,所以请告诉我,如果我在发布过程中犯了一些错误,我会尽快编辑问题。
我是一名ggplot2新手,我想用这个套件绘制radiotracking时期。我的数据包括2个不同年份(2011-2012)活跃季节(从05年5月15日至15月15日)以不均匀间隔(VHF类型数据)采集的动物的坐标位置。
我想做的是以尽可能最好的方式显示每个人在一年中的活跃季节中放射性跟踪的程度。
我知道这听起来很复杂,因此我举了一个例子,说明了我的意思以及到目前为止我能够实现的目标。 “掉落”一栏意味着发射器从动物身上掉落并随后被重新附着,这也意味着在没有发射器的那段时间内,动物无法进行放射性跟踪。
library(ggplot2)
library(scales)
##transform the data for ggplot
traj$Date<-as.Date(as.character(traj$Date),"%Y%m%d")
traj$Date<-as.POSIXct(traj$Date)
traj$Year<-as.factor(traj$Year)
##plot graph
inf<-as.POSIXct(as.Date('20110515',format="%Y%m%d"))
sup<-as.POSIXct(as.Date('20110915',format="%Y%m%d"))
direplot <- ggplot(traj, aes(x=Date, y=Name,colour=Year,drop=TRUE)) +
geom_point()+
labs(title="Radiotracking scheme",x="Month")+
scale_x_datetime(limits=c(inf,sup),breaks = date_breaks("1 month"),labels=date_format("%b"))+
facet_grid(Sex~.,scales="free",drop=TRUE)+guides(size=FALSE)
direplot
无论如何,情节内容丰富但仍然不完美:
盒子或酒吧比积分更好。我曾经尝试过boxplot()没有好结果,因为coord_flip和facet不能很好地使用这个函数。
我希望当遇到被删除列中的值“是”时,条形图/框“停止”,然后继续下一个可用的重定位(不知道是否可行)。
在y轴标签下移动y轴相对侧的“male”和“female”类别的标签。
对代码或建议的任何实现都将非常感激,如果您需要澄清,我会处理。 非常感谢您提前,
流浪汉
这是我的数据:
dires<- "Name Date Sex Year Dropped
1 Nymeria 20110603 Female 2011 no
2 Nymeria 20110604 Female 2011 no
3 Nymeria 20110605 Female 2011 no
4 Nymeria 20110606 Female 2011 no
5 Nymeria 20110607 Female 2011 no
6 Nymeria 20110609 Female 2011 no
7 Nymeria 20110610 Female 2011 yes
8 Nymeria 20110811 Female 2011 no
9 Nymeria 20110812 Female 2011 no
10 Nymeria 20110816 Female 2011 no
11 Nymeria 20110817 Female 2011 no
12 Nymeria 20110818 Female 2011 no
13 Nymeria 20110825 Female 2011 yes
14 Ghost 20110518 Male 2011 no
15 Ghost 20110520 Male 2011 no
16 Ghost 20110521 Male 2011 yes
17 Ghost 20110609 Male 2011 no
18 Ghost 20110610 Male 2011 no
19 Ghost 20110612 Male 2011 no
20 Ghost 20110619 Male 2011 no
21 Ghost 20110620 Male 2011 no
22 Ghost 20110621 Male 2011 no
23 Ghost 20110622 Male 2011 no
24 Ghost 20110704 Male 2011 no
25 Ghost 20110721 Male 2011 no
26 Ghost 20110725 Male 2011 yes
27 Grey_wind 20110515 Male 2012 no
28 Grey_wind 20110516 Male 2012 no
29 Grey_wind 20110524 Male 2012 no
30 Grey_wind 20110610 Male 2012 no
31 Grey_wind 20110611 Male 2012 no
32 Grey_wind 20110614 Male 2012 no
33 Grey_wind 20110615 Male 2012 no
34 Grey_wind 20110630 Male 2012 yes
35 Grey_wind 20110721 Male 2012 no
36 Grey_wind 20110725 Male 2012 no
37 Grey_wind 20110726 Male 2012 no
38 Grey_wind 20110727 Male 2012 no
39 Grey_wind 20110731 Male 2012 yes
40 Lady 20110515 Female 2012 no
41 Lady 20110516 Female 2012 no
42 Lady 20110530 Female 2012 no
43 Lady 20110610 Female 2012 no
44 Lady 20110613 Female 2012 no
45 Lady 20110614 Female 2012 no
46 Lady 20110615 Female 2012 yes
47 Lady 20110727 Female 2012 no
48 Lady 20110731 Female 2012 no
49 Lady 20110802 Female 2012 no
50 Lady 20110808 Female 2012 no
51 Lady 20110809 Female 2012 no
52 Lady 20110811 Female 2012 yes"
traj <- read.table(text=dires, header = TRUE)
编辑我添加一张gimp编辑的图片,说明理想的结果。可能不可能在R中直接获得这样的情节,但越接近越好。
EDIT2 经过一些实验,我能够获得更接近的东西,尽管仍然不完美。由于我的主管告诉我她不需要这个图表的单个日期,而只需要周期,我重新构建了数据,以便在一个发送器“丢弃”和另一个发送器之间产生时间跨度。然后我只是增加了单点的大小,以获得看起来像条的东西。如果社区感兴趣,我可以发布代码(代码片段似乎不起作用)
答案 0 :(得分:0)
尝试:
traj$Tracked = ifelse(traj$Dropped=='yes',0,1)
ggplot(traj, aes(x=Date, y=Tracked, fill=Sex ))+geom_bar(stat='identity', position='dodge')+facet_grid(Name~.)+scale_y_continuous(breaks=c(0,1))