您好我正在尝试将高于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()
答案 0 :(得分: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))
答案 1 :(得分:1)
我使用日期时间戳来格式化我的日期列。然后我使用了@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')) +
接下来,我想在particalr日期和值
添加垂直和水平线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