0

I have two columns of my "Review" dataset that are causing me issues - one "Year" has years formatted like "2001/02". The other "Hour" has the hour of the day formatted like "01-02". Whenever I try and use these columns in graphs, I see "Error: Discrete value supplied to continuous scale". How to I fix this? Sorry if the answer is obvious, I am a total beginner and couldn't find the answer anywhere else.

Here is my code for my "Year" column:

ggplot(review_data, aes(x = YEAR, colour = CAUSE)) +
  geom_point() +
  geom_line() +
  labs(title = "Incidents",
       subtitle = "By year and cause",
       x = NULL,
       y = "Cause") +
  scale_colour_brewer(palette = "Dark2", 
                      labels = c(),
                      name = NULL) +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

And for my "Date" column:

ggplot(review_data) = mapping = aes(x = HOUR, fill = NUMBER) +
  geom_histogram(binwidth = 1, colour = "black") +
  scale_fill_brewer(palette = "Dark2", name = NULL) +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
  labs(title = "Number per hour")
3
  • are CAUSE and NUMBER discrete values? try with fill = factor(NUMBER) instead, for case. Because these could be the variables that cause problem with the color and fill scales_brewer (which are discrete).
    – Ric
    Commented Apr 1, 2023 at 20:40
  • Otherwise, use scale_fill_distiller or scale_color_distiller for continuons variables.
    – Ric
    Commented Apr 1, 2023 at 20:41
  • Can you add a reproducible example?
    – ava
    Commented Apr 1, 2023 at 23:13

1 Answer 1

0

The problem is probably that your computer does not recognize date and hour as a date or hour, but it rather interprets them as plain text (character) variables. As you dive deeper into R, it is worth educating yourself about about its object types and classes.

Speaking simply, the class of an object defines what is allowed to do with it and how it is processed.

You can check the class of your variable with: class(review_data$YEAR).

If I guessed your problem right, it will say "character". This is a class that is used for text, and also as a fallback for everything else. Text is discrete by nature and therefore, it is very good behaviour of ggplot not to allow continuos calculations with it.

The solution is to convert your variables into a suitable class which tells the computer that it represents date or time information and hopefully ggplot will then understand this information.

Here is the conversion process:

For the Year/Month Variable:

To convert from "2001/02" format into a date, consult this question where the possibilities are covered in detail: Converting year and month ("yyyy-mm" format) to a date?

An easy way with the lubridate package would be:

review_data$year_month <- lubridate::ym(review_data$YEAR)

For the Hour/Minute variable:

using the lubridate package:

review_data$hour_minute <- lubridate::hm(review_data$HOUR)
2
  • 1
    Hi, thanks so much for your response. Unfortunately the date format 2001/02 refers to 2001 and 2002. Likewise the 01-01 relates to 01:00AM and 02:00 AM. Any ideas on how to work around that? Thanks! :)
    – ecm1710
    Commented Apr 3, 2023 at 17:37
  • oh, well, I see. I guess this is the reason everybody on SO advocates minimal reproducible examples. Could you edit the question and provide me with one? See instructions here: stackoverflow.com/questions/5963269/…
    – uke
    Commented Apr 4, 2023 at 19:57

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.