Arvind Suryanarayana Arvind Suryanarayana - 2 months ago 13
R Question

Trying to group a range of values on X-Axis in R

Below is a part of the dataset i'm using to visualise.

**Merchandise Stores Year Total Sales**
White goods Shop01 2015 45,547,000
White goods Shop02 2015 28,775,000
White goods Shop03 2015 17,029,000
White goods Shop04 2015 28,827,000
White goods Shop05 2015 38,929,000
White goods Shop06 2015 10,740,000
White goods Shop07 2015 15,008,000
White goods Shop08 2015 10,013,000
White goods Shop09 2015 14,556,000
White goods Shop10 2015 10,669,000
Groceries Shop01 2015 14,334,000
Groceries Shop02 2015 6,521,000
Groceries Shop03 2015 30,025,000
Groceries Shop04 2015 5,899,000
Groceries Shop05 2015 5,333,919
Groceries Shop06 2015 1,915,000
Groceries Shop07 2015 8,312,000
Groceries Shop08 2015 4,053,000
Groceries Shop09 2015 3,506,000
Groceries Shop10 2015 3,492,000


I'm trying to group the Total.Sales into a range of values so that it's not too big for the plot. The code I tried is:

p <- ggplot(dat, aes(x=Year, y=Total.Sales, fill=Stores)) + geom_area() + scale_y_continuous(breaks=c(0, 100000, 1000000, 5000000, 10000000, 150000000))


This gives me an error saying "Discrete value supplied to continuous scale"
Don't know what I'm missing here as I'm new to R. Kindly Help.

UPDATE
Output after Roland's suggestion:
My Plot

Desired output:
Expected visualization

dput(dat):

