ggplot:仅在底部图形上堆叠带有x轴标签的图形

时间:2019-06-20 15:04:38

标签: r ggplot2

我有一个这样的数据框:

ID <- c("A1", "A2", "A3", "A4", "A5", "A6")
group <- c("Cats", "Cats", "Cats", "Dogs", "Dogs", "Dogs")
value <- c(5, 10, 20, 5, 15, 30)

data <- data.frame(ID, group, value)

当我使用此代码绘制图形时

ggplot(data, aes(group, value, color=group)) + geom_jitter(show.legend = FALSE) + labs(x = " ") +
  theme_bw(base_size=20) + theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1)) 

我得到这个结果

enter image description here

我有几个要一起绘制的数据框,但是当我使用grid.arrange时,得到的结果在两个图上都标有x轴:

enter image description here

由于x轴相同,因此我所需的输出仅在底部图形上标记了x轴,如下所示:

enter image description here

在ggplot中这可能吗?

1 个答案:

答案 0 :(得分:1)

也许您可以将数据帧绑定在一起,然后使用facet_grid

ID <- c("A1", "A2", "A3", "A4", "A5", "A6")
group <- c("Cats", "Cats", "Cats", "Dogs", "Dogs", "Dogs")
value <- c(5, 10, 20, 5, 15, 30)
value2 <- c(8, 13, 23, 8, 18, 50)

data <- data.frame(ID, group, value, whichdf = "1")
data2 <-data.frame(ID, group, value = value2, whichdf = "2") 

df <- rbind(data,data2)

ggplot()

ggplot(df, aes(group, value, color=group)) + 
  facet_grid(vars(whichdf)) +
  geom_jitter(show.legend = FALSE) + 
  labs(x = " ") +
  theme_bw(base_size=20) + 
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1))

哪个产量

enter image description here

如果y轴值相差很大,则可以在scales = "free"中指定facet_grid,以使每个图都有自己的y轴比例。

相关问题