首页 > 解决方案 > 如何绘制具有 x 轴非数字的三个变量的条形图?

问题描述

PREMIUM_CUSTOMER    LIFESTAGE              Total Sales
Budget              OLDER FAMILIES          156863.75
Mainstream          YOUNG SINGLES/COUPLES   147582.20
Mainstream          RETIREES                145168.95
Budget              YOUNG FAMILIES          129717.95
Budget              OLDER SINGLES/COUPLES   127833.60
Mainstream          OLDER SINGLES/COUPLES   124648.50
Premium             OLDER SINGLES/COUPLES   123537.55
Budget              RETIREES                105916.30
Mainstream          OLDER FAMILIES          96413.55
Premium             RETIREES                91296.65
Mainstream          YOUNG FAMILIES          86338.25
Mainstream          MIDAGE SINGLES/COUPLES  84734.25
Premium             YOUNG FAMILIES          78571.70
Premium             OLDER FAMILIES          75242.60
Budget              YOUNG SINGLES/COUPLES   57122.10
Premium             MIDAGE SINGLES/COUPLES  54443.85
Premium             YOUNG SINGLES/COUPLES   39052.30
Budget              MIDAGE SINGLES/COUPLES  33345.70
Budget              NEW FAMILIES            20607.45
Mainstream          NEW FAMILIES            15979.70
Premium             NEW FAMILIES            10760.80

下面的代码没有给我想要的结果

ggplot(Total_Sales, aes(x = LIFESTAGE, fill = PREMIUM_CUSTOMER)) +
  geom_bar(position = "fill") +  labs(x = "Lifestage", y = "Premium customer flag", title = "Proportion of sales") + 
  theme(axis.text.x = element_text(angle = 45, vjust = 0.5)) 

我希望 x 轴是LIFESTAGE,Y 轴是 Total Sales 并且条形图用PREMIUM CUSTOMERand填充Total sales

标签: rggplot2

解决方案


这是一个小代表,可能会对您有所帮助。

library(tidyverse)
library(scales)
#> 
#> Attache Paket: 'scales'
#> The following object is masked from 'package:purrr':
#> 
#>     discard
#> The following object is masked from 'package:readr':
#> 
#>     col_factor

df <- structure(list(
  PREMIUM_CUSTOMER = c("Budget", "Mainstream", "Mainstream", "Budget", 
                       "Budget", "Mainstream", "Premium", "Budget", "Mainstream",
                       "Premium", "Mainstream", "MIDAGE", "Premium", "Premium", 
                       "Budget", "Premium", "Premium", "Budget", "Budget", 
                       "Mainstream", "Premium"
                       ), 
  LIFESTAGE = c("OLDER FAMILIES", "YOUNG SINGLES/COUPLES", "RETIREES", 
                "YOUNG FAMILIES", "OLDER SINGLES/COUPLES", 
                "OLDER SINGLES/COUPLES", "OLDER SINGLES/COUPLES", "RETIREES",
                "OLDER FAMILIES", "RETIREES", "YOUNG FAMILIES", 
                "SINGLES/COUPLES", "YOUNG FAMILIES", "OLDER FAMILIES", 
                "YOUNG SINGLES/COUPLES", "MIDAGE SINGLES/COUPLES", 
                "YOUNG SINGLES/COUPLES", "MIDAGE SINGLES/COUPLES", "NEW FAMILIES",
                "NEW FAMILIES", "NEW FAMILIES"),
  `Total Sales` = c("156863.75", "147582.20", "145168.95", "129717.95", 
                    "127833.60", "124648.50", "123537.55", "105916.30", 
                    "96413.55",  "91296.65", "86338.25", "84734.25", "78571.70",
                    "75242.60", "57122.10", "54443.85", "39052.30", "33345.70",
                    "20607.45", "15979.70", "10760.80")), 
  row.names = c(NA, -21L), 
  class = c("tbl_df", "tbl", "data.frame"
))

df <- df %>% 
  mutate(`Total Sales` = as.numeric(`Total Sales`),
         across(where(is.character), factor))

df %>% 
  ggplot(aes(x = LIFESTAGE, y = `Total Sales`, fill = PREMIUM_CUSTOMER)) +
  geom_col() + 
  scale_y_continuous(labels = comma) + 
  labs(x = "Lifestage",
       y = "Premium customer flag", 
       title = "Proportion of sales") +
  coord_flip()  

reprex 包于 2021-09-11 创建(v2.0.1)

如果您需要更改情节,您可以随意更改。factor在绘图之前将数据类型更改为很重要。您还应该检查在函数的各个参数中填充正确的列aes()

问候,M。


推荐阅读