structure(list(Merchandise = structure(c(4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L,
4L, 4L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L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L), .Label = c("Shop01", "Shop02", "Shop03", "Shop04",
"Shop05", "Shop06", "Shop07", "Shop08", "Shop09", "Shop10"), class = "factor"),
Year = c(2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L,
2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L,
2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L,
2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L,
2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2014L, 2014L, 2014L,
2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L,
2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L,
2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L,
2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L,
2014L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L,
2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L,
2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L,
2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L,
2013L, 2013L, 2013L, 2013L, 2013L, 2012L, 2012L, 2012L, 2012L,
2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L,
2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L,
2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L,
2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L,
2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L,
2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L,
2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L,
2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L,
2011L, 2011L, 2011L, 2011L), Total.Sales = c(45547000, 28775000,
17029000, 28827000, 38929000, 10740000, 15008000, 10013000,
14556000, 10669000, 14334000, 6521000, 30025000, 5899000,
5333919, 1915000, 8312000, 4053000, 3506000, 3492000, 11601000,
7730000, 7519000, 14426000, 7046000, 1875000, 7902000, 6775000,
6063000, 15843000, 19047000, 8250000, 21218000, 7161000,
6812000, 1728000, 9807000, 5543000, 5012000, 4790000, 71817000,
43940000, 35523000, 56430000, 70254000, 19149000, 28253000,
133249000, 27730000, 25198000, 15233000, 10179000, 24703000,
9482000, 8850000, 2800000, 11749000, 8734000, 7753000, 7698000,
10135000, 9094000, 8669000, 11978000, 8438000, 2391000, 9526000,
7978000, 8791000, 13948000, 18087000, 8626000, 21835000,
6935000, 6134000, 2380000, 9306000, 5787000, 4388000, 4755000,
80738000, 43987000, 51321000, 54873000, 73234000, 18765000,
38748000, 103439000, 36943000, 27176000, 12348000, 95600000,
18047000, 9104000, 8190000, 2096000, 9719000, 7557000, 7503000,
6918000, 7530000, 5456000, 5452000, 7459000, 5218000, 1615000,
5613000, 4532000, 4147000, 6534000, 18168000, 7462000, 19321000,
5819000, 5733269, 1994284, 8622000, 5745984, 4532426, 4395000,
71823000, 38904000, 35842000, 57027000, 52850000, 19012000,
28645000, 0, 0, 0, 10408000, 7429000, 10915000, 6997000,
5571000, 1559000, 8543000, 0, 0, 0, 7314000, 5396000, 5041000,
9620000, 4468000, 1304000, 5426000, 0, 0, 0, 13694000, 7615000,
20337000, 7569000, 6201000, 135200, 8673000, 0, 0, 0, 65325000,
47179000, 29659000, 37861000, 58183000, 16129000, 23828000,
0, 0, 0, 13237000, 8825000, 13981000, 7841000, 4597000, 1155589,
13109000, 0, 0, 0, 6412000, 4998000, 4776000, 8306000, 5749000,
1539000, 6013000, 0, 0, 0, 16683000, 6223000, 17941000, 5740000,
4019000, 1453000, 7130000, 0, 0, 0), Latitude = c(-37.8594,
-37.8532, -38.3697, -34.3698, -33.4187, -31.0025, -28.8214,
-42.8826, -27.405, -28.0632, -37.8594, -37.8532, -38.3697,
-34.3698, -33.4187, -31.0025, -28.8214, -42.8826, -27.405,
-28.0632, -37.8594, -37.8532, -38.3697, -34.3698, -33.4187,
-31.0025, -28.8214, -42.8826, -27.405, -28.0632, -37.8594,
-37.8532, -38.3697, -34.3698, -33.4187, -31.0025, -28.8214,
-42.8826, -27.405, -28.0632, -37.8594, -37.8532, -38.3697,
-34.3698, -33.4187, -31.0025, -28.8214, -42.8826, -27.405,
-28.0632, -37.8594, -37.8532, -38.3697, -34.3698, -33.4187,
-31.0025, -28.8214, -42.8826, -27.405, -28.0632, -37.8594,
-37.8532, -38.3697, -34.3698, -33.4187, -31.0025, -28.8214,
-42.8826, -27.405, -28.0632, -37.8594, -37.8532, -38.3697,
-34.3698, -33.4187, -31.0025, -28.8214, -42.8826, -27.405,
-28.0632, -37.8594, -37.8532, -38.3697, -34.3698, -33.4187,
-31.0025, -28.8214, -42.8826, -27.405, -28.0632, -37.8594,
-37.8532, -38.3697, -34.3698, -33.4187, -31.0025, -28.8214,
-42.8826, -27.405, -28.0632, -37.8594, -37.8532, -38.3697,
-34.3698, -33.4187, -31.0025, -28.8214, -42.8826, -27.405,
-28.0632, -37.8594, -37.8532, -38.3697, -34.3698, -33.4187,
-31.0025, -28.8214, -42.8826, -27.405, -28.0632, -37.8594,
-37.8532, -38.3697, -34.3698, -33.4187, -31.0025, -28.8214,
-42.8826, -27.405, -28.0632, -37.8594, -37.8532, -38.3697,
-34.3698, -33.4187, -31.0025, -28.8214, -42.8826, -27.405,
-28.0632, -37.8594, -37.8532, -38.3697, -34.3698, -33.4187,
-31.0025, -28.8214, -42.8826, -27.405, -28.0632, -37.8594,
-37.8532, -38.3697, -34.3698, -33.4187, -31.0025, -28.8214,
-42.8826, -27.405, -28.0632, -37.8594, -37.8532, -38.3697,
-34.3698, -33.4187, -31.0025, -28.8214, -42.8826, -27.405,
-28.0632, -37.8594, -37.8532, -38.3697, -34.3698, -33.4187,
-31.0025, -28.8214, -42.8826, -27.405, -28.0632, -37.8594,
-37.8532, -38.3697, -34.3698, -33.4187, -31.0025, -28.8214,
-42.8826, -27.405, -28.0632, -37.8594, -37.8532, -38.3697,
-34.3698, -33.4187, -31.0025, -28.8214, -42.8826, -27.405,
-28.0632), Longitude = c(144.7994, 147.5463, 142.4617, 150.6471,
149.5702, 150.1511, 153.26, 147.3216, 152.4424, 148.2859,
144.7994, 147.5463, 142.4617, 150.6471, 149.5702, 150.1511,
153.26, 147.3216, 152.4424, 148.2859, 144.7994, 147.5463,
142.4617, 150.6471, 149.5702, 150.1511, 153.26, 147.3216,
152.4424, 148.2859, 144.7994, 147.5463, 142.4617, 150.6471,
149.5702, 150.1511, 153.26, 147.3216, 152.4424, 148.2859,
144.7994, 147.5463, 142.4617, 150.6471, 149.5702, 150.1511,
153.26, 147.3216, 152.4424, 148.2859, 144.7994, 147.5463,
142.4617, 150.6471, 149.5702, 150.1511, 153.26, 147.3216,
152.4424, 148.2859, 144.7994, 147.5463, 142.4617, 150.6471,
149.5702, 150.1511, 153.26, 147.3216, 152.4424, 148.2859,
144.7994, 147.5463, 142.4617, 150.6471, 149.5702, 150.1511,
153.26, 147.3216, 152.4424, 148.2859, 144.7994, 147.5463,
142.4617, 150.6471, 149.5702, 150.1511, 153.26, 147.3216,
152.4424, 148.2859, 144.7994, 147.5463, 142.4617, 150.6471,
149.5702, 150.1511, 153.26, 147.3216, 152.4424, 148.2859,
144.7994, 147.5463, 142.4617, 150.6471, 149.5702, 150.1511,
153.26, 147.3216, 152.4424, 148.2859, 144.7994, 147.5463,
142.4617, 150.6471, 149.5702, 150.1511, 153.26, 147.3216,
152.4424, 148.2859, 144.7994, 147.5463, 142.4617, 150.6471,
149.5702, 150.1511, 153.26, 147.3216, 152.4424, 148.2859,
144.7994, 147.5463, 142.4617, 150.6471, 149.5702, 150.1511,
153.26, 147.3216, 152.4424, 148.2859, 144.7994, 147.5463,
142.4617, 150.6471, 149.5702, 150.1511, 153.26, 147.3216,
152.4424, 148.2859, 144.7994, 147.5463, 142.4617, 150.6471,
149.5702, 150.1511, 153.26, 147.3216, 152.4424, 148.2859,
144.7994, 147.5463, 142.4617, 150.6471, 149.5702, 150.1511,
153.26, 147.3216, 152.4424, 148.2859, 144.7994, 147.5463,
142.4617, 150.6471, 149.5702, 150.1511, 153.26, 147.3216,
152.4424, 148.2859, 144.7994, 147.5463, 142.4617, 150.6471,
149.5702, 150.1511, 153.26, 147.3216, 152.4424, 148.2859,
144.7994, 147.5463, 142.4617, 150.6471, 149.5702, 150.1511,
153.26, 147.3216, 152.4424, 148.2859)), .Names = c("Merchandise",
"Stores", "Year", "Total.Sales", "Latitude", "Longitude"), row.names = c(NA,
-200L), class = "data.frame")

Answer

I tried using ggplot2 to create your desired plot but I failed to create the continuous plot.

The closest I got was when I used ggvis:

library(dplyr)
library(ggvis)
ggvis(dat, x=~Year, y=~Total.Sales, fill=~Stores) %>% group_by(Stores) %>% layer_bars

The code above creates this: enter image description here

I hope this helps