【问题标题】:Why does stacked barplot ggplot in R sum values?为什么R sum values中的堆叠barplot ggplot?
【发布时间】:2022-01-04 22:26:12
【问题描述】:

当我进行以下操作时,我只想比较三年而不是求和。即使我没有告诉它这样做,实际的代码也会对它们进行求和。有什么线索吗?

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

【问题讨论】:

  • 您绘制了堆积条形图,但不想将这些值相加?目前还不清楚您还希望发生什么——也许您实际上并不是要堆叠它们?

标签: r ggplot2


【解决方案1】:

你需要这样的东西吗:

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)

【讨论】:

    猜你喜欢
    • 2018-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-04
    • 2017-03-21
    • 2020-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多