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))
CodePudding user response:
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)
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"))