ggplot2,facet wrap,每行固定 y 刻度,行间自由刻度
ggplot2, facet wrap, fixed y scale for each row, free scale between rows
我想使用 facet_wrap 生成一个图,该图的每行换行都有不同的 y 比例。换句话说,同一行上有固定刻度,不同行上有自由刻度,具有固定的 x 刻度。 Free scales 不能准确地给出我正在寻找的东西,facet_grid 也不能。如果可能的话,我想避免创建 2 个单独的图然后将它们粘贴在一起。我正在寻找如下图所示的结果,但第一行的 y 比例最大值为 300,第二行的 y 比例最大值为 50。感谢您的帮助!
这是我的代码:
library(ggplot2)
library(reshape)
# set up data frame
dat <- data.frame(jack = c(150,160,170),
surgeon = c(155,265,175),
snapper = c(10,15,12),
grouper = c(5,12,50))
dat$island<-c("Oahu","Hawaii","Maui")
df<-melt(dat)
# plot
ggplot(df, aes(fill=variable, y=value, x=island)) +
geom_bar(width = 0.85, position= position_dodge(width=0.5),stat="identity", colour="black") +
facet_wrap(~variable, scales = "free_y",ncol=2) +
theme_bw() +
theme(strip.text = element_text(size=15, face="bold"))+
theme(legend.position="none")+
theme(panel.grid.major = element_line(colour = "white", size = 0.2))+
theme(panel.grid.minor = element_line(colour = "white", size = 0.5))+
theme(axis.text.x = element_text(angle = 90, hjust =1, vjust =0.5, size=18))+
labs(y = expression(paste("Yearly catch (kg)")))
借鉴 link Eric 评论的 lower ranked answers 之一,您可以添加一个图层以融入背景以加强坐标轴。
我在这里创建了第二个数据框 (df2),它在 "Hawaii" 处放置了一个点,并为四种 variable/fish 类型放置了您想要的最大值(300 或 50)。通过手动设置 geom_point 白色的颜色,它会淡入背景。
library(ggplot2)
library(reshape)
# set up data frame
dat <- data.frame(jack = c(150,160,170),
surgeon = c(155,265,175),
snapper = c(10,15,12),
grouper = c(5,12,50))
dat$island<-c("Oahu","Hawaii","Maui")
df<-melt(dat)
#> Using island as id variables
df2 <- data.frame(island = rep("Hawaii",4), variable = c("jack","surgeon","snapper","grouper"),value = c(300,300,50,50))
ggplot(df, aes(fill=variable, y=value, x=island)) +
geom_bar(width = 0.85, position= position_dodge(width=0.5),stat="identity", colour="black") +
geom_point(data = df2, aes(x = island, y = value), colour = "white") +
facet_wrap(~variable, scales = "free_y",ncol=2) +
theme_bw() +
theme(strip.text = element_text(size=15, face="bold"))+
theme(legend.position="none")+
theme(panel.grid.major = element_line(colour = "white", size = 0.2))+
theme(panel.grid.minor = element_line(colour = "white", size = 0.5))+
theme(axis.text.x = element_text(angle = 90, hjust =1, vjust =0.5, size=18))+
labs(y = expression(paste("Yearly catch (kg)")))
我想使用 facet_wrap 生成一个图,该图的每行换行都有不同的 y 比例。换句话说,同一行上有固定刻度,不同行上有自由刻度,具有固定的 x 刻度。 Free scales 不能准确地给出我正在寻找的东西,facet_grid 也不能。如果可能的话,我想避免创建 2 个单独的图然后将它们粘贴在一起。我正在寻找如下图所示的结果,但第一行的 y 比例最大值为 300,第二行的 y 比例最大值为 50。感谢您的帮助!
这是我的代码:
library(ggplot2)
library(reshape)
# set up data frame
dat <- data.frame(jack = c(150,160,170),
surgeon = c(155,265,175),
snapper = c(10,15,12),
grouper = c(5,12,50))
dat$island<-c("Oahu","Hawaii","Maui")
df<-melt(dat)
# plot
ggplot(df, aes(fill=variable, y=value, x=island)) +
geom_bar(width = 0.85, position= position_dodge(width=0.5),stat="identity", colour="black") +
facet_wrap(~variable, scales = "free_y",ncol=2) +
theme_bw() +
theme(strip.text = element_text(size=15, face="bold"))+
theme(legend.position="none")+
theme(panel.grid.major = element_line(colour = "white", size = 0.2))+
theme(panel.grid.minor = element_line(colour = "white", size = 0.5))+
theme(axis.text.x = element_text(angle = 90, hjust =1, vjust =0.5, size=18))+
labs(y = expression(paste("Yearly catch (kg)")))
借鉴 link Eric 评论的 lower ranked answers 之一,您可以添加一个图层以融入背景以加强坐标轴。
我在这里创建了第二个数据框 (df2),它在 "Hawaii" 处放置了一个点,并为四种 variable/fish 类型放置了您想要的最大值(300 或 50)。通过手动设置 geom_point 白色的颜色,它会淡入背景。
library(ggplot2)
library(reshape)
# set up data frame
dat <- data.frame(jack = c(150,160,170),
surgeon = c(155,265,175),
snapper = c(10,15,12),
grouper = c(5,12,50))
dat$island<-c("Oahu","Hawaii","Maui")
df<-melt(dat)
#> Using island as id variables
df2 <- data.frame(island = rep("Hawaii",4), variable = c("jack","surgeon","snapper","grouper"),value = c(300,300,50,50))
ggplot(df, aes(fill=variable, y=value, x=island)) +
geom_bar(width = 0.85, position= position_dodge(width=0.5),stat="identity", colour="black") +
geom_point(data = df2, aes(x = island, y = value), colour = "white") +
facet_wrap(~variable, scales = "free_y",ncol=2) +
theme_bw() +
theme(strip.text = element_text(size=15, face="bold"))+
theme(legend.position="none")+
theme(panel.grid.major = element_line(colour = "white", size = 0.2))+
theme(panel.grid.minor = element_line(colour = "white", size = 0.5))+
theme(axis.text.x = element_text(angle = 90, hjust =1, vjust =0.5, size=18))+
labs(y = expression(paste("Yearly catch (kg)")))