0
votes

I have a large dataframe. i am trying to plot sales for 2 different years in the same plots as line graph to show the variation across 2 years each month. There is a long series of grouping and filtering i have done before getting the below dataframe. Dataframe has 3 columns (month, sales and the year)

When I am trying to plot the sales across the different years as :

ggplot(df,aes(x=month.sales,y=sales/100000,color=year)) + 
  geom_line()

I am getting a blank graph with x and y labels , while if I plot a column graph, it works. Please help. thank you

1
It probably has to do with the class of your columns (my guess is that month.sales is a factor in your dataframe).maarvd
Why is this question tagged python? Can you post sample data? Please edit the question with the output of dput(df). Or, if it is too big with the output of dput(head(df, 20)).Rui Barradas
@maarvd... Thank you, I checked , yes the month.sales is a factored column.How do I go further to get the desired plot ?. I am a complete beginner ....please guideSurbhi Mishra

1 Answers

1
votes

I'm guessing your data looks something like this:

set.seed(69)

df <- data.frame(month.sales = factor(rep(month.abb, 2), month.abb),
                 year = rep(2018:2019, each = 12),
                 sales = runif(24, 1, 2) * 100000)

df
#>    month.sales year    sales
#> 1          Jan 2018 114570.1
#> 2          Feb 2018 123197.1
#> 3          Mar 2018 166092.7
#> 4          Apr 2018 163214.1
#> 5          May 2018 109486.6
#> 6          Jun 2018 131429.8
#> 7          Jul 2018 167363.6
#> 8          Aug 2018 191097.6
#> 9          Sep 2018 127427.4
#> 10         Oct 2018 145360.1
#> 11         Nov 2018 134577.1
#> 12         Dec 2018 169486.6
#> 13         Jan 2019 168493.2
#> 14         Feb 2019 147552.5
#> 15         Mar 2019 139811.3
#> 16         Apr 2019 156351.2
#> 17         May 2019 199368.3
#> 18         Jun 2019 130953.6
#> 19         Jul 2019 148150.5
#> 20         Aug 2019 166307.3
#> 21         Sep 2019 121830.8
#> 22         Oct 2019 101838.1
#> 23         Nov 2019 109716.9
#> 24         Dec 2019 125407.9

In which case you can draw a line plot like this:

library(ggplot2)

ggplot(df, aes(x = month.sales, y = sales / 100000, 
               color = factor(year), group = factor(year))) + 
  geom_line()

enter image description here

Note that you need to add the group aesthetic so that ggplot doesn't automatically group your data points according to the factor levels on the x axis.