在分面上对选择变量进行分组

时间:2018-03-28 12:16:32

标签: r ggplot2

给出如下数据框df

text <- "
CAR_MODEL,ENGINE_VENDOR,var,value,label
Toyota Corolla,FIAT,Three_Family_Pct,33.98,33.98
Nissan Sunny,PRATNEY,Three_Family_Pct,29.84,29.84
Renault Duster,FIAT,Three_Family_Pct,27.86,27.86
Suzuki Ciaz,FIAT,Three_Family_Pct,26.6,26.6
Renault Duster,FIAT,Single_Family_Pct,0,0
Toyota Corolla,FIAT,Single_Family_Pct,0,0
Nissan Sunny,PRATNEY,Single_Family_Pct,0,0
Suzuki Ciaz,FIAT,Single_Family_Pct,0,0
Suzuki Ciaz,FIAT,Two_Family_Pct,42.37,42.37
Renault Duster,FIAT,Two_Family_Pct,41.53,41.53
Toyota Corolla,FIAT,Two_Family_Pct,36.31,36.31
Nissan Sunny,PRATNEY,Two_Family_Pct,32.27,32.27
Nissan Sunny,PRATNEY,Four_Family_Pct,37.89,37.89
Suzuki Ciaz,FIAT,Four_Family_Pct,31.03,31.03
Renault Duster,FIAT,Four_Family_Pct,30.61,30.61
Toyota Corolla,FIAT,Four_Family_Pct,29.71,29.71
Nissan Sunny,PRATNEY,Mileage,12688.5,12688
Suzuki Ciaz,FIAT,Mileage,11989,11989
Renault Duster,FIAT,Mileage,11132.5,11132
Toyota Corolla,FIAT,Mileage,10357,10357
"
df <- read.table(textConnection(text), sep=",", header = T)

我可以将var绘制成单个图表,方法是var,如下所示。

library(ggplot2)
ggplot(data=df,
              aes(x=CAR_MODEL, y=value, fill=ENGINE_VENDOR, label = label)
              ) +
    geom_bar( stat = "identity", position='dodge') + 
    geom_text(position = position_dodge(width = 1),
              vjust = 1, angle = 0, size = 3 ) +
    # facet_grid( var ~ ., scales = "free") + 
    facet_wrap(~ var, scales = "free_y", ncol=1) + 
    theme_custom_col +
    scale_fill_brewer(palette = "Paired") + 
    scale_color_brewer(palette = "Paired") +
    theme(
      text = element_text(size=ggplotAxesLabelSize),
      axis.text.x=element_text(angle = 30),
      legend.position="top",
      axis.text.y=element_blank()
      ) +
    labs( y = "")

enter image description here

我得到一张上面的图表。但是我想在图表中进行两处更改。

  1. 需要mileage作为最底部的情节
  2. var s Four_Family_PctThree_Family_PctTwo_Family_PctSingle_Family_Pct - 需要绘制为群集条形图,因为它们属于同一个逻辑属性 - 它们显示家庭类型的百分比
  3. 我应该怎么做?

    @PoGibas - 添加了一个表示以供参考(以excel制作) - X轴标签需要在那里。

    enter image description here

2 个答案:

答案 0 :(得分:1)

这样的事情?

library(tidyverse)
p1 <- df %>% 
  mutate(var2=ifelse(var =="Mileage", "Mileage","Family types")) %>% 
  mutate(var=factor(var, levels = levels(df$var)[c(1,3:5,2)])) %>% 
  mutate(label2=ifelse(var =="Mileage", scales::comma(label,digits = 0),paste0(label,"%"))) %>% 
  ggplot(aes(x=CAR_MODEL, y=value, fill=var, label = label2))+ 
     geom_col(aes(col=ENGINE_VENDOR),position = position_dodge(width = 0.9), size=1.2) +
     geom_label(position = position_dodge(width = 0.9) ,show.legend = F)+
  scale_color_manual(values = c(1,2))+
  theme(legend.position="top")
# and the plot using wrap
p1 + facet_wrap( ~ var2, scales = "free_y", ncol = 1) 

enter image description here

# and grid
p1 + facet_grid(var2 ~ ENGINE_VENDOR , scales = "free", space = "free_x") 

enter image description here

对于网格方法,我会删除aes(col=ENGINE_VENDOR)参数。

答案 1 :(得分:1)

不是一个完美的解决方案,但会显示所有想要的信息:

library(ggplot2)

ggplot(df, aes(CAR_MODEL, value)) +
    geom_rect(aes(xmin = CAR_MODEL, xmax = CAR_MODEL,
                  ymin = -Inf, ymax = Inf,
                  color = ENGINE_VENDOR),
                  size = 60) +
    geom_bar(data = subset(df, var != "Mileage"),
             aes(fill = var),
             stat = "identity", position = "dodge", width = 0.6) +
    geom_bar(data = subset(df, var == "Mileage"),
             stat = "identity", width = 0.2, fill = "grey20") +
    facet_wrap(~ var == "Mileage", ncol = 1, scales = "free") +
    labs(x = NULL,
         y = NULL,
         fill = NULL,
         color = NULL) +
    scale_fill_brewer(palette = "Accent") +
    scale_color_brewer(palette = "Paired") +
    theme_classic() +
    theme(strip.background = element_blank(),
          strip.text.x = element_blank(),
          axis.ticks.x = element_blank(),
          legend.position = "top") +
    guides(colour = guide_legend(override.aes = list(fill = brewer.pal(2, "Paired")[1:2], 
                                                     size = 1)))

enter image description here

使用facet_wrap获取里程/非里程信息,使用geom_rect获取引擎信息。