1

I want to dodge the points and error bars on this time series, but I keep getting stacked points/error bars

AcrossSites<-structure(list(ExpectedPA = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 
5L, 5L, 5L), .Label = c("0", "1", "5", "20", "50"), class = "factor"), 
    Date = structure(c(1649721600, 1653609600, 1657497600, 1660262400, 
    1662508800, 1649721600, 1653609600, 1657497600, 1660262400, 
    1662508800, 1649721600, 1653609600, 1657497600, 1660262400, 
    1662508800, 1649721600, 1653609600, 1657497600, 1660262400, 
    1662508800, 1649721600, 1653609600, 1657497600, 1660262400, 
    1662508800), tzone = "UTC", class = c("POSIXct", "POSIXt"
    )), avg = c(79, 55.5, 63.7916666666667, 53.2916666666667, 
    64.5416666666667, 79.7916666666667, 53.2916666666667, 52.9166666666667, 
    43.9166666666667, 48.5, 75.1666666666667, 56.2916666666667, 
    63.8333333333333, 53.7916666666667, 65.5416666666667, 74.9583333333333, 
    61.25, 55.375, 43.2083333333333, 47.5416666666667, 80.9166666666667, 
    63.125, 57, 51.2916666666667, 49.9583333333333), SE = c(2.46717581897585, 
    2.05362881367296, 3.5914447290117, 3.92939459818081, 6.08632917100101, 
    2.70933649203142, 2.69895253881266, 4.06688494306104, 4.94422392712404, 
    6.65097791654557, 2.56415273083589, 2.67535665146918, 3.56394315628856, 
    4.71044905651358, 7.02814712802315, 2.26302923721776, 1.89129393633649, 
    4.1230232417698, 4.1702792310269, 5.23746923019105, 2.20006313040735, 
    1.69217348015992, 3.87765804114519, 4.22230068418071, 5.85249827946247
    )), class = c("grouped_df", "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -25L), groups = structure(list(ExpectedPA = structure(1:5, .Label = c("0", 
"1", "5", "20", "50"), class = "factor"), .rows = structure(list(
    1:5, 6:10, 11:15, 16:20, 21:25), ptype = integer(0), class = c("vctrs_list_of", 
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -5L), .drop = TRUE))

Here's my data frame, and I want to graph "ExpectedPA" changing over time



ggplot(AcrossSites, aes(x=Date, y=avg, ymin=avg-SE, ymax=avg+SE, color=ExpectedPA))+
  geom_line(size=1, position=position_dodge(width=10))+
  geom_point(position =position_dodge(width=10), size=2)+
  geom_errorbar(position=position_dodge(width=10), width=10, size=0.6)+
  ylab("Total Stems")+
  ggtitle("Total Stems Across Sites")+
  xlab("")+
  theme_bw()+
  theme(legend.title = element_blank(), legend.position = c(.8, .8), text = element_text(color="black", size = 12))

Please advise!

stefan
  • 90,330
  • 6
  • 25
  • 51
Hallie
  • 11
  • 2

1 Answers1

0

One option would be to convert your datetimes to dates. Not sure whether that is an option for you but at least from your example data I don't see any reason to work with date times.

library(ggplot2)

ggplot(AcrossSites, aes(x = as.Date(Date), y = avg, ymin = avg - SE, ymax = avg + SE, color = ExpectedPA)) +
  geom_line(size = 1, position = position_dodge(width = 10)) +
  geom_point(position = position_dodge(width = 10), size = 2) +
  geom_errorbar(position = position_dodge(width = 10), width = .1, size = 0.6) +
  ylab("Total Stems") +
  ggtitle("Total Stems Across Sites") +
  xlab("") +
  theme_bw() +
  theme(legend.title = element_blank(), legend.position = c(.8, .8), text = element_text(color = "black", size = 12))

enter image description here

UPDATE I had another look. Dodging works for date times too. But to make it visible we have to increase the width even further, e.g. in the code below I use 1e6:

library(ggplot2)

ggplot(AcrossSites, aes(x = Date, y = avg, ymin = avg - SE, ymax = avg + SE, color = ExpectedPA)) +
  geom_line(size = 1, position = position_dodge(width = 1e6)) +
  geom_point(position = position_dodge(width = 1e6), size = 2) +
  geom_errorbar(position = position_dodge(width = 1e6), width = .1, size = 0.6) +
  ylab("Total Stems") +
  ggtitle("Total Stems Across Sites") +
  xlab("") +
  theme_bw() +
  theme(legend.title = element_blank(), legend.position = c(.8, .8), text = element_text(color = "black", size = 12))

enter image description here

stefan
  • 90,330
  • 6
  • 25
  • 51
  • Thank you! I take it you can't dodge a datetime? – Hallie Oct 26 '22 at 15:10
  • Hi Hallie. I just had another look. And actually the dodging is already present in your original plot. But it's hardly or not visible. To make it visible increase the width for the dodging. See my update. – stefan Oct 26 '22 at 15:21