使用ggplot2的stat_smooth(),我很好奇如何调整生成的回归线的透明度.使用geom_points()或geom_line(),通常会为'alpha'设置一个值,表示透明度百分比.但是,使用stat_smooth(),alpha设置置信区间的透明度(在我的示例中,关闭 - se = FALSE).
我似乎无法找到一种方法使回归线的透明度低于1.
你的建议很棒.
示例代码
library(reshape2)
df <- data.frame(x = 1:300)
df$y1 <- 0.5*(1/df$x + 0.1*(df$x-1)/df$x + rnorm(300,0,0.015))
df$y2 <- 0.5*(1/df$x + 0.3*(df$x-1)/df$x + rnorm(300,0,0.015))
df$y3 <- 0.5*(1/df$x + 0.6*(df$x-1)/df$x + rnorm(300,0,0.015))
df <- melt(df, id = 1)
ggplot(df, aes(x=x, y=value, color=variable)) +
geom_point(size=2) +
stat_smooth(method = "lm", formula = y ~ 0 + I(1/x) + I((x-1)/x),
se = FALSE,
size = 1.5,
alpha = 0.5)
Run Code Online (Sandbox Code Playgroud)

好奇如何创建一个只包含文本信息的情节.这基本上是绘图窗口的"打印".
我到目前为止找到的最佳选择如下:
library(RGraphics)
library(gridExtra)
text = paste("\n The following is text that'll appear in a plot window.\n",
" As you can see, it's in the plot window",
" One might imagine useful informaiton here")
grid.arrange(splitTextGrob(text))
Run Code Online (Sandbox Code Playgroud)

