Plotting a total across categories

I'm extracting from a large dataset and looking to plot values and totals of some of the resultant data. As every extract is different I have to calculate the totals on the fly and can't quite get it to work.

This example displays the monthly cost for Shaft and Wheel and I want to add a third data series for Shaft+Wheel.
I think this should be simple but I just can't find the answer!

Thanks

Michael

``````library(ggplot2)
# Sample Data Frame
sampdata <-data.frame (Code = c('ab44c', 'ab55d', 'ab44c', 'ab55d', 'ab44c', 'ab55d', 'ab44c', 'ab55d', 'ab44c', 'ab55d', 'ab44c', 'ab55d'),
Desc = c('Shaft', 'Wheel', 'Shaft', 'Wheel', 'Shaft', 'Wheel', 'Shaft', 'Wheel', 'Shaft', 'Wheel', 'Shaft', 'Wheel'),
Month = c(202007, 202007, 202008, 202008, 202009, 202009, 202010, 202010, 202011, 202011, 202012, 202012),
Number = c(5, 45, 3, 23, 4, 89, 7, 2, 12, 88, 2, 47),
Cost = c(256.5, 1352.4, 153.9, 30.05, 205.2, 691.23, 359.1, 2674.75, 615.6, 60.11, 102.6, 2644.69)
)
# Simple plot.
ggplot(data = sampdata, aes(x = Month, y = Cost, group = Desc)) + geom_line()
``````

Change to

``````color  = Desc
``````
``````library(ggplot2)
# Sample Data Frame
sampdata <- data.frame(
Code = c("ab44c", "ab55d", "ab44c", "ab55d", "ab44c", "ab55d", "ab44c", "ab55d", "ab44c", "ab55d", "ab44c", "ab55d"),
Desc = c("Shaft", "Wheel", "Shaft", "Wheel", "Shaft", "Wheel", "Shaft", "Wheel", "Shaft", "Wheel", "Shaft", "Wheel"),
Month = c(202007, 202007, 202008, 202008, 202009, 202009, 202010, 202010, 202011, 202011, 202012, 202012),
Number = c(5, 45, 3, 23, 4, 89, 7, 2, 12, 88, 2, 47),
Cost = c(256.5, 1352.4, 153.9, 30.05, 205.2, 691.23, 359.1, 2674.75, 615.6, 60.11, 102.6, 2644.69)
)
# Simple plot.
ggplot(data = sampdata, aes(x = Month, y = Cost, color = Desc)) +
geom_line()
``````

Thanks technocrat that shows the plot I get (but nicer ) . What I'm looking for is to be able to plot a third line which is the sum of the other two.
It is the equivalent of adding a third row of data called "Total" which would have, in this case, the sum of the Cost fields, grouped by the Month field.

Michael

Various searches have led me to this solution which is not what I had envisioned but might work out better for me as I expand what I'm doing with the data.

Any other suggestions are still welcome!

``````library(ggplot2)

sampdata <-data.frame (Code = c('ab44c', 'ab55d', 'ab44c', 'ab55d', 'ab44c', 'ab55d', 'ab44c', 'ab55d', 'ab44c', 'ab55d', 'ab44c', 'ab55d'),
Desc = c('Shaft', 'Wheel', 'Shaft', 'Wheel', 'Shaft', 'Wheel', 'Shaft', 'Wheel', 'Shaft', 'Wheel', 'Shaft', 'Wheel'),
Month = c(202007, 202007, 202008, 202008, 202009, 202009, 202010, 202010, 202011, 202011, 202012, 202012),
Number = c(5, 45, 3, 23, 4, 89, 7, 2, 12, 88, 2, 47),
Cost = c(256.5, 1352.4, 153.9, 30.05, 205.2, 691.23, 359.1, 2674.75, 615.6, 60.11, 102.6, 2644.69)
)
# Calculate the total and store in a separate dataframe
samp2data <- sampdata %>% group_by(Month) %>% summarise(Total = sum(Cost))

# This plots the two dataframes together on the one chart.
# Note how they could have different plot types etc.
ggplot() +
geom_line(data = sampdata, aes(x = Month, y = Cost, color = Desc)) +
geom_line(data = samp2data, aes(x = Month, y = Total, color = "Total"))
# Adding +facet_wrap(~Desc) gives separate charts for each category
# and each also has the total plotted.
``````

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.