user13192625
user13192625

Reputation:

R data not being plotted correctly

I'm really struggling with plots in R with ggplot and dplyr.

I have a dataframe with columns:

Customer.Name, Customer.Code, Product, Date, Platform, Input.Records, Output.Records

I performed a grouping with dplyr, to group the input records by the date.

df$Date <- as.Date(df$Date, format = "%Y-%m-%d")
dateGrouping <- df%>% group_by(`Date`) %>% summarise(`Input.Records` = sum(`Input.Records`))

Then I tried to plot that.

myPlot <- ggplot(data=dateGrouping, aes(x=factor(Date), y=`Input.Records`, group=1)) +
  geom_line() + ylim(0, 85000000)
myPlot

But the output looks wrong, it looks like a bar plot, there is no connection between the lines The "Line" plot

So I swapped geom_line() with geom_path() enter image description here

And this definitely looks wrong...why is the path going back and forth? it should be a left-to-right linear trajectory shouldn't it?

This is the code to replicate the example. Including only the code for the grouped frame because the full file is too large and contains confidential data.

   library(ggplot2) 
   library(dplyr) 
   df <- data.frame("Date" = c( "2020-08-10", "2020-08-11", "2020-08-12", "2020-08-13", "2020-08-14", "2020-08-15", "2020-08-16", "2020-08-17", "2020-08-18",
 "2020-08-19", "2020-08-20", "2020-08-21", "2020-08-22", "2020-08-23", "2020-08-24"),
 "Input.Records" = c(19501675,19298520,75546425,90104271,34139598,35384083,11849216,21996019,241643844,55643434,20733736,46198249,9815057,78211864,103263783))
    
    myPlot <- ggplot(data=df, aes(x=factor(Date), y=`Input.Records`, group=1)) +
      geom_path() + ylim(0, 85000000)
    myPlot

Upvotes: 0

Views: 66

Answers (1)

Duck
Duck

Reputation: 39613

I think this is what you are looking for. I made some slight changes to your code:

library(ggplot2)
#Plot
ggplot(data=df, aes(x=Date, y=Input.Records, group=1)) +
  geom_line()

Output:

enter image description here

Let me know if that works for you.

Upvotes: 1

Related Questions