我正在尝试编辑Word文档并插入使用某些图形编辑的日历。我该如何执行以下操作:
-
用颜色填充日历日?
-
在选定的一周左右画一个盒子?
以下是使用ThexchDemos软件包构建光泽的代码。我愿意使用其他包装(例如GGPLOT2)(引用:使用GGPLOT2创建每月/每年的日历图像)。我也包括一个例子:
#using TeachingDemos-----------------------------------------
library(TeachingDemos)
red = "flagged"
yellow = "draw box naround nweek"
#calendar and variables
day = as.Date("2017-09-01")
year = as.numeric(substr(day,1,4))
month = months(day)
month_n= as.numeric(substr(day,6,7))
cal(month
,year)
setday = cal(month_n,year)
#mark days 8 and 9 as flagged
for( i in 8:9) {
setday(i)
text(0.6,0.6, red)
}
#highlight 17:23
for( i in 17:23) {
setday(i)
text(0.6,0.6, yellow)
}
下面使用GGPLOT
library(ggplot2)
input <- as.Date("2017-09-11") # input date
st <- as.Date(cut(input, "month")) # calculate start of month
dates31 <- st + 0:30 # st and next 30 dates (31 in all)
dates <- dates31[months(dates31) == months(st)] # keep only dates in st's month
week.ch <- format(dates, "%U") # week numbers
mydf <- data.frame(
day = as.numeric(format(dates, "%d")),
week.f = factor(week.ch, rev(unique(week.ch))),
dow = factor(format(dates, "%a"), c("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"))
)
ggplot(mydf, aes(x = dow, y = week.f)) +
geom_tile(colour = "black", fill = "white") +
geom_text(label = mydf$day, size = 4) +
scale_x_discrete(expand = c(0,0)) +
theme(axis.ticks = element_blank()) +
theme(axis.title = element_blank()) +
theme(panel.background = element_rect(fill = "transparent"))+
theme(legend.position = "none")
这是Deachertydemos cal
选项的一些选项。
要用颜色填充一天,您可以使用rect
功能:
for( i in 8:9) {
setday(i)
rect(-0.04,-0.04,1.04,1.04, col="#ff000066")
}
我使用了透明的红色,以使一天数量仍然显示,但是您也可以填充纯色然后重新绘制数字:
for( i in 8:9) {
setday(i)
rect(-0.04,-0.04,1.04,1.04, col="red")
text(0,1, i, adj=c(0,1))
}
您可能还想查看rect
的帮助页面,以查看如何关闭或更改边框的颜色或设置其他选项。使用0和1代替-0.04和1.04将主要填充矩形并产生不同的效果。
在一个星期左右绘制矩形(或其他组)采取几个步骤:
# find left coordinate in device units
setday(17)
left <- grconvertX(-0.04, from='user', to='ndc')
# switch to last day of week and find left value in user coords
setday(23)
newleft <- grconvertX(left, from='ndc', to='user')
# turn off clipping
par(xpd=NA)
# draw rectangle
rect(newleft, -0.04, 1.04, 1.04, border='blue', lwd=3)
如果要将矩形进一步增加,则可以增加数值。