【问题标题】:how to plot shaded region around a line using ggplot如何使用ggplot围绕一条线绘制阴影区域
【发布时间】:2012-11-20 08:48:27
【问题描述】:

我希望使用 ggplot 在一条线周围绘制一个阴影区域。这两个数据集没有直接关系,我正在对两者进行一些比较。基本上,我希望绘制下面代码产生的输出,以及在变量“lower_region_values”和“upper_region_values”中输入的具有最小和最大y轴值的阴影区域。

请帮我解决这个问题。

可重现的代码从这里开始

library("ggplot2")
plotName <- 'Comparison of latitudinal averages'

xlims <- c(65,80) # x axis extent

input_df <- structure(list(flux = c(0.08733913, 0.1015934, 
      0.1244135, 0.1390303,0.08417182,   0.02371609), 
      model = structure(c(1L, 1L, 1L,1L, 1L, 1L),.Label = 
      c("CASA_GFED_Optimized"),  class = "factor"), lat = 
      c(79, 77, 75, 73, 71, 69)), .Names = c("flux","model",  
      "lat"),row.names = c(NA, -6L), class = "data.frame")

 lower_region_values<-c(-0.002157493,-0.004465291,-0.376925852,
     -0.312737571,-0.327533801,  -0.299551351)

 upper_region_values<-c(1.943331e-06,1.758454e-04,3.183347e-01,
      2.442368e-01,1.206353e-  01,1.572531e-02)

 sd_input_lower$model <- factor(sd_input_lower$model, 
      levels=levels(input_df$model))
 sd_input_upper$model <- factor(sd_input_upper$model, 
      levels=levels(input_df$model))

需要使用两个向量中的值的阴影区域:lower_region_values 和 upper_region_values

 chart <- ggplot(input_df, aes(x=lat, group=model, 
      colour=model, fill=model)) +

# geom_ribbon(data = sd_input_upper, aes(ymin = -sd, ymax=sd), alpha=0.5) +

 geom_line(aes(y=flux), size=1.0) + 
      opts(title=plotName,legend.position='bottom') +
      scale_x_continuous('Latitude',limits=xlims) +   
      scale_y_continuous(expression(paste('Latitudinal average of NEE ', 
      (g~C~m^{-2}/day)))) +
      scale_colour_discrete(name='Model') +
      scale_fill_discrete(name='Model')

 print(chart) 

【问题讨论】:

  • 你能把这个问题改写成更多的问题吗?
  • 我需要绘制一条线,并在该线周围绘制一个区域。线值与区域的边界点没有任何关系。线的坐标在变量中给出:“input_df”,并且需要使用两个向量中的值绘制区域:“lower_region_values”和“upper_region_values”。
  • 如果您在使用 API 的多个部分时遇到问题,那么也许您应该提出多个问题。
  • 在这种情况下,你能帮忙画一条线和它周围的区域吗? line_data

标签: r ggplot2


【解决方案1】:

我会将用于着色的区域添加到同一个 data.frame 中,因为如果它们都在同一个对象中,则更容易进行 ggplots。我得到的图看起来有点滑稽,所以我认为我们需要确保当我们添加这些区域时,它们在它们的行中正确匹配 x 轴值。

input_df$lower_region_values<-c(-0.002157493,-0.004465291,-0.376925852,
     -0.312737571,-0.327533801,  -0.299551351)

input_df$upper_region_values<-c(1.943331e-06,1.758454e-04,3.183347e-01,
      2.442368e-01,1.206353e-01,1.572531e-02)

如果我正确理解你的目标,我基本上会做你正在做的事情,但稍微移动 aes():

chart<-
      ggplot(input_df, aes(x=lat, y=flux,group=model)) +
      geom_line(size=1.0,colour="red") + 
      opts(title=plotName,legend.position='bottom') +
      scale_x_continuous('Latitude',limits=xlims) +   
      scale_y_continuous(expression(paste('Latitudinal average of NEE ', 
      (g~C~m^{-2}/day)))) +
      geom_ribbon(aes(ymin = lower_region_values, 
      ymax=upper_region_values), colour="blue", fill="blue",alpha=0.5)

 print(chart) 

您似乎使用的是旧版本的ggplot2,所以如果您还在学习ggplot,我会更新并学习新的,它有一些重大变化。

【讨论】:

  • 效果很好,谢谢。只是一个简单的问题,如何更改线条和区域的颜色?
  • 现在,颜色和填充在 ggplot 函数的开头被分配以反映变量模型。您可以在此处省略该颜色并填充分配。还要省略 scale_colour_discrete() 和 scale_fill_discrete() ,而是在 geom_line 中放置一个 colour="blue" 或在 geom_ribbon 中放置一个 fill="green" 和 colour="green"。我已经编辑了上面的情节来做到这一点。
猜你喜欢
  • 1970-01-01
  • 2022-01-17
  • 1970-01-01
  • 2018-07-08
  • 2017-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多