我有一个这样的数据框:
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))
我得到这个结果
我有几个要一起绘制的数据框,但是当我使用grid.arrange
时,得到的结果在两个图上都标有x轴:
由于x轴相同,因此我所需的输出仅在底部图形上标记了x轴,如下所示:
在ggplot中这可能吗?
答案 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))
哪个产量
如果y轴值相差很大,则可以在scales = "free"
中指定facet_grid
,以使每个图都有自己的y轴比例。