0

I have a line graph with the number of visitors staying at a campsite per night. My dataset ends at 10-28-2019, but my graph extends past that date which is unnecessary and provides false information. I want the graph to end at max(nightcounts$date) but am not sure where this should properly go. I tried adding it in line 1 as

aes(max(nightcounts$date), nightcounts$Freq

as an addition to the current aesthetic. Secondly, I am tryin got reformat the x- axis labeling to have the number day below each point, and then the month as a second line below thse points such as

27 28 29 30 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

September___________October

1 base <- ggplot(data=nightcounts, aes(nightcounts$date, nightcounts$Freq)) #using ggplot with x = date, y = counts
2 base + geom_line(col="darkorchid4", aes(group = 1)) + #calling geom_line to make a line connecting data points
3  geom_point() + #calling geom_point to add points for each data point
4  labs(title = "Number of Visitors Camping per Night", #adding labels
5       x = "Date",
6       y = "Visitors") +
7  scale_x_date(labels = date_format("%m-%d-%Y"), breaks = date_breaks("week")) +
8  ggsave("Camping by Date.png") #save the image

image of my current plot

Maridee Weber
  • 231
  • 1
  • 8
  • 1
    Add `expand = c(0, 0)` inside `scale_x_date` to keep the axis from expanding beyond the data. To label the day of month use `date_format("%d")`. Getting month labels is a little trickier, I can maybe find a dupe for that too. – Gregor Thomas Dec 12 '19 at 15:53
  • Also, don't use `data$column` inside `aes()`, just use column names. `aes(date, Freq)`. If you use some more complicated features like facets or aggregates, `data$column` will cause problems. – Gregor Thomas Dec 12 '19 at 15:55
  • Thank you for the response, I changed the aesthetic coding. The ```expand = c(0, 0)``` did not change the graph and is still including extra dates. Edit: it was an error in the original dataset I just found, thank you!! – Maridee Weber Dec 12 '19 at 22:11

0 Answers0