0

I've created a gathered data frame with 3 columns.
Column 1 is of chr type called "Designation" and the content is either "yes" or "no".
Column 2 is of chr type called "Month" and the content is the month for the associated data in column 3. Content would be "Jan 19", "Feb 19"...
Column 3 is of num type called "Volumes" and will have counts in each field.

For every month there are two designations, each with a count. So, the first four rows would be:

yes, Jan 19, 123456
no, Jan 19, 789012
yes, Feb 19, 5858585
no, Feb 19, 2543425
...

I'm trying to goem_line this data and getting strange results. Here is the code:

ggplot(datagathered, aes(x = Month, y = Volumes, color = Designation))+
  geom_line()

I do not see any graph results. The x axis lists the months, the y axis lists the volume scale (ugly, but I can fix this), there is no line graph...

What am I missing?

Dave2e
  • 22,192
  • 18
  • 42
  • 50
  • 3
    Please add a [mcve] of your data by using the `dput` function so that we can reproduce you problem and try to solve it – divibisan Nov 19 '19 at 19:23

2 Answers2

1

Your x aesthetic, "Month", is a character and being coerced to a factor. Factors require a group= clause to properly plot a line plot. See more here:

Using `geom_line()` with X axis being factors

In your case, this should work:

ggplot(datagathered, aes(x = Month, y = Volumes, 
                         group = Designation, color = Designation))+
  geom_line()
ravic_
  • 1,731
  • 9
  • 13
1

You can specify Designation as the group variable, but the order is off (as it's coercing from character). Easiest to just set the date.

Given the data:

df <- tribble(
  ~Designation, ~Month, ~Volumes,
  'yes', 'Jan 19', 123456,
  'no', 'Jan 19', 789012,
  'yes', 'Feb 19', 5858585,
  'no', 'Feb 19', 2543425
)

and declaring Month as a date:

df <- 
  df %>%
  mutate(
    Month = as.Date(Month, '%B %d')
  )

The graph works without the grouping:

df %>%
  ggplot(aes(x = Month, y = Volumes, color = Designation)) +
  geom_line()
AHart
  • 448
  • 3
  • 10
  • Afraid this didn't work, but did give me another idea about converting the date. Instead used: ''' df$Month <- as.date(df$Month, '%B %d') Then the graph worked, although ugly at the moment. Thanks for the guidance on the issue. – Jeff Aucone Nov 20 '19 at 02:35
  • Aha. I see that I didn't assign the mutate to any object (edited above and should work now). The line you use is equivalent. – AHart Nov 20 '19 at 13:33