我有超过500只狗的存活/采样日期数据,每只狗至少采样了一次,有几只被采样了三到四次。例如。
Microchip_number Date Sampling_occasion
White notched fatso 20,11,2018 First
White notched fatso 28,12,2018 Second
White notched fatso 09,04,2019 Third
White notched fatso 23,10,2019 Fourth
Tuttu Jeevan 06,12,2018 First
Tuttu Jeevan 03,01,2019 Second
Tuttu Jeevan 04,05,2019 Third
Tuppy 22,10,2018 First
Tuppy 20,11,2018 Second
Tuppy 17,04,2019 Third
Tuppy 31,07,2019 Lost to study
我设法将其绘制在ggplot中,但这是一个非常大的图像,需要放大和滚动才能查看每只狗的采样时间。
我发现了根据某个变量(例如月份)拆分大型数据帧或使用facet_wrap的建议,但就我而言,我没有任何此类变量可以使用。有没有一种方法可以将这个大图分成多个较小的图,而无需放大即可清楚地查看所有细节,例如下面的图(无需单独绘制数据帧的子集)?
How I'd like each split/sub-plot to appear
这是我正在使用的代码
outcomes <- read_xlsx("Dog outcomes.xlsx", col_types = c("text", "date", "text"))
outcomes$Microchip_number<- as.factor(outcomes$Microchip_number)
outcomes$Sampling_occasion<- factor(outcomes$Sampling_occasion,
levels = c("First", "Second", "Third", "Fourth", "Lost to study", "Died"))
g<- ggplot(outcomes)
g + geom_point(aes(x = Date, y = Microchip_number, colour = Sampling_occasion, shape = Sampling_occasion)) +
geom_line(aes(x = Date, y = Microchip_number, group = Microchip_number, colour = Sampling_occasion)) +
theme_bw()
答案 0 :(得分:1)
您可以简单地将dasatet划分为包含相同数量(例如10条)的狗的子组。
添加中间的counter
列以克服小的困难,即每条狗的行数不必相同。
我建议:
library('dplyr')
outcomes <- outcomes %>%
mutate(counter = 1 + cumsum(c(0,as.numeric(diff(Microchip_number))!=0)), # this counter starting at 1 increments for each new dog
subgroup = as.factor(ceiling(counter/10)))
您将获得一个新的数据集,其中包含一个因子subgroup
列,其值每10条狗都不同。然后只需将+ facet_wrap(.~subgroup)
添加到绘图中即可。
希望这会有所帮助。
答案 1 :(得分:1)
非常感谢Jrm FRL,添加counter
和subgroup
列的代码正是我所需要的!正如Gregor提到的,facet_wrap
使得查看起来更加困难,因此我使用了subgroup
的for循环在每个pdf页面(或任何其他设备)上绘制50条狗。这是我使用的代码,并且运行良好,尽管出于某种原因,尽管以其他方式组织了“ Microchip_number
”,但它们以相反的顺序/字母顺序显示(68481、68480、68479等)在主数据框“结果”中四舍五入。但是,有一点小问题!这使得可视化特定个体的结果变得非常容易。干杯!
outcomes2 <- outcomes %>%
mutate(counter = 1 + cumsum(c(0,as.numeric(diff(Microchip_number))!=0)), # this counter starting at 1 increments for each new dog
subgroup = as.factor(ceiling(counter/50)))
pdf(file = "All_outcomes_50.pdf") #
for (i in 1:length(unique(outcomes2$subgroup))) {
outcomes2 %>%
filter(subgroup == i) -> df
ggplot(df) + geom_point(aes(x = Date, y = Microchip_number, colour = Sampling_occasion, shape = Sampling_occasion)) +
geom_line(aes(x = Date, y = Microchip_number, group = Microchip_number, colour = Sampling_occasion)) +
theme_bw() -> wow
print(wow)
}
dev.off()