将日期标签添加到Barplot
我正在尝试将日期标签添加到我绘制的每个单独的酒吧中。数据看起来像这样:
structure(list(lakeID = c("WE1", "WE1", "WE1", "WE1", "WE1",
"WE1", "WE1", "WE1", "WE1", "WE1"), depth = c("hypolimnion",
"hypolimnion", "hypolimnion", "hypolimnion", "hypolimnion", "hypolimnion",
"hypolimnion", "hypolimnion", "hypolimnion", "hypolimnion"),
date = structure(c(1505779200, 1529366400, 1534723200, 1537142400,
1559088000, 1590624000, 1592352000, 1596153600, 1597881600,
1599696000), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
year = c(2017L, 2018L, 2018L, 2018L, 2019L, 2020L, 2020L,
2020L, 2020L, 2020L), BACI = c("Control", "Control", "Control",
"Control", "Impact", "Impact", "Impact", "Impact", "Impact",
"Impact"), N = c(2L, 2L, 1L, 1L, 7L, 56L, 10L, 58L, 3L, 2L
), meanlengths = c(1.44092870850016, 1.90413253399558, 2.08356379729054,
1.24630120343851, 6.39410976055929, 6.07576588787627, 5.63215818993585,
6.00116183368038, 5.42957068546558, 3.62096915399567), sd = c(0.261562571663927,
0.237860661401758, NA, NA, 0.454442531596122, 0.703831022329251,
0.465831548164042, 1.1448481521142, 1.19135665831808, 1.41507359493585
), SE = c(0.184952668128155, 0.1681928866547, NA, NA, 0.171763131967707,
0.0940533765621035, 0.14730886981608, 0.150325927020161,
0.687830087380796, 1.00060813485716), Upper2SE = c(1.80343593803135,
2.23379059183879, NA, NA, 6.73076549921599, 6.26011050593799,
5.92088357477536, 6.2958006506399, 6.77771765673194, 5.58216109831572
), Lower2SE = c(1.07842147896898, 1.57447447615237, NA, NA,
6.05745402190258, 5.89142126981455, 5.34343280509633, 5.70652301672087,
4.08142371419922, 1.65977720967563)), row.names = 22:31, class = "data.frame")
我试图用此代码绘制日期,但无法让它们显示:
xxx <- barplot(ChiSum$meanlength, axisnames=TRUE, beside = TRUE, names.arg=c(ChiSum$date), col=ifelse(ChiSum$BACI=="Control",'white','gray'), ylim = c(-0.01,8), ylab = "Mean body length",
xlab="Date",xaxt="n")
arrows(xxx,ChiSum$Lower2SE,xxx,ChiSum$Upper2SE, code=3, length=0.02, angle = 90,
col=ifelse(ChiSum$BACI=="Control",'black','black'))
axis(side=1,at=XXX[c(1,20)], labels=format(ChiSum$date))
I'm trying to add date labels to each individual bar that i've plotted. The data looks like this:
structure(list(lakeID = c("WE1", "WE1", "WE1", "WE1", "WE1",
"WE1", "WE1", "WE1", "WE1", "WE1"), depth = c("hypolimnion",
"hypolimnion", "hypolimnion", "hypolimnion", "hypolimnion", "hypolimnion",
"hypolimnion", "hypolimnion", "hypolimnion", "hypolimnion"),
date = structure(c(1505779200, 1529366400, 1534723200, 1537142400,
1559088000, 1590624000, 1592352000, 1596153600, 1597881600,
1599696000), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
year = c(2017L, 2018L, 2018L, 2018L, 2019L, 2020L, 2020L,
2020L, 2020L, 2020L), BACI = c("Control", "Control", "Control",
"Control", "Impact", "Impact", "Impact", "Impact", "Impact",
"Impact"), N = c(2L, 2L, 1L, 1L, 7L, 56L, 10L, 58L, 3L, 2L
), meanlengths = c(1.44092870850016, 1.90413253399558, 2.08356379729054,
1.24630120343851, 6.39410976055929, 6.07576588787627, 5.63215818993585,
6.00116183368038, 5.42957068546558, 3.62096915399567), sd = c(0.261562571663927,
0.237860661401758, NA, NA, 0.454442531596122, 0.703831022329251,
0.465831548164042, 1.1448481521142, 1.19135665831808, 1.41507359493585
), SE = c(0.184952668128155, 0.1681928866547, NA, NA, 0.171763131967707,
0.0940533765621035, 0.14730886981608, 0.150325927020161,
0.687830087380796, 1.00060813485716), Upper2SE = c(1.80343593803135,
2.23379059183879, NA, NA, 6.73076549921599, 6.26011050593799,
5.92088357477536, 6.2958006506399, 6.77771765673194, 5.58216109831572
), Lower2SE = c(1.07842147896898, 1.57447447615237, NA, NA,
6.05745402190258, 5.89142126981455, 5.34343280509633, 5.70652301672087,
4.08142371419922, 1.65977720967563)), row.names = 22:31, class = "data.frame")
I've attempted to plot the dates with this code but have not been able to get them to show:
xxx <- barplot(ChiSum$meanlength, axisnames=TRUE, beside = TRUE, names.arg=c(ChiSum$date), col=ifelse(ChiSum$BACI=="Control",'white','gray'), ylim = c(-0.01,8), ylab = "Mean body length",
xlab="Date",xaxt="n")
arrows(xxx,ChiSum$Lower2SE,xxx,ChiSum$Upper2SE, code=3, length=0.02, angle = 90,
col=ifelse(ChiSum$BACI=="Control",'black','black'))
axis(side=1,at=XXX[c(1,20)], labels=format(ChiSum$date))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
怎么样:
关键是要知道条的中点在哪里。默认设置是条栏是一个单元宽的,其空间(每个条之前)为0.2。因此,第一个条的中点为0.7。第二个栏的中点为0.7 + 1.2,第三个栏的中点为0.7 + 1.2*2等...这些中点应在上指定为
可以将日期用作标签。为了使它们绘制得很好,您可以使用
las = 2
将垂直于轴的标签转动。您也可以使用
ggplot2
:How about this:
The key is to know where the mid-points of the bars are. The default settings are that the bars are one-unit wide with a space (before every bar) of 0.2. So the mid-point of the first bar is at 0.7. The mid-point of the second bar is 0.7 + 1.2, the mid-point of the third is 0.7 + 1.2*2, etc... These mid-points should be specified as
at
and then you can use the dates as the labels. To make them plot nicely, you can uselas=2
to turn the labels perpendicular to the axis.You could also do it with
ggplot2
: