小编vin*_*nzu的帖子

在R中计算星期五或星期一的月数

我想要一个计算每月特定天数的函数.

即.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)

什么是更好的方法来做到这一点?

r lubridate

9
推荐指数
1
解决办法
2020
查看次数

如何在主图区域之外的 ggplot2 中添加多个标题

我想为页脚添加两个标题。但 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)

r ggplot2

8
推荐指数
1
解决办法
6721
查看次数

更改ggplot2中堆积填充列的顺序

我想更改堆积条形图的顺序。例如,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)

在此处输入图片说明

r ggplot2

3
推荐指数
1
解决办法
3256
查看次数

使用带有R中的地图数据的ggplot创建动画

我有一个州的每个县的每周数据.我想创建一个循环遍历每周的动画,显示绘制到地图上的数据,颜色表示强度和/或上周的变化.

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)

animation r ggplot2

1
推荐指数
1
解决办法
4156
查看次数

标签 统计

r ×4

ggplot2 ×3

animation ×1

lubridate ×1