小编Rom*_*man的帖子

dplyr对行的子集进行mutate/replace

我正在尝试基于dplyr的工作流程(而不是主要使用我习惯的data.table),而且我遇到了一个问题,我无法找到一个等效的dplyr解决方案.我经常遇到需要根据单个条件有条件地更新/替换多个列的场景.这是一些示例代码,我的data.table解决方案:

library(data.table)

# Create some sample data
set.seed(1)
dt <- data.table(site = sample(1:6, 50, replace=T),
                 space = sample(1:4, 50, replace=T),
                 measure = sample(c('cfl', 'led', 'linear', 'exit'), 50, 
                               replace=T),
                 qty = round(runif(50) * 30),
                 qty.exit = 0,
                 delta.watts = sample(10.5:100.5, 50, replace=T),
                 cf = runif(50))

# Replace the values of several columns for rows where measure is "exit"
dt <- dt[measure == 'exit', 
         `:=`(qty.exit = qty,
              cf = 0,
              delta.watts = 13)]
Run Code Online (Sandbox Code Playgroud)

是否有一个简单的dplyr解决方案来解决同样的问题?我想避免使用ifelse,因为我不想多次输入条件 - 这是一个简化的例子,但有时很多基于单个条件的赋值.

在此先感谢您的帮助!

r dplyr data.table

65
推荐指数
9
解决办法
3万
查看次数

通过gganimate和ggforce移动的facet缩放的动画情节?

在此输入图像描述

目标

我想放大这些年来的GDP Europe.幻象ggforce::facet_zoom允许非常容易地将其用于静态图(即,特定年份).

然而,移动尺度证明比预期更难.gganimate似乎从第一帧(year == 1952)获取x轴限制并一直持续到动画结束.不幸的是,这个相关但代码过时的问题没有得出答案.既不能+ coord_cartesian(xlim = c(from, to)),也facet_zoom(xlim = c(from, to))不能影响facet_zoom超出静态极限的窗口.

  • 有没有办法让gganimate'重新计算' facet_zoom每一帧的比例?

理想的结果

第一帧

2

最后一帧

3

目前的代码
library(gapminder)
library(ggplot2)
library(gganimate)
library(ggforce)
p <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, color = continent)) +
    geom_point() + scale_x_log10() +
    facet_zoom(x = continent == "Europe") +
    labs(title = "{frame_time}") +
    transition_time(year) 

animate(p, nframes = 30)
Run Code Online (Sandbox Code Playgroud)

r ggplot2 gganimate ggforce

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

get_map没有传递API密钥(HTTP状态为'403 Forbidden')

我一直在R 中的get_map()函数(ggmap库)中遇到这个问题.

我的代码运行时无需source = "google"为几个月指定API密钥(for ).但是,代码在几周前停止了工作.我知道谷歌已经强制要求API密钥(或者他们可能在没有我用尽的api密钥的情况下允许一定数量的呼叫).

但是,即使在指定API密钥(从Google Cloud Platform获得)后,我的代码仍然以相同的方式运行.我甚至联系了谷歌云支持,但是他们说API密钥本身没有任何问题,他们可以在最后调用地图.

我怀疑该get_map()功能api_key在从谷歌调用地图时没有通过.任何指向解决方案的指针都将受到赞赏.

下面是可重现的代码(失败).

library(ggmap)

lat <- c(4,41)  # India lat boundaries
lon <- c(68,99) # India long boundaries
center = c(mean(lat), mean(lon))

map <- get_map(location = c(lon = mean(lon), 
                            lat = mean(lat)),
               api_key = <my api key>,
               zoom = 6,
               maptype = "terrain",
               source = "google",
               messaging = TRUE
)
Run Code Online (Sandbox Code Playgroud)

以下是R中的错误消息(注意API密钥未通过)

