Jeff Aucone
Jeff Aucone

Reputation: 39

GGPlot Challenges

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?

Upvotes: 0

Views: 153

Answers (2)

AHart
AHart

Reputation: 448

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()

Upvotes: 1

ravic_
ravic_

Reputation: 1831

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()

Upvotes: 1

Related Questions