add-semi-colons
add-semi-colons

Reputation: 18810

building a ggplot facet grid for 4 columns after melt

I started with a wider table and after melting I got the table to 4 columns as shown below:

      team       DATE     Rank        winning_times
     team1    20180925 place1           1646
     team2    20180925 place1            876
     team3    20180925 place1            601
     team4    20180925 place1            438
     team5    20180925 place1            321
     team1    20180925 place2           1546
     team2    20180925 place2            976
     team3    20180925 place2            501
     team4    20180925 place2            338
     team5    20180925 place2            421
     team1    20180925 place3           2546
     team2    20180925 place3            476
     team3    20180925 place3            501
     team4    20180925 place3            638
     team5    20180925 place3            121
     team1    20180926 place1           1046
     team2    20180926 place1            806
     team3    20180926 place1            61
     team4    20180926 place1            48
     team5    20180926 place1            31
     team1    20180925 place2           1446
     team2    20180925 place2            276
     team3    20180925 place2            201
     team4    20180925 place2            238
     team5    20180925 place2            221 

My goal is to build a plot where X-axis representing date and y-axis representing winning_times and build a facet_grid or facet_wrap. But when I try to do facet_grid I get

geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?

This is also a large data set where there are data for 30 days for each team representing each place they obtain along with winning times.

ggplot(data=df.m, 
       aes(x=factor(DATE), y=winning_times, 
           group=Rank,
           shape=team,
           color=team)) + 
  geom_line() + 
  geom_point() +
  scale_x_discrete("DATE") +
  scale_y_continuous("WiningTimes") + 
  facet_grid(Rank ~ team )

Upvotes: 2

Views: 722

Answers (1)

www
www

Reputation: 39154

If I change DisplayedTimes to winning_times in your code, I can create the following plot.

library(ggplot2)

ggplot(data=df.m, 
       aes(x = factor(DATE), y = winning_times, 
           group = Rank,
           shape = team,
           color = team)) + 
  geom_line() + 
  geom_point() +
  scale_x_discrete("DATE") +
  scale_y_continuous("WiningTimes") + 
  facet_grid(Rank ~ team)

enter image description here

This looks good to me.

Update

A version with histogram.

library(ggplot2)

df.m$DATE <- factor(df.m$DATE)

ggplot(data=df.m, 
       aes(x = winning_times, 
           fill = DATE,
           color = DATE)) + 
  geom_histogram(alpha = 0.5, position = "identity") +
  scale_x_continuous("WiningTimes") +
  facet_grid(Rank ~ team)

enter image description here

DATA

df.m <- read.table(text = "       team       DATE     Rank        winning_times
     team1    20180925 place1           1646
     team2    20180925 place1            876
     team3    20180925 place1            601
     team4    20180925 place1            438
     team5    20180925 place1            321
     team1    20180925 place2           1546
     team2    20180925 place2            976
     team3    20180925 place2            501
     team4    20180925 place2            338
     team5    20180925 place2            421
     team1    20180925 place3           2546
     team2    20180925 place3            476
     team3    20180925 place3            501
     team4    20180925 place3            638
     team5    20180925 place3            121
     team1    20180926 place1           1046
     team2    20180926 place1            806
     team3    20180926 place1            61
     team4    20180926 place1            48
     team5    20180926 place1            31
     team1    20180925 place2           1446
     team2    20180925 place2            276
     team3    20180925 place2            201
     team4    20180925 place2            238
     team5    20180925 place2            221 ",
                  header = TRUE, stringsAsFactors = FALSE)

Upvotes: 1

Related Questions