小编use*_*059的帖子

多边形内的r sf包质心

我需要向多边形添加标签,并且通常使用质心,但是质心不会落在多边形内。我发现这个问题是在SpatialPolygon中/内部计算质心,但我使用的是sf包。

以下是玩具数据

rm(list = ls(all = TRUE)) #start with empty workspace

library(sf)
library(tidyverse)
library(ggrepel)

pol <- st_polygon(list(rbind(c(144, 655),c(115, 666)
                         ,c(97, 660),c(86, 640)
                         ,c(83, 610),c(97, 583)
                         ,c(154, 578),c(140, 560)
                         ,c(72, 566),c(59, 600)
                         ,c(65, 634),c(86, 678)
                         ,c(145, 678),c(144, 655)))) %>%
  st_sfc()

a = data.frame(NAME = "A")
st_geometry(a) = pol

a <- a  %>% 
  mutate(lon = map_dbl(geometry, ~st_centroid(.x)[[1]]),
     lat = map_dbl(geometry, ~st_centroid(.x)[[2]]))

ggplot() +
  geom_sf(data = a, fill = "orange") +
  geom_label_repel(data = a, aes(x = lon, y = lat, label …
Run Code Online (Sandbox Code Playgroud)

r centroid r-sf

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

R从多种格式转换为日期

我需要将多种格式的日期字符串转换为有效日期.

例如

dates <- c("01-01-2017","02-01-2017","12-01-2016","20160901","20161001", "20161101")

> as.Date(dates, format=c("%m-%d-%Y","%Y%m%d"))
[1] "2017-01-01" NA           "2016-12-01" "2016-09-01" NA           "2016-11-01"
Run Code Online (Sandbox Code Playgroud)

两个日期显示为NA

r as.date

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

r mutate_each函数已弃用

我使用了tidyverse包中的mutate_each函数,并且收到一条消息,表明该函数已被弃用。我想使用其他不建议更改字段类型的功能。

以下是我目前如何使用mutate_each的可复制示例。

library(tidyverse)

set.seed(123)

df <- data.frame(FirstName = sample(LETTERS[1:2],size=3, replace=TRUE),
             LastName = sample(LETTERS[3:6],size=3, replace=TRUE),
             StartDate =  c("1/31/2000","2/1/2000","3/1/2000"),
             EndDate =   c("1/31/2010","2/10/2011","3/1/2016"),
             stringsAsFactors = FALSE)
str(df)

df %>% mutate_each(funs(as.factor(as.character(.))), 
               c(FirstName:LastName)) %>% 
   mutate_each(funs(as.Date(., format = "%m/%d/%Y",
                       origin = "1899-12-30")), 
          c(StartDate:EndDate))

`mutate_each()` is deprecated.
Use `mutate_all()`, `mutate_at()` or `mutate_if()` instead.
To map `funs` over a selection of variables, use `mutate_at()`...etc
Run Code Online (Sandbox Code Playgroud)

我玩过mutate_all(),mutate_at()和mutate_if(),但是没有运气。

each r dplyr tidyverse mutate

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

使用R中的循环重复值

我正在处理类似于下面的汇总数据集,我需要扩展它,使其看起来像第二个数据集.

df <- data.frame(CustName = letters[1:3],
Years = c(4,2,1), 
MinYear = c(1995,1992,1998),
stringsAsFactors = F)

df
Run Code Online (Sandbox Code Playgroud)

我试过使用循环,但我没有成功

所需的输出是这样的

dfResult <- data.frame(CustName = rep(letters[1:3], c(4,2,1)),
Years = c(1995:1998, 1992:1993, 1998), stringsAsFactors = F)

dfResult
Run Code Online (Sandbox Code Playgroud)

loops r repeat

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

堆积条形图上的R ggplot标签

我有需要放入堆栈条形图的数据,但是当我添加计数的标签时,一些标签在类别之上,有些在类别之下。我尝试修改 geom_text 函数的位置参数无济于事。

下面是一个可重现的示例,显示了该类别上方“Under”类别座位的标签以及酒吧内“Over”类别座位的标签。

library(tidyverse)

data.frame(AgeGroup = sample(c(rep("Over",10),"Under"), 6000, replace = TRUE),
DueDate = sample( 
         seq( as.Date("2015-01-01"), 
              as.Date("2015-06-30"), by="1 month") ,  
         6000,replace = TRUE),
             stringsAsFactors = TRUE) %>%
group_by(AgeGroup,DueDate) %>%
  tally() %>% ungroup %>% 
  ggplot() +
  geom_bar(aes(x=DueDate, y=n, fill = AgeGroup),stat = "identity") +
  geom_text(aes(x=DueDate, y=n
            ,label = prettyNum(n,big.mark = ","))
        , vjust = 0,  size = 2) +
  scale_y_continuous(labels = scales::comma) +
  theme_bw() +
  labs(title="Where are the labels")
Run Code Online (Sandbox Code Playgroud)

下面是输出图表。 在此处输入图片说明

r ggplot2 geom-text

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

r ggplot2 facet_grid如何在图表顶部和边框之间添加空间

有没有一种方法可以使用ggplot的facet_grid在图表顶部的标签和图表的边距之间添加空间。以下是可重现的示例。

library(dplyr)
library(ggplot2)
Titanic %>% as.data.frame() %>%
filter(Survived == "Yes") %>% 
mutate(FreqSurvived = ifelse(Freq > 100, Freq*1e+04,Freq)) %>%
ggplot( aes(x = Age, y = FreqSurvived, fill = Sex)) +
geom_bar(stat = "identity", position = "dodge") +
facet_grid(Class ~ ., scales = "free") +
theme_bw() +
geom_text(aes(label = prettyNum(FreqSurvived,big.mark = ",")), vjust = 0, position = position_dodge(0.9), size = 2)
Run Code Online (Sandbox Code Playgroud)

生成的图表在图的边框旁边有数字标签。

图片

r ggplot2 facet-grid

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

标签 统计

r ×6

ggplot2 ×2

as.date ×1

centroid ×1

dplyr ×1

each ×1

facet-grid ×1

geom-text ×1

loops ×1

mutate ×1

r-sf ×1

repeat ×1

tidyverse ×1