ggplot2,facet wrap,每行固定的y标度,行间的自由标度 [英] ggplot2, facet wrap, fixed y scale for each row, free scale between rows

查看:214
本文介绍了ggplot2,facet wrap,每行固定的y标度,行间的自由标度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用facet_wrap为每个换行的y行生成一个plot。换句话说,对于同一行上的固定比例,不同行上的自由比例,具有固定的x比例。自由尺度并不能完全给我我要找的东西,也不会facet_grid。如果可能,我想避免创建2个独立的地块,然后将它们粘贴在一起。我正在寻找类似下图的结果,但第一行的y最大值为300,第二行的y最大值为50。

I would like to produce a plot using facet_wrap that has a different y scale for each row of the wrap. In other words, with fixed scales on the same row, free scales on different rows, with a fixed x scale. Free scales doesn't give me exactly what I'm looking for, nor does facet_grid. If possible, I'd like to avoid creating 2 separate plots and then pasting them together. I'm looking for a result like the plot below, but with a y scale max of 300 for the first row, and an y scale max of 50 in the second row. Thanks for any help!

这是我的代码:

Here is my code:

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)")))

推荐答案

绘制其中一个>排名较低的答案,您可以添加一个混合到背景中的图层来执行轴。

Drawing on one of the lower ranked answers from the link Eric commented, you can add a layer that blends into the background to enforce the axes.

在这里,我创建了第二个数据框(df2),将单点放在夏威夷上,并为四种变量/鱼类型设置最大值(300或50)。通过手动设置geom_point white的颜色,它会淡入背景中。

Here I created a second data frame (df2) that puts a single point at "Hawaii" and the max value you wanted (300 or 50) for the four variable/fish types. By manually setting the color of the geom_point white, it fades into the background.

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)")))

这篇关于ggplot2,facet wrap,每行固定的y标度,行间的自由标度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