trying URL 'http://maps.googleapis.com/maps/api/staticmap?center=22.5,83.5&zoom=6&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false'
Error in download.file(url, destfile = tmp, quiet = …
Run Code Online (Sandbox Code Playgroud)

google-maps r google-maps-api-3 ggmap

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

使用API​​密钥在ggmap中映射时出错(403 Forbidden)

我通常用来ggmap在简单的城市地图上绘制点.今天这样做时,我遇到了一个新的错误,禁止我使用该功能get_map()

        #get API key @ https://developers.google.com/places/web-service/get-api-key
    key<-"AIzaSyCYgKKt2fn7Crt-V6Hnc5aw5lSfy7XLQ-Y"
    register_google(key = key)

atw<- get_map(location=c(-88.68,42.14), zoom=10, scale=2)
Run Code Online (Sandbox Code Playgroud)

我不确定问题出在哪里.我尝试了一个新的API密钥,但没有运气.有什么输入?

错误如下:

无法打开网址' https://maps.googleapis.com/maps/api/staticmap?center=42.14,-88.68&zoom=10&size=640x640&scale=2&maptype=terrain&language=en-EN&key=AIzaSyCYgKKt2fn7Crt-V6Hnc5aw5lSfy7XLQ-Y':HTTP状态为download.file中的'403 Forbidden'错误(url,destfile = destfile,quiet =!messaging,mode ="wb"):无法打开网址' https://maps.googleapis.com/maps/api/staticmap?center= 42.14,-88.68&zoom = 10&size = 640x640&scale = 2&maptype = terrain&language = en-EN&key = AIzaSyCYgKKt2fn7Crt-V6Hnc5aw5lSfy7XLQ-Y '

gis r ggplot2 ggmap

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

聚合打印不正确的列数

我使用聚合函数来获得因子级别的范围.我正在尝试重命名列,但聚合函数的输出没有min和max作为单独的列.

# example data
size_cor <- data.frame(SpCode = rep(c(200, 400, 401), 3),
                       Length = c(45, 23, 56, 89, 52, 85, 56, 45, 78))

# aggregate function
spcode_range <- with(size_cor, aggregate(Length, list(SpCode), FUN = range))
Run Code Online (Sandbox Code Playgroud)

输出:

spcode_range 

  Group.1 x.1 x.2
1     200  45  89
2     400  23  52
3     401  56  85
Run Code Online (Sandbox Code Playgroud)

数据结构:

str(spcode_range)

'data.frame':   3 obs. of  2 variables:
 $ Group.1: num  200 400 401
 $ x      : num [1:3, 1:2] 45 23 56 89 52 85

dim(spcode_range)
[1] 3 …
Run Code Online (Sandbox Code Playgroud)

aggregate r dataframe

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

ggplot2 中带有 geom_smooth 的点密度

目标

我的数据由许多点组成,这些点太多而无法表示为散点图。我想绘制一个点密度。

具体来说,我想知道是否有办法使用 ggplot2 获得与此类似的内容:

在此处输入图片说明

当前代码

pEPNSABobs1<-ggplot(dataE, aes(comp1,mod1obs, group=1))+ 
  geom_smooth(aes(color="chartreuse"), se=F, linetype="longdash", size=1)+ 
  stat_smooth(data=dataE, fill="chartreuse", color="chartreuse4", linetype="blank")+

  geom_smooth(data=dataS, aes(comp1,mod1obs, group=1, color="lightpink"), se=F, linetype="longdash", size=1)+ 
  stat_smooth(data=dataS, fill="lightpink",color="lightpink4", linetype="blank")+

  scale_colour_manual(name = 'Legend', 
                      values =c('lightpink'='lightpink4','chartreuse'='chartreuse4'), labels = c('PIM','ABB'))+
  scale_size_area() + 
  xlab("CDD0.year") +
  ylab(expression(f[CDD0.year.obs]))+
  labs(title='(A)')+ 
  theme(plot.title = element_text(hjust = 0, vjust=1),axis.title = element_text(size = rel(1.2)), title=element_text(size = rel(1.2)),axis.text=element_text(size = rel(1.2)),
        legend.text=element_text(size = rel(1.2)))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

数据

DataE <- structure(list(comp1 = c(1338.2461, 1721.8119, 1878.2578, 1781.8827, 1813.2432, 1711.5277, 2033.0855, 1636.394, 1580.0748, 1834.4927, 2150.4177, 1790.7859, 1980.2718, 1610.0624, 2064.5809, 2002.82, …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

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

Plot line and bar graph (with secondary axis for line graph) using ggplot

Problem

I have just started R two days back. I have gone through some basic R tutorials and I am able to plot two dimensional data. I pull data from an Oracle database. Now, I am having problems when I try to merge two graph types (Line and Bar) using secondary axis.

I have no problem, plotting this data on Excel. Following is the plot:

在此处输入图片说明

I am unable to plot it on R. I searched some related examples but I …

r ggplot2

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