Tipulin
Tipulin

Reputation: 25

Why does stacked barplot ggplot in R sum values?

When I make the following, I want just to compare the three years not to sum them. And the actual code sums them even if I don't tell it to do it. Any clue?

hoteles <- tibble::tribble(
  ~Hotel          , ~Año, ~Ingresos
  , "Hawaiian Club" , "1993",    450000
  , "Hawaiian Club" , "1994",    475000
  , "Hawaiian Club" , "1995",    390000
  , "French Riviera", "1993",    225000
  , "French Riviera", "1994",    240000
  , "French Riviera", "1995",    205000
  , "Bahamas Beach" , "1993",    245000
  , "Bahamas Beach" , "1994",    255000
  , "Bahamas Beach" , "1995",    345000
)

g <- ggplot(hoteles, aes(fill=Año, y=Ingresos, x=reorder(Hotel, Ingresos))) + 
  geom_bar(position="stack", stat="identity") + 
  ggtitle("Ingresos por año por hotel") +
  labs(y = "Ingresos", x = "Hotel")
g

Graph

Upvotes: 1

Views: 39

Answers (1)

TarJae
TarJae

Reputation: 78917

Do you need something like this:

ggplot(hoteles, aes(fill=Año, y=Ingresos, x=reorder(Hotel, Ingresos))) + 
  geom_col(position="dodge")+
  ggtitle("Ingresos por año por hotel") +
  labs(y = "Ingresos", x = "Hotel")+
  scale_y_continuous(labels = scales::comma)

enter image description here

Upvotes: 1

Related Questions