使用小平面网格的x轴不是ggplot中的aes?

时间:2018-11-01 18:37:37

标签: r ggplot2

我想创建一个ggplot刻面网格,其中网格的x轴(而不是绘图)是标签,而不是挤压每个统计图。

示例:

"require-dev": {
    "barryvdh/laravel-ide-helper": "^2.5",
    "filp/whoops": "^2.0",
    "fzaninotto/faker": "^1.4",
    "laravel/dusk": "^4.0",
    "mockery/mockery": "^1.0",
    "nunomaduro/collision": "^2.0",
    "phpunit/phpunit": "^7.0",
    "squizlabs/php_codesniffer": "3.*"
}

看起来像: enter image description here

我希望网格的水平部分不是每个单独的图表,而是在每个单独的图表上具有3到9。

换句话说,每个条形图应仅是单列条形图。

我该怎么做?

2 个答案:

答案 0 :(得分:2)

使用一个常量作为x值,同时仍然用cyl进行分面:

library(tidyverse)

mtcars %>% 
  group_by(mpg) %>% 
  mutate(cnt = n()) %>% 
  ggplot(aes(x = 1, y = cnt)) +
  geom_bar(stat = "identity") +
  facet_grid(vs ~ cyl) + 
  theme(axis.text.x = element_blank(),
        axis.ticks.x = element_blank()) + 
  labs(x = "cyl")

enter image description here

答案 1 :(得分:1)

喜欢吗?

mtcars %>% 
  group_by(mpg) %>% 
  mutate(cnt = n()) %>% 
  ggplot(aes(x = cyl, y = cnt)) +
  geom_bar(stat = "identity") +
  facet_grid(vs ~ cyl)+
  theme(axis.text.x = element_blank(),axis.ticks = element_blank())

enter image description here

还是这样?

mtcars %>% 
  group_by(mpg) %>% 
  mutate(cnt = n()) %>% 
  ggplot(aes(x = cyl, y = cnt)) +
  geom_bar(stat = "identity") +
  facet_grid(vs ~ cyl, scales="free")+
  theme(axis.text.x = element_blank(),axis.ticks = element_blank())

enter image description here