我想画一个并排图,但我不知道如何指定共同的 x 和 y 轴,请参见下图。另外,我不知道如何调整条形图末尾的值(计数)。另外,如何将 x 轴固定为 300 个计数?
我从这里找到了一个使用 R(但不是 ggplot)的示例,谁能给我一些关于使用 ggplot 执行此操作的提示?谢谢!
示例数据:
df1<-structure(list(Day = structure(c(5L, 1L, 6L, 7L, 4L, 2L), .Label = c("Monday",
"Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"
), class = "factor"), counts = c(178L, 55L, 131L, 116L, 164L,
96L)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"
))
df2<-structure(list(Day = structure(c(5L, 1L, 6L, 7L, 4L, 2L), .Label = c("Monday",
"Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"
), class = "factor"), counts = c(244L, 120L, 264L, 198L, 223L,
163L)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"
))
使用的代码:
rm(list = ls())
library(data.table)
library(reshape2)
library(ggplot2)
library(dplyr)
library(gridExtra)
Less <- read_dta("Data/Less.dta")
More <- read_dta("Data/More.dta")
df1 <- Less%>%
group_by(Day) %>%
summarise(counts = n())
df1
df1$Day <- factor(df1$Day,levels = c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"))
g1<-ggplot(df1, aes(x = df1$Day, y = counts)) +
geom_bar(fill = "#0073C2FF", stat = "identity") +
geom_text(aes(label = counts), vjust = -0.3) +
labs(x="Day of the week", y ="Count", fill="", title="Time") +
theme(plot.title = element_text(hjust = 0.5, size=15))+ theme(axis.text.x = element_text(angle = ,family="Times", face="bold", size=15), axis.title.y = element_text(size = 15),axis.text.y = element_text(size=15, face="bold")) + theme(axis.title.y = element_text(size = 15)) + theme(axis.title.x = element_text(size = 15),plot.margin = unit(c(1,0,1,0), "mm")) +
theme(legend.position = "bottom", legend.key.width = unit(8, "cm"), legend.text=element_text(size=15))+
scale_y_reverse()+coord_flip()
g1
###############
df2 <- More %>%
group_by(Day) %>%
summarise(counts = n())
df2
df2$Day <- factor(df2$Day,levels = c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"))
g2<-ggplot(df2, aes(x = df2$Day, y = counts)) +
geom_bar(fill = "#0073C2FF", stat = "identity") +
geom_text(aes(label = counts), vjust = -0.3) +
labs(x="Day of the week", y ="Count", fill="", title="Time") +
theme(plot.title = element_text(hjust = 0.5, size=15))+ theme(axis.text.x = element_text(angle = ,family="Times", face="bold", size=15), axis.title.y = element_text(size = 15),axis.text.y = element_text(size=15, face="bold")) + theme(axis.title.y = element_text(size = 15)) + theme(axis.title.x = element_text(size = 15), plot.margin = unit(c(1,5,1,0), "mm")) +
theme(legend.position = "bottom", legend.key.width = unit(8, "cm"), legend.text=element_text(size=15))+
coord_flip()
g2
grid.arrange(g1, g2,
widths=c(0.4, 0.4), ncol=2)
答案 0 :(得分:2)
这看起来似乎 facets 可以完成这项工作,但困难在于让第一个 x 轴反向,而另一个则不是。一种选择是使用 ggh4x::facetted_pos_scales()
来做轴(免责声明我是 ggh4x 的作者)。
在下面的示例中,假设 df1
和 df2
是您的示例数据。为简洁起见,我保留的代码比您的示例更简洁。
library(ggplot2)
library(ggh4x)
# Combine data.frames
all_df <- rbind(cbind(df1, facet = "Left"),
cbind(df2, facet = "Right"))
ggplot(all_df, aes(counts, forcats::fct_rev(Day))) +
geom_col() +
geom_text(aes(label = counts),
nudge_x = 15) +
facet_wrap(~ facet, scales = "free_x") +
facetted_pos_scales(x = list(
scale_x_reverse(limits = c(300, 0)),
scale_x_continuous(limits = c(0, 300))
))