1
ggplot(data = hotel_bookings) +
  geom_bar(mapping = aes(x = hotel, fill = market_segment)) + 
  geom_text(aes(label = "Online TA", count, ""),y = 5)

Error: Aesthetics must be valid data columns. Problematic aesthetic(s): x = count. Did you mistype the name of a data column or forget to add after_stat()? Run rlang::last_error() to see where the error occurred.

I've tried about 30 different ways to do this and I can't seem to get a label inside the stacked bar graph.

ggplot(data = hotel_bookings) + geom_bar(mapping = aes(x = hotel, fill = market_segment))

This is the bar graph I was working with, and I thought I'd be able to easily add the "count" quantities to the bars. With the stacked look of it, it's hard to tell what the quantities are and I think it would make analysis easier to have quantities labeled inside the bars. How do I do this? I'm working through the coursera Google Analytics program and, although it's not a part of my current lesson, it would be helpful to know. Thanks for the help in advance.

I have also just tried the code: geom_text(aes(label=count)), and geom_text(aes(label = "count")), as well as geom_text(label='count')...many iterations of this. Nothing seems to add the "count" value into label form. The other puzzling aspect is that I've had an error message saying it needed a value for the y-axis, but in this geom_bar() graph there was no y value, it was the default count value, totaling up the values of the x-axis' values. If that makes sense...just more clarity on my issue. Please help.

DCosta
  • 63
  • 1
  • 1
  • 10
  • The value of `label` should not be quoted. If `Online TA` is a column name, put it in backticks. Other than that, difficult to answer without seeing some example data from `hotel_bookings`. – neilfws Mar 01 '22 at 01:36
  • market_segment is a column name. I've been trying to make this happen and researching how to do this for about an hour so I'm sure this post seems a bit scattered. market_segment is the column, and there are about 8 different values for it. The bar graph has all the market_segment values stacked and differentiated using hotel types in the x-axis, so I wanted to show the values of the stacked items of each bar, their quantities, inside each bar segment. – DCosta Mar 01 '22 at 01:43
  • 2
    [Here's a question demonstrating several answers](https://stackoverflow.com/a/2553324/903061). We can't help more without sample data. If you want more help, please share sample data, e.g., `dput(hotel_bookings[1:10, ])` for the first 10 rows. Choose a suitable subset to illustrate the problem. – Gregor Thomas Mar 01 '22 at 02:14
  • Try `geom_text(aes(label = after_stat(count)), stat = "count", position = position_stack())`. See e.g. [How to add frequency count labels to the bars in a bar graph using ggplot2?](https://stackoverflow.com/questions/26553526/how-to-add-frequency-count-labels-to-the-bars-in-a-bar-graph-using-ggplot2) – stefan Mar 01 '22 at 06:12

1 Answers1

2

One possible solution, with stat_count that computes percentages by group, e.g. by the groups set via the fill aes. These can be accessed via ..prop...

Sample code:

library(readr)
    hotel_data <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-02-11/hotels.csv')  

                                                                                                                                                                      

(ggplot(hotel_data, aes(x = hotel, fill=factor(market_segment))) +
          geom_bar(position = "fill") +
         geom_text(aes(label = ..count..), stat = "count", position = "fill")+
  labs(x="", y="", title="", fill="")+
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5,  face="bold", size=20, color="black")) + 
  theme(axis.title.x = element_text(family="Times", face="bold", size=16, color="black"))+
  theme(axis.title.y = element_text(family="Times", face="bold", size=16, color="black"))+
  theme(axis.text.x = element_text( hjust = 1,  face="bold", size=14, color="black") )+
  theme(axis.text.y = element_text( hjust = 1,  face="bold", size=14, color="black") )+
  theme(plot.title = element_text(hjust = 0.5))+
  theme(legend.title = element_text(family="Times", color = "black", size = 16,face="bold"),
         legend.text = element_text(family="Times", color = "black", size = 14,face="bold"),
        legend.position="bottom",
       plot.title = element_text(hjust = 0.5)))

enter image description here

Or:

(ggplot(hotel_data, aes(x = hotel, fill=factor(market_segment))) +
    geom_bar(position = position_dodge2(width = 0.9))+
    geom_text(aes(label = ..count..), stat = "count", vjust=-0.3, position = position_dodge(width=0.9))+
  labs(x="", y="", title="", fill="")+
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5,  face="bold", size=20, color="black")) + 
  theme(axis.title.x = element_text(family="Times", face="bold", size=16, color="black"))+
  theme(axis.title.y = element_text(family="Times", face="bold", size=16, color="black"))+
  theme(axis.text.x = element_text( hjust = 1,  face="bold", size=14, color="black") )+
  theme(axis.text.y = element_text( hjust = 1,  face="bold", size=14, color="black") )+
  theme(plot.title = element_text(hjust = 0.5))+
  theme(legend.title = element_text(family="Times", color = "black", size = 16,face="bold"),
         legend.text = element_text(family="Times", color = "black", size = 14,face="bold"),
        legend.position="bottom",
       plot.title = element_text(hjust = 0.5)))

enter image description here

Rfanatic
  • 2,224
  • 1
  • 5
  • 21