I have a data frame (see below) that shows sales by region by year. The final column calculates the sum of all the sales in the region over the three year period.
I am new to R and would like use ggplot
to create a SINGLE scatter plot to analyze the data. The x-axis would be the three years and the y-axis would sales.
Ideally, each region would have its own line with points (other than a few NAs) in 2013, 2014, 2015, and 2016. I would then like to color each line based on its region. The sum column should not appear on the plot. Any ideas?
df <- structure(list(Region = structure(1:6,
.Label = c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
"K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U"),
class = "factor"),
"2016" = c(8758.82, 25559.89, 30848.02, 8696.99, 3621.12, 5468.76),
"2015" = c(26521.67, 89544.93, 92825.55, 28916.4, 14004.54, 16618.38),
"2014" = c(NA, NA, 199673.73, 37108.09, 16909.87, 20610.58),
"2013" = c(27605.35, NA, 78794.31, 31824.75, 17990.21, 17307.11),
"Total Sales" = c(35280.49, 115104.82, 323347.3, 74721.48, 34535.53, 42697.72)),
row.names = c(NA, 6L), class = "data.frame")
Your data is in wide format so it's better to convert it to long format to work with
ggplot
. Here I usetidyr::gather()
to do thatPlot: specify
color = Region
andgroup = Region
insideaes
soggplot
knows how to pick color and draw linesCan also use
facet_grid()
Created on 2018-10-12 by the reprex package (v0.2.1.9000)