小编use*_*377的帖子

ggplot中有六种以上的形状

我想使用离散颜色绘制具有六组以上数据的不同形状的线条.问题是1)为线条颜色和形状生成了不同的图例,但应该只有一个带有线条颜色和形状的图例,2)在修正线条颜色图例的标题时,颜色会消失.

t=seq(0,360,20)
for (ip in seq(0,10)) {
  if (ip==0) {
    df<-data.frame(t=t,y=sin(t*pi/180)+ip/2,sn=ip+100)
  } else {
    tdf<-data.frame(t=t,y=sin(t*pi/180)+ip/2,sn=ip+100)
    df<-rbind(df,tdf)

  }
}
head(df)

# No plot
# Error: A continuous variable can not be mapped to shape
gp <- ggplot(df,aes(x=t,y=y,group=sn,color=sn,shape=sn))
gp <- gp + labs(title = "Demo more than 6 shapes", x="Theat (deg)", y="Magnitude")
gp <- gp + geom_line() + geom_point()
print(gp)

# No plot
# Error: A continuous variable can not be mapped to shape (doesn't like integers)
gp <- ggplot(df,aes(x=t,y=y,group=sn,color=sn,shape=as.integer(sn)))
gp <- …
Run Code Online (Sandbox Code Playgroud)

r colors shape line ggplot2

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

R:运行debugSource时fBody [[i]]出错

我有一个大约1400行的R脚本.我最近开始收到以下错误消息.我可以通过突出显示并使用ctrl-R来运行脚本,但我无法在调试模式下运行脚本.有关如何找到错误的任何建议?

> debugSource('~/working/R/h60_maintenance/do_mega_analysis.R')
Error in fBody[[i]] : subscript out of bounds
> options(error=recover)
> debugSource('~/working/R/h60_maintenance/do_mega_analysis.R')
Error in fBody[[i]] : subscript out of bounds
Enter a frame number, or 0 to exit   
 1: debugSource("~/working/R/h60_maintenance/do_mega_analysis.R")
 2: (function (fileName, encoding, breaklines) 
{
    env <- new.env(parent = emptyenv())
    env$fun <- 
 3: suppressWarnings(.rs.setFunctionBreakpoints("fun", env, lapply(steps, function(step) {
    step$at
}
 4: withCallingHandlers(expr, warning = function(w) invokeRestart("muffleWarning"))
 5: .rs.setFunctionBreakpoints("fun", env, lapply(steps, function(step) {
    step$at
}))
 6: suppressMessages(trace(what = functionName, where = envir, at = lapply(strsplit(as.character(steps
 7: withCallingHandlers(expr, …
Run Code Online (Sandbox Code Playgroud)

debugging error-handling r

9
推荐指数
2
解决办法
5939
查看次数

按组滚动/移动平均值

如何使用分组数据生成滚动平均值.这是数据

set.seed(31)
dd<-matrix(sample(seq(1:20),30,replace=TRUE),ncol=3)
Run Code Online (Sandbox Code Playgroud)

添加组标识符,并按组标识符排序

du<-sample(seq(1:4),10,replace=TRUE)
d<-cbind(du,dd)
d<-d[order(d[,1]),]
Run Code Online (Sandbox Code Playgroud)

这给出了滚动平均值但忽略了组bounderis

d_roll_mean <- apply(d[,2:4], 2, 
                   function(x) {
                     rollapply(zoo(x), 3, mean, partial=TRUE, align='right')
                   }
)
Run Code Online (Sandbox Code Playgroud)

这给出了下面的结果

# cbind(d,d_roll_mean)
# [1,]  1  3  3 12  3.000000  3.000000 12.000000
# [2,]  2 10 13  8  6.500000  8.000000 10.000000
# [3,]  2 17  2 17 10.000000  6.000000 12.333333
# [4,]  3 14  6  3 13.666667  7.000000  9.333333
# [5,]  3  6 20  1 12.333333  9.333333  7.000000
# [6,]  3  1 16 19  7.000000 14.000000  7.666667
# [7,] …
Run Code Online (Sandbox Code Playgroud)

grouping r moving-average

7
推荐指数
2
解决办法
567
查看次数

创建/审阅后放大ggplot

我正在以批处理模式制作地块.在查看图表时,放大感兴趣的serval区域会很有用.有没有办法在绘图后缩放/重新缩放轴,然后将其恢复到原始轴范围?

在收到反馈和评论之后回答......

set.seed(5)
gplist<-list()
for (i in seq(1,29)) {
  mod_evt = paste("plot",i)
  df <- data.frame(x=runif(10), y=runif(10))
  gp <- ggplot(df,aes(x=x,y=y)) + geom_line() + geom_point() +
    labs(title = mod_evt, x="X", y="Y") 
  print(gp)
  gplist[[i]] <- gp
}
Run Code Online (Sandbox Code Playgroud)

我想放大图27中x = 0.52附近的那个倾角

print(gplist[[27]] +  coord_cartesian(xlim= c(.5,.6)))
Run Code Online (Sandbox Code Playgroud)

这将再现x轴放大在.5和.6之间的图.

r zoom ggplot2

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

标签 统计

r ×4

ggplot2 ×2

colors ×1

debugging ×1

error-handling ×1

grouping ×1

line ×1

moving-average ×1

shape ×1

zoom ×1