删除ggplot2的x轴标签?

时间:2018-06-13 11:43:00

标签: r ggplot2

提供以下数据:

    data2 <- list(structure(list(super_group = c(1,1), Group.1 = structure(3:4, .Label = c("A","B", "C", "D", "E", "F"), class = "factor"), Pr1 = c(65, 75), Pr2 = c(54, 88), Prh = c(25, 5), SE = c(25, 75 )), row.names = c(NA, -2L), class = "data.frame"), NULL, structure(list(super_group = c(3,3), Group.1 = structure(3:4, .Label = c("A","B", "C", "D", "E", "F"), class = "factor"), Pr1 = c(81,4), Pr2 = c(66, 57),Prh = c(3,3), SE = c(8, 9)), row.names = c(NA,
-2L), class = "data.frame"))

使用ggplot2绘图:

 data2 %>%
 bind_rows() %>%
 ggplot(., aes(x = Group.1, y = Pr1, fill = Group.1)) +
 geom_bar(stat = "identity") +
 facet_grid(. ~ super_group)

- 你看到x轴上的C和D标签是不必要的。所以我想删除它们并减少条的宽度。任何想法?

- 我们可以将1和3从x轴顶部移动到x轴底部吗?

1 个答案:

答案 0 :(得分:3)

我们可以使用<li routerLinkActive="active"> <a [routerLink]="['/settings']">Contact Settings</a> <ul class="child-link-sub"> <li> <a href="#contactTypes">Contact Types</a> </li> </ul> </li> 参数<a [routerLink]="['/settings/']" fragment="contactTypes" >Contact Types</a> 来更改构面标签的位置 参数switchfacet_grid控制文本并在x轴上打勾。要删除它们,请将它们声明为axis.text.x

axis.ticks.x

enter image description here

要更改图表的比例,请使用不同尺寸保存。例如:

element_blank()

或者,如果使用library(tidyverse) data2 %>% bind_rows() %>% ggplot(., aes(x = Group.1, y = Pr1, fill = Group.1)) + geom_bar(stat = "identity") + facet_grid(. ~ super_group, switch = "x") + theme(axis.text.x = element_blank(), axis.ticks.x = element_blank()) -> p1 通过定义:

来更改块选项 例如

ggsave("p1.pdf", plot = p1, device = "pdf", width = 3, height = 5)

knitr

并更改条的宽度使用参数fig.width=3, fig.height=5

```{r p1, fig.width=3, fig.height=5}  
data2 %>%
      bind_rows() %>%
      ggplot(., aes(x = Group.1, y = Pr1, fill = Group.1)) +
      geom_bar(stat = "identity") +
      facet_grid(. ~ super_group, switch = "x") +
      theme(axis.text.x = element_blank(),
            axis.ticks.x = element_blank())
``` 

enter image description here

另一种选择(基于我认为这是期望的评论)是改变width

data2 %>%
  bind_rows() %>%
  ggplot(., aes(x = Group.1, y = Pr1, fill = Group.1)) +
  geom_bar(stat = "identity", width = 0.5) +
  facet_grid(. ~ super_group, switch = "x") +
  theme(axis.text.x = element_blank(),
        axis.ticks.x = element_blank())

enter image description here

相关问题