Veliano Tembo
Veliano Tembo

Reputation: 11

Values not showing on the facets in R

Ok, so I wanted to display the amount of calorie distribution in facet, but when I have the following code, it does not show the values. The facet shows but now values. what could be the problem. this analysing the Macdonald data set in R.

menu <- read.csv("mac_menu_processed.csv", header=T)%>% menu %>%
  gather(Nutritions, Value, Total.Fat:Iron.DV, factor_key=TRUE) %>%
  filter(grepl("DV", Nutritions)) %>%
  ggplot(menu,mapping = aes(x= menu$Categories, y= menu$Value)) + 
    facet_wrap(~ Nutritions, nrow=4) +
    coord_flip()

Upvotes: 1

Views: 31

Answers (2)

jared_mamrot
jared_mamrot

Reputation: 26695

Looks like you have some typos; does this provide your expected output?

library(tidyr)
library(ggplot2)

menu <- read.csv("mac_menu_processed.csv", header=T)
head(menu)

menu %>%
  gather(Nutritions, Value, Total.Fat:Iron.DV, factor_key=TRUE) %>%
  filter(grepl("DV", Nutritions)) %>%
  ggplot(aes(x = Categories, y = Value)) +
    geom_point() +
    coord_flip() +
    facet_wrap(~ Nutritions, nrow = 4)

Edit

I don't have "mac_menu_processed.csv", but here is how I would approach it using the raw data:

library(tidyverse)
# Dataset downloaded from https://www.kaggle.com/mcdonalds/nutrition-facts
menu <- read_csv("~/Desktop/menu.csv")
#> Rows: 260 Columns: 24
#> ── Column specification ────────────────────────────────────────────────────────
#> Delimiter: ","
#> chr  (3): Category, Item, Serving Size
#> dbl (21): Calories, Calories from Fat, Total Fat, Total Fat (% Daily Value),...
#> 
#> ℹ Use `spec()` to retrieve the full column specification for this data.
#> ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

menu %>%
  pivot_longer(-c(Calories, Category, Item, `Serving Size`),
               names_to = "Nutritions",
               values_to = "Value") %>%
  filter(str_detect(Nutritions, "% Daily Value")) %>%
  ggplot(aes(x = Category, y = Value)) +
  geom_col() +
  coord_flip() +
  facet_wrap(~ Nutritions, nrow = 4)

Created on 2022-02-04 by the reprex package (v2.0.1)

Upvotes: 2

TarJae
TarJae

Reputation: 79194

Try this: You are missing the geom?

ggplot(menu,mapping = aes(x= Categories, y= Value)) + 
  geom_col()+
  facet_wrap(~ Nutritions, nrow=4) + coord_flip()

Upvotes: 2

Related Questions