
library("ggplot2")
library(grid)
library(gridExtra)

# read in the file
g2 = read.csv(file.choose())

# Now, assign a color for each distinct food item
g2$Color = rep(NA, nrow(g))
g2$Color[g2$Item == "Bananas"] = "#ffe135"
g2$Color[g2$Item == "Milk"] = "#fdfff5"
g2$Color[g2$Item == "Broccoli"] = "#76904b"
g2$Color[g2$Item == "Macaroni.&.cheese"] = "#ffbd88"
g2$Color[g2$Item == "Cheese"] = "#EEE1C1"
g2$Color[g2$Item == "Yogurt"] = "#EDDDDA"
g2$Color[g2$Item == "Mushrooms"] = "#d8ccc0"
g2$Color[g2$Item == "Bread"] = "#9D7753"
g2$Color[g2$Item == "Kale"] = "#38664c"
g2$Color[g2$Item == "Eggs"] = "#fffcec"
g2$Color[g2$Item == "Goat.cheese"] = "#F1E7D7"
g2$Color[g2$Item == "Pasta"] = "#FAECA5"
g2$Color[g2$Item == "Pasta.sauce"] = "#b21807"
g2$Color[g2$Item == "Greens"] = "#83976D"
g2$Color[g2$Item == "Nutella"] = "#52270b"
g2$Color[g2$Item == "Sweet.potatoes"] = "#D87C3B"
g2$Color[g2$Item == "Apples"] = "#C83D34"
g2$Color[g2$Item == "Carrots"] = "#EB8921"
g2$Color[g2$Item == "Chucken.nuggets"] = "#C96619"
g2$Color[g2$Item == "Ginger"] = "#DFC89A"
g2$Color[g2$Item == "Chicken"] = "#EFAC99"
g2$Color[g2$Item == "Chips"] = "#F79133"
g2$Color[g2$Item == "Fruit"] = "#ff5555"
g2$Color[g2$Item == "Green.beans"] = "#709048"
g2$Color[g2$Item == "Polenta"] = "#ead098"
g2$Color[g2$Item == "Tofu"] = "#f9f5ef"
g2$Color[g2$Item == "Ice.cream"] = "#F2EDE8"
g2$Color[g2$Item == "Peaches"] = "#E77852"
g2$Color[g2$Item == "Soy.sauce"] = "#745e43"
g2$Color[g2$Item == "Spinach"] = "#1D800E"
g2$Color[g2$Item == "Tuna"] = "#F7D0A4"
g2$Color[g2$Item == "Brussels.sprouts"] = "#969e50"
g2$Color[g2$Item == "Cauliflower"] = "#f4f3e2"
g2$Color[g2$Item == "Feta"] = "#DAD8C6"
g2$Color[g2$Item == "KitKat"] = "#79462B"
g2$Color[g2$Item == "Olive.oil"] = "#dbcf5c"
g2$Color[g2$Item == "Onion"] = "#E6D2D8"
g2$Color[g2$Item == "Pinto.beans"] = "#DB8C5E"
g2$Color[g2$Item == "Rice"] = "#ede6d9"
g2$Color[g2$Item == "Sour.cream"] = "#F0ECE4"
g2$Color[g2$Item == "Tea"] = "#C58E4C"
g2$Color[g2$Item == "Teddy.grahams"] = "#BE722A"
g2$Color[g2$Item == "Tomatoes"] = "#ff6347"
g2$Color[g2$Item == "Vegetables"] = "#97BE11"
g2$Color[g2$Item == "Water.chestnuts"] = "#E3D8C0"
g2$Color[g2$Item == "Baby.corn"] = "#f7e982"
g2$Color[g2$Item == "Butternut.squash"] = "#DF8D28"
g2$Color[g2$Item == "Cheez-its"] = "#EFD773"
g2$Color[g2$Item == "Chicken.broth"] = "#D18800"
g2$Color[g2$Item == "Cookie.dough"] = "#f7e4c4"
g2$Color[g2$Item == "Cream.cheese"] = "#FFFEF2"
g2$Color[g2$Item == "Grapes"] = "#664E71"
g2$Color[g2$Item == "Grits"] = "#F5D798"
g2$Color[g2$Item == "Hummus"] = "#FDD78D"
g2$Color[g2$Item == "Jam"] = "#a50b5e"
g2$Color[g2$Item == "Ketchup"] = "#b21807"
g2$Color[g2$Item == "Kettle.corn"] = "#E1BF7B"
g2$Color[g2$Item == "Larabars"] = "#924112"
g2$Color[g2$Item == "Milano.cookies"] = "#E3CA9B"
g2$Color[g2$Item == "Nectarines"] = "#FFBD57"
g2$Color[g2$Item == "Nuts"] = "#904b1d"
g2$Color[g2$Item == "Peanut.butter"] = "#cd9141"
g2$Color[g2$Item == "Potatoes"] = "#b79268"
g2$Color[g2$Item == "Salmon"] = "#fa8072"
g2$Color[g2$Item == "Stawberries"] = "#c83f49"
g2$Color[g2$Item == "Almonds"] = "#A27F5B"
g2$Color[g2$Item == "Arborio.rice"] = "#FBEDC1"
g2$Color[g2$Item == "Asperagus"] = "#87a96b"
g2$Color[g2$Item == "Baguette"] = "#eec07b"
g2$Color[g2$Item == "Baking.soda"] = "#FFFFFF"
g2$Color[g2$Item == "Balsamic.vinegar"] = "#580A04"
g2$Color[g2$Item == "Blue.cheese"] = "#f1f7ff"
g2$Color[g2$Item == "Bread.crumbs"] = "#e3d3be"
g2$Color[g2$Item == "Brie"] = "#fcf5db"
g2$Color[g2$Item == "Brown.sugar"] = "#af6e4d"
g2$Color[g2$Item == "Buns"] = "#AB7A49"
g2$Color[g2$Item == "Burgers"] = "#3D2B23"
g2$Color[g2$Item == "Butter"] = "#fdf6c5"
g2$Color[g2$Item == "Candy"] = "#e4717a"
g2$Color[g2$Item == "Canola.oil"] = "#DBCF5C"
g2$Color[g2$Item == "Cherries"] = "#752426"
g2$Color[g2$Item == "Cherry.tomatoes"] = "#ff6347"
g2$Color[g2$Item == "Chili.powder"] = "#982A0B"
g2$Color[g2$Item == "Chocolate"] = "#7B3F00"
g2$Color[g2$Item == "Chocolate.chips"] = "#7B3F00"
g2$Color[g2$Item == "Cinnamon"] = "#d2691e"
g2$Color[g2$Item == "Corn.starch"] = "#FFFFFF"
g2$Color[g2$Item == "Corn.tortillas"] = "#DABF7E"
g2$Color[g2$Item == "Cucumber"] = "#73af59"
g2$Color[g2$Item == "Cumin"] = "#c88762"
g2$Color[g2$Item == "Drink.squeeze"] = "#f94d00"
g2$Color[g2$Item == "Fettuccini"] = "#FAECA5"
g2$Color[g2$Item == "Flour"] = "#fff5f3"
g2$Color[g2$Item == "Frozen.peas"] = "#739122"
g2$Color[g2$Item == "Garlic"] = "#f2e9d2"
g2$Color[g2$Item == "Grape.juice"] = "#63344b"
g2$Color[g2$Item == "Ground.pork"] = "#DF8A77"
g2$Color[g2$Item == "Ground.turkey"] = "#FBAC97"
g2$Color[g2$Item == "Hard.cider"] = "#DE5270"
g2$Color[g2$Item == "Horseradish"] = "#EDE8DB"
g2$Color[g2$Item == "Jelly.beans"] = "#74bf20"
g2$Color[g2$Item == "Lemon.juice"] = "#fff44f"
g2$Color[g2$Item == "Lemon.pepper.seasoning"] = "#E5BE28"
g2$Color[g2$Item == "Lemonade.powder"] = "#f7e5a3"
g2$Color[g2$Item == "Mandarins"] = "#f37a48"
g2$Color[g2$Item == "Mashed.potatoes"] = "#f3f0dc"
g2$Color[g2$Item == "Matzo"] = "#FAD290"
g2$Color[g2$Item == "Meat"] = "#f9906f"
g2$Color[g2$Item == "Mustard"] = "#e1ad01"
g2$Color[g2$Item == "Oranges"] = "#ffa500"
g2$Color[g2$Item == "Parmesan.cheese"] = "#f7f2d8"
g2$Color[g2$Item == "Parsnips"] = "#F1DEC1"
g2$Color[g2$Item == "Pesto"] = "#698238"
g2$Color[g2$Item == "Pickles"] = "#67744a"
g2$Color[g2$Item == "Pita"] = "#f6e7d2"
g2$Color[g2$Item == "Pizza"] = "#d79914"
g2$Color[g2$Item == "Potato.gnocchi"] = "#f8e9d1"
g2$Color[g2$Item == "Poultry.seasoning"] = "#B1A773"
g2$Color[g2$Item == "Quinoa"] = "#cfb597"
g2$Color[g2$Item == "Raspberries"] = "#e30b5d"
g2$Color[g2$Item == "Refried.beans"] = "#DB8C5E"
g2$Color[g2$Item == "Rotisserie.chicken"] = "#eece80"
g2$Color[g2$Item == "Salsa"] = "#f83838"
g2$Color[g2$Item == "Sandwich.meat"] = "#ffb8b8"
g2$Color[g2$Item == "Snap.peas"] = "#6E8A69"
g2$Color[g2$Item == "Soba.noodles"] = "#8F8482"
g2$Color[g2$Item == "Spaghetti"] = "#FAECA5"
g2$Color[g2$Item == "Sugar"] = "#F0F0EC"
g2$Color[g2$Item == "Tomato.paste"] = "#b21807"
g2$Color[g2$Item == "Tomato.soup"] = "#ff6347"
g2$Color[g2$Item == "Tortillas"] = "#d4cab8"
g2$Color[g2$Item == "Tortellini"] = "#FAECA5"
g2$Color[g2$Item == "Wheat.thins"] = "#98694d"
g2$Color[g2$Item == "White.wine"] = "#f1f285"
g2$Color[g2$Item == "Yellow.squash"] = "#FFDF61"
g2$Color[g2$Item == "Zucchini"] = "#516138"
g2$Color[g2$Item == "Chicken.nuggets"] = "#FEAC24"
g2$Color[g2$Item == "Wine"] = "#722f37"
g2$Color[g2$Item == "Strawberries"] = "#c83f49"
g2$Color[g2$Item == "Asparagus"] = "#87a96b"
g2$Color[g2$Item == "Ground.beef"] = "#D89C82"

