1

Hello I am wokring with hotel bookings data and ahve created a new column dubbed "total_guests" this gives me the size of each party that stays at a hotel. I am on to making bar charts for each months data and want to see the total traffic per day. However when I create it it gives me the total occurences of each group. I want to know the total people per day what funciton would I sue insdie ggplot to figure this out.

My code i am currently running is

ggplot(data = August_2015_mydg) +
+ geom_bar(mapping = aes(x = total_guests)) +
+ facet_wrap(~arrival_date_day_of_month)

Here is the data https://docs.google.com/spreadsheets/d/15Pfdn8plXDW2meysOgeubFCe4AY08M0xHZHNAtI8JPM/edit?usp=sharing

  • Welcome to StackOverflow. Instead of posting a GoogleDrive link, please make your example [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by providing your data using `dput(August_2015_mydg)` or by using a built-in dataset (e.g., `airquality`). – jrcalabrese Dec 26 '22 at 19:53

1 Answers1

0

Since it looks like your data only contains August for arrival month, it would make sense to have one bar chart (i.e., not facet wrapped) with each bar representing one day in August. You can achieve this by summarizing your data and summing all the guests for each day in August.

library(tidyverse)
set.seed(123)
total_guests <- sample(1:5, 100, replace = T)
arrival_date_day_of_month <- sample(1:31, 100, replace = T)
August_2015_mydg <- data.frame(total_guests, arrival_date_day_of_month)

# old plot
# ggplot(August_2015_mydg) +
  # geom_bar(mapping = aes(x = total_guests)) +
  # facet_wrap(~arrival_date_day_of_month)

new <- August_2015_mydg %>%
  group_by(arrival_date_day_of_month) %>%
  summarize(total_guests_per_day = sum(total_guests))

ggplot(new, aes(x = arrival_date_day_of_month, y = total_guests_per_day)) +
  geom_bar(stat = "identity")

enter image description here

EDIT

If you want every value on the x-axis to be shown, one option is to turn arrival_date_day_of_month into a factor.

ggplot(new, aes(x = as.factor(arrival_date_day_of_month), y = total_guests_per_day)) +
  geom_bar(stat = "identity")

enter image description here

jrcalabrese
  • 2,184
  • 3
  • 10
  • 30
  • Thank you! I was curious is there a way to make the steps on the x axis be each day? as in 1,2,3,4,5 etc instead of 0,10,20,30 – user19587849 Jan 06 '23 at 20:39
  • Yes, you can do so by turning your x-axis variable into a factor (see updated question), although the x-axis becomes a little cramped. – jrcalabrese Jan 06 '23 at 20:47