将日期标签添加到Barplot

发布于 2025-01-22 15:44:22 字数 2242 浏览 1 评论 0原文

我正在尝试将日期标签添加到我绘制的每个单独的酒吧中。数据看起来像这样:

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妥活 2025-01-29 15:44:22

怎么样:

bar_mids <- cumsum(c(.7, rep(1.2,9)))
par(mar=c(8,4,2,1)+.1)
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="",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=bar_mids, labels=format(ChiSum$date), las=2)

“在此处输入图像描述”

关键是要知道条的中点在哪里。默认设置是条栏是一个单元宽的,其空间(每个条之前)为0.2。因此,第一个条的中点为0.7。第二个栏的中点为0.7 + 1.2,第三个栏的中点为0.7 + 1.2*2等...这些中点应在上指定为可以将日期用作标签。为了使它们绘制得很好,您可以使用las = 2将垂直于轴的标签转动。

您也可以使用ggplot2

ChiSum %>% 
  arrange(date) %>% 
  mutate(obs = 1:n()) %>% 
ggplot(aes(x=obs, y=meanlengths, ymin = Lower2SE, ymax=Upper2SE, fill=BACI)) + 
  geom_bar(stat="identity", col="black") + 
  geom_errorbar(width=.15) + 
  scale_x_continuous(breaks=1:10, labels=ChiSum$date) + 
  theme_classic() + 
  theme(axis.text.x = element_text(angle=45, hjust=1), 
        legend.position = c(.15, .85)) + 
  labs(x="", y="Mean Length") + 
  scale_fill_manual(values=c("white", "gray50"))

”在此处输入图像描述”

How about this:

bar_mids <- cumsum(c(.7, rep(1.2,9)))
par(mar=c(8,4,2,1)+.1)
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="",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=bar_mids, labels=format(ChiSum$date), las=2)

enter image description here

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 use las=2 to turn the labels perpendicular to the axis.

You could also do it with ggplot2:

ChiSum %>% 
  arrange(date) %>% 
  mutate(obs = 1:n()) %>% 
ggplot(aes(x=obs, y=meanlengths, ymin = Lower2SE, ymax=Upper2SE, fill=BACI)) + 
  geom_bar(stat="identity", col="black") + 
  geom_errorbar(width=.15) + 
  scale_x_continuous(breaks=1:10, labels=ChiSum$date) + 
  theme_classic() + 
  theme(axis.text.x = element_text(angle=45, hjust=1), 
        legend.position = c(.15, .85)) + 
  labs(x="", y="Mean Length") + 
  scale_fill_manual(values=c("white", "gray50"))

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文