我想要一个计算每月特定天数的函数.
即.2013年11月 - > 5周五..而13年12月将返回4周五..
有一个优雅的功能会返回这个?
library(lubridate)
num_days <- function(date){
x <- as.Date(date)
start = floor_date(x, "month")
count = days_in_month(x)
d = wday(start)
sol = ifelse(d > 4, 5, 4) #estimate that is the first day of the month is after Thu or Fri then the week will have 5 Fridays
sol
}
num_days("2013-08-01")
num_days(today())
Run Code Online (Sandbox Code Playgroud)
什么是更好的方法来做到这一点?
我想为页脚添加两个标题。但 ggplot 似乎只需要 1。是否有解决方法可以将注释或 geom_text 添加到左下角和右下角。
library(ggplot2)
p <- ggplot(mtcars, aes(x=wt, y = mpg)) +
geom_point()
p + labs(caption = "left footer") + theme(plot.caption=element_text(hjust=0))
p + labs(caption = "right footer") + theme(plot.caption=element_text(hjust=1))
p + labs(caption = "right footer") + theme(plot.caption=element_text(hjust=1)) +
labs(caption = "left footer") + theme(plot.caption=element_text(hjust=0))
Run Code Online (Sandbox Code Playgroud) 我想更改堆积条形图的顺序。例如,mpg我要订购c("4", "r", "f")
改变因素水平的唯一方法是吗?
library(ggplot2)
library(dplyr)
s <- ggplot(mpg, aes(fl, fill=drv)) + geom_bar(position="stack")
s
Run Code Online (Sandbox Code Playgroud)
我有一个州的每个县的每周数据.我想创建一个循环遍历每周的动画,显示绘制到地图上的数据,颜色表示强度和/或上周的变化.
library(ggplot2); library(animation); library(maps); library(plyr)
county <- map_data("county")
wy <- county[county$region =="wyoming",]
l = length((wy$subregion))
#Add random variales
wy <- mutate(wy, a = runif(length(region)),
b = runif(length(region)),
c= runif(length(region)))
test <- function(j){
ggplot(wy, aes(long, lat, group = group))+
geom_path() +
geom_polygon(aes_string(fill=j))
}
test("c")
test("b")
v = c("a","b","c"))
oopt <- animation::ani.options(interval = 0.1)
FUN2 <- function() {
lapply(v, function(i) {
test(i)
animation::ani.pause()
})
}
FUN2()
saveHTML(FUN2(), autoplay = FALSE, loop = FALSE, verbose = FALSE, outdir = "images/animate/new",
single.opts = "'controls': …Run Code Online (Sandbox Code Playgroud)