我有一个多面图,我要做的就是让每个面都有不同背景阴影区域-这是图形:
当我尝试绘制不同的坐标/构面时,使用geom_rect和annotate(“ rect”)时,它们都像这样重叠在所有它们上:
或导致数据向左移动,并在x轴上放置矩形,从而像这样形成巨大的间隙:
我尝试同时使用数据框,但这也会导致图形发生偏移,并且尝试了所有我能找到但没有运气的建议内容,希望它是代码的简单问题。
非常感谢您提供的帮助,
我的geom_rect图的基本代码如下;
rm(list=ls())
library("ggplot2")
data<- read.csv("circadian_time_n8.csv", header = TRUE, sep=",")
head(data)
as.data.frame(data)
data$id<-factor(data$id)
data$binary<-factor(data$binary)
ggplot(data, aes(x=factor(time), y=factor(binary)),fill="black")+
geom_bar(stat='identity')+
facet_wrap(~id, ncol=1,strip.position = "right")+
geom_rect(aes(xmin = "2000", xmax = "2400", ymin = 0, ymax = 2),
fill = "pink", alpha = 0.03)+
scale_y_discrete(limits = c(0,2), breaks=c(0,1,2), label=c("Inactive", "walk", "run"))+
scale_x_discrete(breaks=seq(1600,3200,100),
label=c("16:00", "17:00", "18:00", "19:00", "20:00","21:00", "22:00", "23:00","00:00", "01:00","02:00", "03:00","04:00", "05:00", "06:00", "07:00", "08:00"))+
labs(x="Time", y="Activity level")+
theme_bw()+
theme(panel.border = element_blank())+
theme(panel.grid.major = element_line(size = 1))+
theme(axis.text.x = element_text(angle = 45, hjust = 1))
并用于注释(“ rect”)
ggplot(data, aes(x=factor(time), y=factor(binary)),fill="black")+
geom_bar(stat='identity')+
facet_wrap(~id, ncol=1,strip.position = "right")+
scale_y_discrete(limits = c(0,2), breaks=c(0,1,2), label=c("Inactive", "walk", "run"))+
scale_x_discrete(breaks=seq(1600,3200,100),
label=c("16:00", "17:00", "18:00", "19:00", "20:00","21:00", "22:00", "23:00","00:00", "01:00","02:00", "03:00","04:00", "05:00", "06:00", "07:00", "08:00"))+
labs(x="Time", y="Activity level")+
theme_bw()+
theme(panel.border = element_blank())+
theme(panel.grid.major = element_line(size = 1))+
theme(axis.text.x = element_text(angle = 45, hjust = 1))+
annotate("rect", xmin = "1930", xmax = "3030", ymin = 0, ymax = 2,alpha=.2)
我尝试过的数据框的格式与该答案中建议的格式相同; Different geom_rect() objects for facets
数据;
> str(data)
'data.frame': 8153 obs. of 6 variables:
$ X24hr : Factor w/ 2046 levels "00:00:00","00:01:00",..: 546 547 548 549 550 551 552 553 554 555 ...
$ id : Factor w/ 8 levels "231","235","236",..: 6 6 6 6 6 6 6 6 6 6 ...
$ bark : num 29.3 35.3 18.7 20.7 11.5 ...
$ activity: Factor w/ 3 levels "high activity",..: 1 1 1 1 1 3 3 3 3 1 ...
$ binary : Factor w/ 3 levels "0","1","2": 3 3 3 3 3 2 2 2 2 3 ...
$ time : int 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 ...
较短版本的数据,因为此处的数据集太大;
> dput(data)
structure(list(X24hr = structure(c(11L, 12L, 13L, 14L, 15L, 16L,
17L, 18L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
13L, 14L, 15L, 16L, 17L, 18L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 1L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L,
18L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L,
14L, 15L, 16L, 17L, 18L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L), .Label = c("00:00:00", "01:00:00",
"02:00:00", "03:00:00", "04:00:00", "05:00:00", "06:00:00", "07:00:00",
"08:00:00", "09:00:00", "16:00:00", "17:00:00", "18:00:00", "19:00:00",
"20:00:00", "21:00:00", "22:00:00", "23:00:00"), class = "factor"),
id = structure(c(6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("231",
"235", "236", "237", "263", "267", "269"), class = "factor"),
bark = c(29.2727, 10.5455, 0.3636, 2.5455, 0, 0, 0, 0.1818,
0, 0, 0, 2, 0, 0, 0, 1.4545, 8.3636, 11.8182, 0.7273, 6.7273,
1.6364, 0.1818, 0, 0, 0, 12.1818, 0.1818, 0, 1.0909, 0, 0,
0, 0.1818, 0, 2.1818, 12.1818, 0, 6.7273, 0, 0, 0, 11.4545,
0.3636, 0, 0, 0.7273, 4.1818, 19.0909, 4, 2.1818, 0, 0.1818,
4.5455, 5.2727, 9.4545, 50.5455, 10.1818, 9.2727, 0.7273,
0, 0.3636, 0, 0, 0, 0, 0.1818, 0.5455, 0, 42.3636, 0, 0.1818,
13.0909, 43.0909, 0, 3.8182, 0, 0, 0, 0, 1.2727, 0.5455,
0, 0.3636, 0, 3.0909, 0, 10.1818, 2, 5.2727, 2, 29.2727,
15.2727, 8.3636, 1.8182, 0, 5.4545, 0, 3.2727, 0.3636, 1.6364,
1.2727, 2.9091, 0.3636, 0, 0, 0, 1.4545, 6, 10.3636, 42.9091,
4.7273, 0, 0, 0, 16, 12.5455, 4.5455, 0.1818, 1.0909, 0,
0, 5.2727, 12.5455, 2.5455, 5.2727), activity = structure(c(1L,
1L, 3L, 3L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 3L,
3L, 1L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 1L, 3L, 2L, 3L, 2L, 2L,
2L, 3L, 2L, 3L, 1L, 2L, 3L, 2L, 2L, 2L, 1L, 3L, 2L, 2L, 3L,
3L, 1L, 3L, 3L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 3L, 3L, 2L, 3L,
2L, 2L, 2L, 2L, 3L, 3L, 2L, 1L, 2L, 3L, 1L, 1L, 2L, 3L, 2L,
2L, 2L, 2L, 3L, 3L, 2L, 3L, 2L, 3L, 2L, 1L, 3L, 3L, 3L, 1L,
1L, 3L, 3L, 2L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L,
3L, 3L, 1L, 1L, 3L, 2L, 2L, 2L, 1L, 1L, 3L, 3L, 3L, 2L, 2L,
3L, 1L, 3L, 3L), .Label = c("high activity", "inactive",
"low activity"), class = "factor"), binary = structure(c(3L,
3L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L,
2L, 3L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 3L, 2L, 1L, 2L, 1L, 1L,
1L, 2L, 1L, 2L, 3L, 1L, 2L, 1L, 1L, 1L, 3L, 2L, 1L, 1L, 2L,
2L, 3L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 2L, 2L, 1L, 2L,
1L, 1L, 1L, 1L, 2L, 2L, 1L, 3L, 1L, 2L, 3L, 3L, 1L, 2L, 1L,
1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 3L, 2L, 2L, 2L, 3L,
3L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L,
2L, 2L, 3L, 3L, 2L, 1L, 1L, 1L, 3L, 3L, 2L, 2L, 2L, 1L, 1L,
2L, 3L, 2L, 2L), .Label = c("0", "1", "2"), class = "factor"),
time = c(1600L, 1700L, 1800L, 1900L, 2000L, 2100L, 2200L,
2300L, 2400L, 2500L, 2600L, 2700L, 2800L, 2900L, 3000L, 3100L,
3200L, 3300L, 1600L, 1700L, 1800L, 1900L, 2000L, 2100L, 2200L,
2300L, 2400L, 2500L, 2600L, 2700L, 2800L, 2900L, 3000L, 3100L,
3200L, 3300L, 1600L, 1700L, 1800L, 1900L, 2000L, 2100L, 2200L,
2300L, 2400L, 2500L, 2600L, 2700L, 2800L, 2900L, 3000L, 3100L,
3200L, 3300L, 1600L, 1700L, 1800L, 1900L, 2000L, 2100L, 2200L,
2300L, 2400L, 2500L, 2600L, 2700L, 2800L, 2900L, 3000L, 3100L,
3200L, 3300L, 1600L, 1700L, 1800L, 1900L, 2000L, 2100L, 2200L,
2300L, 2400L, 2500L, 2600L, 2700L, 2800L, 2900L, 3000L, 3100L,
3200L, 3300L, 1600L, 1700L, 1800L, 1900L, 2000L, 2100L, 2200L,
2300L, 2400L, 2500L, 2600L, 2700L, 2800L, 2900L, 3000L, 3100L,
3200L, 3300L, 1600L, 1700L, 1800L, 1900L, 2000L, 2100L, 2200L,
2300L, 2400L, 2500L, 2600L, 2700L, 2800L, 2900L, 3000L, 3100L,
3200L)), row.names = c(NA, -125L), class = "data.frame")
> str(data)
'data.frame': 125 obs. of 6 variables:
$ X24hr : Factor w/ 18 levels "00:00:00","01:00:00",..: 11 12 13 14 15 16 17 18 1 2 ...
$ id : Factor w/ 7 levels "231","235","236",..: 6 6 6 6 6 6 6 6 6 6 ...
$ bark : num 29.273 10.546 0.364 2.546 0 ...
$ activity: Factor w/ 3 levels "high activity",..: 1 1 3 3 2 2 2 3 2 2 ...
$ binary : Factor w/ 3 levels "0","1","2": 3 3 2 2 1 1 1 2 1 1 ...
$ time : int 1600 1700 1800 1900 2000 2100 2200 2300 2400 2500 ...
谢谢!