自定义ggplot2在分类线图上标出错误区域 [英] Custom ggplot2 shaded error areas on categorical line plot

查看:214
本文介绍了自定义ggplot2在分类线图上标出错误区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b $我试图画出一条线,通过黄土平滑,但我想弄清楚如何包含由现有变量定义的阴影错误区域,但也是平滑的。 b

此代码创建示例数据:

  set.seed(12345)
data < - cbind( rep(A,100),rnorm(100,0,1))
data <-rbind(data,cbind(rep(B,100),rnorm(100,5,1)) )
data < - rbind(data,cbind(rep(C,100),rnorm(100,10,1)))
data < - rbind(data,cbind(rep (数据)
数据< - 数据帧(代表(1:100,4),数据)
数据<数据)
名称(数据)< - c(num,category,value)
data $ num< - as.numeric(data $ num)
data $ value < - as.numeric(data $ value)
data $ upper < - data $ value + 0.20
data $ lower < - data $ value-0.30

绘制下面的数据,这就是我得到的结果:

  ggplot(data,aes(x = num,y = value,color = category))+ 
stat_smooth(method =loe ss,se = F)



我想要的是一个看起来像下面的情节,除了上面的和阴影区域的下界由生成数据中上和下变量的平滑线限定。 $ b



任何帮助都将不胜感激。

解决方案

以下是一种添加 upper 和 lower 的平滑版本的方法。 code>。我们将LOESS预测添加到数据框中,然后将 upper lower 添加到数据框中,然后使用 geom_ribbon 。如果这可以在 ggplot 的调用中完成,那将更加优雅。这可能是通过向 stat_summary 提供一个特殊用途函数,并希望其他人使用该方法发布答案。

 #扩大上限值和下限值的比例,以便在
data $ upper = data $ value + 10 $中显示差异
# b $ b data $ lower = data $ value - 10

#按类别和数量排序数据
data = data [order(data $ category,data $ num),]

#为上和下
#的值创建LOESS预测,并将它们添加到数据框中。我相信有更好的方法来做到这一点,
#但我的dplyr和tapply的尝试都失败了,所以我使用了下面的笨重的
#方法。
data $ upperLoess = unlist(lapply(LETTERS [1:4],
function(x)predict(loess(data $ upper [data $ category == x]〜
data $ num [data $ category == x]))))
data $ lowerLoess = unlist(lapply(LETTERS [1:4],
function(x)predict(loess(data $ lower [data $ category == x]〜
data $ num [data $ category == x])))

#使用geom_ribbon添加由LOESS预测包围的预测区域,用于
#上下限
ggplot(data,aes(num,value,color = category,fill = category))+
geom_smooth(method =loess,se = FALSE)+
geom_ribbon (aes(x = num,y = value,ymax = upperLoess,ymin = lowerLoess),
alpha = 0.2)

结果如下:




I'm trying to plot a line, smoothed by loess, but I'm trying to figure out how to include shaded error areas defined by existing variables, but also smoothed.

This code creates example data:

set.seed(12345)
data <- cbind(rep("A", 100), rnorm(100, 0, 1))
data <- rbind(data, cbind(rep("B", 100), rnorm(100, 5, 1)))
data <- rbind(data, cbind(rep("C", 100), rnorm(100, 10, 1)))
data <- rbind(data, cbind(rep("D", 100), rnorm(100, 15, 1)))
data <- cbind(rep(1:100, 4), data)
data <- data.frame(data)
names(data) <- c("num", "category", "value")
data$num <- as.numeric(data$num)
data$value <- as.numeric(data$value)
data$upper <- data$value+0.20
data$lower <- data$value-0.30

Plotting the data below, this is what I get:

ggplot(data, aes(x=num, y=value, colour=category)) +
  stat_smooth(method="loess", se=F)

What I'd like is a plot that looks like the following, except with the upper and lower bounds of the shaded areas being bounded by smoothed lines of the "upper" and "lower" variables in the generated data.

Any help would be greatly appreciated.

解决方案

Here's one way to add smoothed versions of upper and lower. We'll add LOESS predictions for upper and lower to the data frame and then plot those using geom_ribbon. It would be more elegant if this could all be done within the call to ggplot. That's probably possible by feeding a special-purpose function to stat_summary, and hopefully someone else will post an answer using that approach.

# Expand the scale of the upper and lower values so that the difference
# is visible in the plot
data$upper = data$value + 10
data$lower = data$value - 10

# Order data by category and num
data = data[order(data$category, data$num),]

# Create LOESS predictions for the values of upper and lower 
# and add them to the data frame. I'm sure there's a better way to do this,
# but my attempts with dplyr and tapply both failed, so I've resorted to the clunky 
# method below.
data$upperLoess = unlist(lapply(LETTERS[1:4], 
                  function(x) predict(loess(data$upper[data$category==x] ~ 
                                                  data$num[data$category==x]))))
data$lowerLoess = unlist(lapply(LETTERS[1:4], 
                  function(x) predict(loess(data$lower[data$category==x] ~ 
                                                  data$num[data$category==x]))))

# Use geom_ribbon to add a prediction band bounded by the LOESS predictions for 
# upper and lower
ggplot(data, aes(num, value, colour=category, fill=category)) +
  geom_smooth(method="loess", se=FALSE) +
  geom_ribbon(aes(x=num, y=value, ymax=upperLoess, ymin=lowerLoess), 
              alpha=0.2)

And here's the result:

这篇关于自定义ggplot2在分类线图上标出错误区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