小编Bry*_*yan的帖子

为data.table R保留lapply(.SD,...)中的列名

当将具有多个输出变量(例如,列表)的函数应用于data.table的子集时,我丢失了变量名称.有没有办法保留它们?

library(data.table)

foo <- function(x){
  list(mn = mean(x), sd = sd(x))
}

bar <- data.table(x=1:8, y=c("d","e","f","g"))

# column names "mn" and "sd" are replaced by "V1" and "V2"
bar[, sapply(.SD, foo), by = y, .SDcols="x"]

# column names "mn" and "sd" are retained
bar_split <- split(bar$x, bar$y)
t(sapply(bar_split, foo))
Run Code Online (Sandbox Code Playgroud)

r data.table

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

R Lattice xyplot中的精确轴刻度和标签

我正在创建许多图(每个图作为单独的图像),所有图必须具有相同的轴限制.当使用ylim时,刻度标记位于极端边缘处,并且对于极值而省略刻度标签.

library(lattice)
x=1:100
y=10+20*(runif(100))
xyplot(y~x)                 # Case 1 - automatic ylim
xyplot(y~x, ylim=c(10,20))  # Case 2 - specified ylim
Run Code Online (Sandbox Code Playgroud)

在案例1中,轴刻度和标签在(y = 10,15,20,25,30)处自动生成.标记了所有刻度线,并且绘图矩形中的极端刻度线(y = 10和y = 30)有一些垂直填充.

在案例2中,当我指定ylim值时,刻度标记在(y = 10,12,14,16,18,20)处生成,但标签仅出现(y = 12,14,16,18).勾选标签在极端情况下缺失.此外,绘图矩形中的极端刻度标记没有垂直填充.

有没有办法指定ylim,仍然有刻度线和标签出现类似于案例1?

更一般地说,在指定ylim时:
1.如何准确指定每个刻度标记的位置?
2.如何准确指定哪些刻度标记?

r lattice axis-labels

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

在格子面板中合并或叠加xyplots

尝试弄清楚如何在创建现有点阵面板后将点或数据系列添加到现有点阵面板.这与plot.points和/或更新功能有关吗?

# Initialize first plot
library(lattice)
a <- xyplot(Sepal.Length~Sepal.Width, groups=Species, data=iris
            , subset=Species %in% levels(Species)[1:2])
print(a) 

# Create second plot to overlay or merge with the first plot
b <- xyplot(Sepal.Length~Sepal.Width, groups=Species, data=iris
            , subset=Species %in% levels(Species)[3])

# Initial attempt at merging plots:
library(latticeExtra)
print(c(a,b)) # this displays the data in an adjacent second panel
print(c(a,b,layout=c(1,1))) # and this only shows series "b"
Run Code Online (Sandbox Code Playgroud)

注意:在此示例中,"a"和"b"两个图都来自原始的虹膜数据帧,但理想情况下,该解决方案可以使用不同的数据帧.

有任何想法吗?谢谢!
布赖恩

merge overlay r panel lattice

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

绘制格子xyplot中每组面板数据的第一个点

我正在尝试使用组和面板创建线图,这些组和面板将符号叠加在每个组的第一个值上.此尝试仅绘制第一组的第一个点,而不对其他两个组执行任何操作.

library(lattice)
foo <- data.frame(x=-100:100, y=sin(c(-100:100)*pi/4))
xyplot( y~x | y>0,  foo, groups=cut(x,3),
        panel = function(x, y, subscripts, ...) {
          panel.xyplot(x, y, subscripts=subscripts, type='l', ...)
          # almost but not quite:
          panel.superpose(x[1], y[1], subscripts=subscripts, cex=c(1,0), ...) 
        } )
Run Code Online (Sandbox Code Playgroud)

可以理解一般解决方案,以允许绘制每个组和面板内的特定点(例如,第一,中间和端点).

在此输入图像描述

r panel lattice

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

R晶格:如何调整图例标记线宽?

有没有人知道如何调整格子图中的标记线宽度,理想情况下使用auto.key?我需要独立于绘图中的线条粗细调整它,我需要非常薄.默认情况下,图例中的线条也非常薄,这使得很难通过颜色或线条样式区分不同的系列.中等厚度的矩形将是图例中理想的系列标记.

x = seq(1,360*10,1)
y = sin(x*pi/180)
df = data.frame(x=x, y=y, id="x")
p.xy <- xyplot(y~x, groups=id, data=df, type="l", lwd=.1
              ,auto.key=list(lines=T, points=F))
print(p.xy)
Run Code Online (Sandbox Code Playgroud)

谢谢!
布赖恩

r line legend width lattice

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

Format time series in lattice

I often deal with time series data with timescales of years, months, days, minutes, and seconds. When plotting, it would be convenient to easily change the way the time series axes are displayed, along the lines of a strptime command.

library(lattice)
t_ini = "2013-01-01"
ts = as.POSIXct(t_ini)+seq(0,60*60*24,60*60)
y = runif(length(ts))
# default plot with time series axis in M D h:m 
xyplot(y~ts) 
# this attempt at using format to display only hours on the x-axis does not work:
xyplot(y~ts, scales=list(x=list(labels=format("%H")))) …
Run Code Online (Sandbox Code Playgroud)

format r time-series lattice

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

标签 统计

r ×6

lattice ×5

panel ×2

axis-labels ×1

data.table ×1

format ×1

legend ×1

line ×1

merge ×1

overlay ×1

time-series ×1

width ×1