但是,对于字体类型,大小,对齐等,人们无法控制(据我所知).
在与数据交互时,我发现dplyr库的select()函数是组织数据框列的一种很好的方法.
一个很好的用途,如果我碰巧使用具有许多列的df,我经常会发现自己将两个变量放在一起以便于比较.这样做时,我需要在之前或之后附加所有其他列.我发现这个matches(".")功能是一种非常方便的方法.
例如:
library(nycflights13)
library(dplyr)
# just have the five columns:
select(flights, carrier, tailnum, year, month, day)
# new order for all column:
select(flights, carrier, tailnum, year, month, day, matches("."))
# matches(".") attached all other columns to end of new data frame
Run Code Online (Sandbox Code Playgroud)
问题 - 如果有更好的方法,我很好奇吗?更灵活的意义上更好.
例如,有一个问题:是否有某种方法可以在新data.frame的开头或中间包含"所有其他"列?(请注意,由于它们是现有列名称的重复,select(flights, matches("."), year, month, day, )因此不会产生所需的结果,因为它们是matches(".")附加的所有列,year, month, day因此它们会被忽略.
我很好奇是否有人可以提出一种(更快)的方法来计算可变时间间隔(窗口)的滚动统计(滚动均值,中位数,百分位数等).
也就是说,假设有一个随机定时观察(即不是每日,或每周数据,观察只有时间戳,如在滴答数据中),并且假设您想查看中心和离散统计数据,您可以扩大并收紧计算这些统计数据的时间间隔.
我做了一个简单的for循环来做到这一点.但它显然运行得非常慢(实际上我认为我的循环仍在运行在我设置的一小部分数据样本上以测试其速度).我一直试图让ddply这样做 - 这对于每日统计数据来说似乎是不可能的 - 但我似乎无法摆脱它.
例:
样品设置:
df <- data.frame(Date = runif(1000,0,30))
df$Price <- I((df$Date)^0.5 * (rnorm(1000,30,4)))
df$Date <- as.Date(df$Date, origin = "1970-01-01")
Run Code Online (Sandbox Code Playgroud)
示例函数(运行非常慢,有很多观察结果
SummaryStats <- function(dataframe, interval){
# Returns daily simple summary stats,
# at varying intervals
# dataframe is the data frame in question, with Date and Price obs
# interval is the width of time to be treated as a day
firstDay <- min(dataframe$Date)
lastDay <- max(dataframe$Date)
result <- data.frame(Date = NULL,
Average = NULL, …Run Code Online (Sandbox Code Playgroud) 如何使标签文本的一部分在绘图标签中有删除线?
例如,要使y轴标签读作"标签中的删除线文字? "
ggplot(mpg, aes(x=displ, y=hwy))
+ geom_point()
+ ylab("~~strikethrough~~ text in a label?")
Run Code Online (Sandbox Code Playgroud)
我认为相当小的问题,找到一个解决方案也是微不足道的,但经过一段时间的寻找后却无济于事.
在R中工作,我有与下面类似结构的数据(代码块1).我正在寻找一个具有以下特征的新data.frame:
对于每个唯一的ID_1值,我想要有两个新列,一个包含(ID_2共享ID_1和方向== 1)的列表,另一列包含一个列表(ID_2共享ID_1和方向== 0) ),(见下一个代码块2)
数据集块1(初始):
ID_1 ID_2 Direction
100001 1 1
100001 11 1
100001 111 1
100001 1111 0
100001 11111 0
100001 111111 0
100002 2 1
100002 22 1
100002 222 0
100002 2222 0
100003 3 1
100003 33 1
100003 333 1
100003 3333 0
100003 33333 0
100003 333333 1
100004 4 1
100004 44 1
Run Code Online (Sandbox Code Playgroud)
转换成:
数据集块2(所需输出):
ID_1 ID_2_D1 ID_2_D0
100001 1,11,111 1111,11111,111111
100002 2,22 222,222
100003 3,33,333,333333 3333,33333
100004 4,44
Run Code Online (Sandbox Code Playgroud)
我有代码执行此操作,(采用子集子集的循环),但我在数百万个独特的"ID_1"上运行这个,这使得这非常耗费时间(小时,我告诉你!!). …
我有一个列表矩阵,其中一个"列"是一个列表(我意识到它是一个奇数数据集,但我发现它对其他操作很有用).列表的每个条目都是; (1)空(整数(0)),(2)整数,或(3)整数向量.
例如R对象"df",df $ ID是索引向量,df $ Basket_List列表.
ID <- c(1,2,3,4,5,6,7,8,9)
Basket_List <- list(integer(0),c(123,987),c(123,123),456,
c(456,123),456,c(123,987),c(987,123),987)
d.f <- data.frame(ID)
d.f$Basket_List <- Basket_List
Run Code Online (Sandbox Code Playgroud)
我想根据"Basket_List"是否包含某些值来创建一个新数据集,该数据集是初始数据集的子集.例如,df中所有行的子集,使得Bask_list具有"123"或"123"和"987" - 或其他更复杂的条件.
我已尝试过以下各种变化,但无济于事.
d.f2 <- subset(d.f, 123 %in% Basket_List)
d.f2 <- subset(d.f, 123 == any(Basket_List))
d.f2 <- d.f[which(123 %in% d.f$Basket_List,]
# should return the subset, with rows 2,3,5,7 & 8
Run Code Online (Sandbox Code Playgroud)
我的另一个问题是,我将在数百万行(它的事务数据)上运行此操作,所以我想尽可能地优化速度(我现在有一个复杂的for循环,但它需要太多时间).
如果您认为它可能有用,则数据也可能设置如下:
ID <- c(1,2,2,3,3,4,5,5,6,7,7,8,8,9)
Basket <- c(NA,123,987,123,123,456,456,123,456,123,987,987,123,987)
alt.d.f <- data.frame(ID,Basket)
Run Code Online (Sandbox Code Playgroud) 简短版本:我有一个比平常更复杂的合并操作,我想帮助优化dplyr或合并.我已经有了很多解决方案,但是这些解决方案在大型数据集上运行得非常慢,我很好奇R中是否存在更快的方法(或者在SQL或python中)
我有两个data.frames:
问题:商店ID是特定位置的唯一标识符,但商店位置可能会将所有权从一个时段更改为下一个时段(并且只是为了完整性,没有两个所有者可能同时拥有相同的商店).因此,当我合并商店级别信息时,我需要某种条件,将商店级信息合并到正确的时间段.
可重复的例子:
# asynchronous log.
# t for period.
# Store for store loc ID
# var1 just some variable.
set.seed(1)
df <- data.frame(
t = c(1,1,1,2,2,2,3,3,4,4,4),
Store = c(1,2,3,1,2,3,1,3,1,2,3),
var1 = runif(11,0,1)
)
# Store table
# You can see, lots of store location opening and closing,
# StateDate is when this business came into existence
# Store is the store id from df
# CloseDate is when this store when out of business
# …Run Code Online (Sandbox Code Playgroud)