【问题标题】:Highlighting values above a threshold using lattice in R xyplot在 R xyplot 中使用晶格突出显示阈值以上的值
【发布时间】:2014-08-12 23:36:56
【问题描述】:

您好,我正在尝试将 35 摄氏度以上的值突出显示为红点,并将月份和年份添加到 x 轴。我承认修改了this script。我是 R 新手,非常感谢您的帮助。此外,另一个包也可以。

这是数据和代码的示例。

Date    reference   shade   sun
2010-03-01  NA  50.6221 53.3561
2010-03-02  NA  38.1599 40.3847
2010-03-03  NA  34.8627 38.3919
2010-03-04  NA  33.4927 35.0268
2011-04-22  NA  38.2654 45.972
2011-04-23  NA  38.0786 37.2561
2013-07-18  13.7    13.7    13.7
2013-07-19  18.7    19.1    18.7
2014-05-26  20.6    20.7    23.7
2014-05-27  21.6    22.2    24.6
2014-05-28  17.1    17.7    22.1

require(lattice)


require(RColorBrewer)
colset <- brewer.pal(3, "Set1")

trellis.device(device="pdf", file="nestbox_temp_profiles.pdf", height=11, 
             width=7.5, color=TRUE)

lab.reference <- "Reference Area"
lab.shade <- "Shaded Nest-boxes"
lab.sun <- "Sun Exposed Nest-boxes"

my.strip <- function(which.given, which.panel, ...) {
  strip.labels <- c(lab.shade, lab.sun, lab.reference)
  panel.rect(0, 0, 1, 1, col="#ffe5cc", border=1)
  panel.text(x=0.5, y=0.5, adj=c(0.5, 0.55), cex=0.95,
             lab=strip.labels[which.panel[which.given]])
} #--Custom strip function:

Nestemp$Date <- strptime(Nestemp$Date, format="%Y-%m-%d")#--Define X axis date range:

xlim <- range(Nestemp$Date)

d <- seq(from=as.Date("2010-03-01"), to=as.Date("2015-03-01"), by=365/4) #--Define annual quarters for plot grid line markers:


col.raw <- "#377EB8"    
col.lm <- "red"


plot1<-xyplot(sun + shade + reference ~ Date, data=Nestemp, 
       scales=list(y="free", rot=0), 
       strip=my.strip, outer=TRUE, layout=c(1, 3, 1), ylab="Temperature (celsius)", xlab="",
       panel=function(x, y, groups=groups) {panel.grid(h=0, v=0) panel.abline(v=d, col="grey90") panel.xyplot(x, y, ..., type="p", col=col.raw, lwd=0.8) panel.abline(h=35, lty=2, col="red", lwd=1)},

key=list(text=list(c("Nestbox Temperature", "Threshold 35 degrees celsius")),
                title="Daily Maximum Nest box Temperature",
                col=c(col.raw, col.lm), lty=c(1, 2),
                columns=2, cex=0.95,
                lines=TRUE),)

plot1
dev.off()

【问题讨论】:

    标签: r lattice threshold


    【解决方案1】:

    这种情节很容易用ggplot2制作(本例使用示例数据集mtcars):

    library(ggplot2)
    ggplot(mtcars, aes(x = wt, y = mpg, color = mpg > 30)) + geom_point() + 
        scale_color_manual(values = c('red', 'blue'), breaks = c(TRUE, FALSE))
    

    【讨论】:

    • 干杯保罗,我已经尝试过了,但是失去了相对的时间序列,并且随着多年以及盒子类型的另一个因素(即阴影太阳或参考)变得混乱。我可以在几个小时内发布这个作为示例。
    • 从你想要做的事情的声音来看,ggplot2 应该能够很好地处理这个问题。发布一个示例,也许我们可以帮助您完善它。
    • 成功了,谢谢保罗。我使用 ggplot2 和格式化日期列与 df$Date
    【解决方案2】:

    我使用日期时间戳来格式化我的日期列。然后我将@PaulHiemstra 的建议与 ggplot2 一起使用。

    样本df

    Date    sseye   max month   season
    2009/10/6   Shaded Nest-boxes   29.9508 11  Non-breeding season
    2011/06/7   Sun Exposed Nest-boxes  38.1586 11  Non-breeding season
    2013/11/10  Shaded Nest-boxes   29.2015 11  Non-breeding season
    2014/05/9   Sun Exposed Nest-boxes  39.4666 11  Non-breeding season
    
    library(scales) # for units
    library(ggplot2)
    library(gridExtra)
    library(plyr)
    library(ffbase)
    
    
    Nestemp2$Date <- as.Date(Nestemp2$Date, format="%Y/%m/%d")
    plotnest=ggplot(Nestemp2, aes(x=Date, y=max, color = max > 35)) + 
      geom_point() + scale_color_manual(values = c('black', 'red')) +  
    

    接下来我想在特定日期和值处添加垂直和水平线

    geom_hline(yintercept=35, colour="red", linetype="dashed") + 
      geom_hline(yintercept=0, colour="black")+
      geom_vline(xintercept=as.numeric(as.Date("2010-04-01")), vjust=0.5, linetype=2) + 
      annotate("text", x=as.Date("2010-04-10"), y=25, label=" Not Breeding", angle=90, size=3,  vjust=-1, hjust=1) + 
      geom_vline(xintercept=as.numeric(as.Date("2010-10-01"), vjust=0.5), linetype=2) + 
      annotate("text", x=as.Date("2010-09-30"), y=25, label=" Breeding", angle=90, size=3,  vjust=-1, hjust=1) + 
    

    我的 df 列包含嵌套位置的多个因素(阳光、阴影和参考位置),所以我使用 facet wrap 制作了一个包含因素 ~ssey 的面板图(三个级别(阳光、阴影和参考)。我不能发图,但是看起来很甜。

    ylab("Temperature (c)") +
      facet_wrap( ~ sseye, ncol=1) +                  #this is what puts them in a grid
      scale_y_continuous(limits=c(0,60)) +    
    
      theme_bw() + theme(
      strip.text.x = element_text(size=16),           #sets the size of the title to each grid section as 10
      strip.background = element_blank(),             #removes the grey background in the title of each grid section
      axis.text.x = theme_text(size=16),               #sets the x axis tick text size to 8
      axis.text.y = theme_text(size=16),               #sets the y axis tick text size to 8
      axis.title.x=theme_blank(),                     #removes the x axis title
      axis.title.y=theme_text(size=16, vjust = .3),               #sets the y axis title to size 10
      axis.line=theme_segment(colour="black"),        #sets the axis lines
      panel.grid.minor = theme_blank(),               #removes minor grid lines
      panel.grid.major = theme_blank(),               #removes major grid lines
      panel.border=theme_blank(),                     #removes the border around the graph
      panel.background=theme_blank(),                 #removes the grey background of the plot
      legend.justification=c(10,10), 
      legend.position=c(10,10), # Position legend in top right
      legend.title=element_blank(),
      legend.text = theme_blank(),
      legend.title = theme_blank(),                   #removes the legend title
      legend.key = theme_blank())   
    
    plotnest
    

    【讨论】:

    • 感谢您发布您的解决方案,您能否在结果中添加一张图片?
    • 是的。在图像中,我在我想要的所有日期重复了 geom_vline() 代码。本质上是复制粘贴,仅更改代码中的年份。我没有提供这个,但第一个垂直行代码在那里。
    猜你喜欢
    • 2019-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-07
    • 1970-01-01
    • 2021-10-04
    相关资源
    最近更新 更多