Christopher Williams
Christopher Williams

Reputation: 35

Repeat years on x-axis in ggplot2

I have a dataframe that I used dplyr to create mean values for some numerical variables. I grouped them by site and by year. I'm now plotting the trendlines in ggplot2, using this code and delta is just a variable for the difference in mean values of interest:

ggplot(data=df_byyear, aes(x=Year, y=delta)) +
  geom_line(aes(color=Siteid)) +geom_smooth(method=lm)

The problem I am hitting is that it is adding duplicate years on the x axis as seen in this chart:

chart results from ggplot2 showing the duplicate years

I've verified the data only contains single instances of each of the three years of interest (please note I've left out the Siteid, each grouping of three years is assigned to one of the different sites.

capture of data - delta column is not shown

I hope this provides sufficient information to help with this, I've never seen this occur and am still a beginner in r....thanks!

Upvotes: 2

Views: 563

Answers (1)

iod
iod

Reputation: 7592

Just so it's easy for people to find, here's the answer:

It would seem ggplot is adding points along what it considers to be a continuous scale (i.e., 2016.5, 2017.5 etc). I'm not sure why they appear as integers, nor why 2017 only shows up once. At any rate, the way to resolves this is to add the following to the ggplot call:

scale_x_continuous(breaks=unique(df_byyear$Year))

This tells ggplot to only place ticks for the numbers that actually appear in df_byyear$Year (that is, the integers).

Upvotes: 1

Related Questions