Jack Maling
Jack Maling

Reputation: 23

Trouble w/ stratifying x-axis labels in R

I'm trying to display year labels on the x-axis in intervals of ten. When I use the following code, it says "Error: Discrete value supplied to continuous scale", but when I use scale_x_discrete, the labels don't display.

angler_byclass %>%
  filter(class != "NA") %>%
  ggplot(aes(x = tournament_year, y = total_weight_oz, color = class)) +
    geom_point() + 
    theme(axis.text.x = element_blank()) + 
    scale_x_continuous(breaks = seq(1970, 2020, by = 10))

Here's the head of my data:

angler tournament_year                                                                              tournament
1 matt-arey            2020                           2020 YETI Bassmaster Elite at Lake St. Clair (Lake St. Clair)
2 matt-arey            2020                                2020 Bassmaster Elite at Lake Champlain (Lake Champlain)
3 matt-arey            2020                2020 SiteOne Bassmaster Elite at St. Lawrence River (St. Lawrence River)
4 matt-arey            2020                             2020 DEWALT Bassmaster Elite at Lake Eufaula (Lake Eufaula)
5 matt-arey            2020  2020 Academy Sports + Outdoors Bassmaster Classic presented by Huk (Lake Guntersville)
6 matt-arey            2020                        2020 AFTCO Bassmaster Elite at St. Johns River (St. Johns River)
           location place total_weight_oz cash_winnings merchandise_bonus cash_bonus total_money   class
1  Macomb County MI    67             464          2500                 0          0        2500   elite
2    Plattsburgh NY    17             865         10000                 0          0       10000   elite
3        Clayton NY    42             521          7500                 0          0        7500   elite
4        Eufaula AL     7            1225         15000                 0          0       15000   elite
5     Birmingham AL    43             261         10000                 0          0       10000 classic
6        Palatka FL     7             641         15000                 0          0       15000   elite

Thanks for the help!

Upvotes: 1

Views: 43

Answers (1)

Dave2e
Dave2e

Reputation: 24119

I can't reproduce your problem.
Here is the a script I wrote in simulate your data:

angler_byclass <- data.frame(tournament_year = sample(1970:2020, 100, replace=TRUE),
                             total_weight_oz =runif(100, 400, 1500),
                             class=sample(c("A", "B", "C"), 100, replace=TRUE))
str(angler_byclass)
angler_byclass %>%
   filter(class != "NA") %>%
   ggplot(aes(x = tournament_year, y = total_weight_oz, color = class)) +
   geom_point() + 
#   theme(axis.text.x = element_blank()) + 
   scale_x_continuous(breaks = seq(1970, 2020, by = 10))

Unless you can provide more information, this is the best answer I can provide.

Update
Based on you comment below the type of "tournament_year" is a factor and not an integer as mention in your comment above.
The easiest solution is to covert this column to an integer, provided you can afford to lose (or edit) the nonstandard values like: " 1993-1994".

To convert a factor to numeric:

angler_byclass$tournament_year <-as.integer(trimws(as.character(angler_byclass$tournament_year)))

Upvotes: 0

Related Questions