# 22 x 22 grid
# assign an x and y value for every item from top left to bottom right
g2$x = ((g2$Index-1) %% 22)
g2$y = 22 - ((g2$Index-1) %/% 22)

###### Plot the legend #######

#create g3, data frame with unique items
g3 = g2[!duplicated(g2$Item),]

# convert to rgb
g5 = t(col2rgb(g3$Color))
g5 = cbind(g3,g5)
# convert from rgb to hsv, then order by hue
g6 = cbind(g5,t(rgb2hsv(r = g5$red, g = g5$green, b = g5$blue)))
g6 = g6[order(g6$h),]
# g6 is now data frame with unique items arranged by hue

# this is a plot that includes a legend with the colors and item names
g7 = ggplot(g6, aes(x = x, y = y, color = Color)) +
  geom_point(size = 5.8, pch = 15) +
  scale_color_identity(guide = "legend", breaks = g6$Color, labels = g6$Item) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_blank(),
        legend.background = element_rect(colour = NA),
        legend.key = element_rect(colour = "white", fill = NA),
        axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        axis.title.y=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank())

# extract and plot the legend only
legend <- cowplot::get_legend(g7)
grid.newpage()
grid.draw(legend)


# Now, plot the foods in order purchased (use g2, data frame with all items in order)
ggplot(g2, aes(x = x, y = y)) +
  # select point size to fill a grid, make points square
  geom_point(size = 5.8, pch = 15, colour = g2$Color) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
      panel.background = element_blank(), axis.line = element_blank(),
      axis.title.x=element_blank(),
      axis.text.x=element_blank(),
      axis.ticks.x=element_blank(),
      axis.title.y=element_blank(),
      axis.text.y=element_blank(),
      axis.ticks.y=element_blank())

##### Rearrange the foods by hue ###########
# save just the columns with item and hue from g6
g6 = g6[,c(1,9)]
# merge hue into g2
g2 = left_join(g2, g6, by = "Item")
# order g2 by hue
g2 = g2[order(g2$h),]

# create new data frame with x and y values (will be location on grid)
# ordered by distance from top left corner
i = as.data.frame(1:nrow(g2))
colnames(i)[1] = "index"
# start with the valus of x and y that we need to create the full grid
i$xs = ((i$index-1) %% 22)
i$ys = 22-((i$index-1) %/% 22)
# this is the distance from each point to the top left corner
i$sumxy = i$xs^2 + (22-i$ys)^2
# now, order the xs and ys by distance from top left orner
i = i[order(i$sumxy),]

# now bind the x and y values with g2 to get location of each item in grid, 
# arranged by hue (because xs and ys are arranged in order from top left corner, and 
# items in g2 are arranged by hue)
g2 = cbind(g2, i)

# plot the items in the appropriate locations, arranged by hue
ggplot(g2, aes(x = xs, y = ys)) +
  geom_point(size = 5.8, pch = 15, colour = g2$Color) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_blank(),
        axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        axis.title.y=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank())

