我正在使用ggplot2
创建以下图:
library(ggplot2)
df <- data.frame(site=rep(c("CA-Oas","US-Oho","CA-Obs","CA-TP1"), each=124),
exper=rep(c("bberry","medlyn"), each=62, 4),
sm=runif(496, min=0, max=100),
lh=runif(496, min=0, max=400),
month=rep(c("Jul","Aug"), each=31, 8))
ggplot(df, aes(x=sm, y=lh, colour=exper, shape=month)) +
geom_point(size=2.5, alpha=0.9) +
facet_wrap(~site, nrow=2) +
theme_bw(base_size=18) +
ylab(bquote('Latent heat ('*W~m^-2*')')) +
xlab(bquote('Soil water content (%)')) +
scale_color_manual(values=c("bberry"="tomato2", "medlyn"="dodgerblue"),
labels=c("bberry"="Ball-Berry", "medlyn"="Medlyn")) +
theme(panel.grid.minor=element_blank(),
panel.grid.major=element_blank(),
legend.title=element_blank(),
legend.box="horizontal",
legend.position=c(0.4, 0.6),
legend.spacing = unit(-0.2,"cm"))
但是,我写的期刊要求所有图都在两个轴上都带有刻度(上/下和左/右)。我正在努力在ggplot2
上实现这一目标。
基本上,我需要做的是
关于如何到达那里的任何提示?
答案 0 :(得分:3)
facet_rep_wrap
非常适合此操作,因为它可以在所有面上保留轴线(以及可选的标签)。
有关代码的其他调整,请参见下面的内联注释:
library(lemon)
ggplot(df, aes(x=sm, y=lh, colour=exper, shape=month)) +
geom_point(size=2.5, alpha=0.9) +
facet_rep_wrap(~site, nrow = 2) + # instead of facet_wrap
theme_bw(base_size=18) +
ylab(bquote('Latent heat ('*W~m^-2*')')) +
xlab(bquote('Soil water content (%)')) +
scale_x_continuous(sec.axis = dup_axis()) + # add duplicated secondary axis to get
scale_y_continuous(sec.axis = dup_axis()) + # axes on top / right sides
scale_color_manual(values=c("bberry"="tomato2", "medlyn"="dodgerblue"),
labels=c("bberry"="Ball-Berry", "medlyn"="Medlyn")) +
theme(panel.grid.minor=element_blank(),
panel.grid.major=element_blank(),
legend.title=element_blank(),
legend.box="horizontal",
legend.position=c(0.4, 0.6),
legend.spacing = unit(-0.2,"cm"),
strip.placement = "outside", # place facet strips outside axis
axis.ticks.length = unit(-2.75, "pt"), # point axis ticks inwards (2.75pt is
# the default axis tick length here)
axis.text.x.top = element_blank(), # do not show top / right axis labels
axis.text.y.right = element_blank(), # for secondary axis
axis.title.x.top = element_blank(), # as above, don't show axis titles for
axis.title.y.right = element_blank()) # secondary axis either
(注意:仅通过ggplot2和一些grob黑客就可以实现相同的结果,但是这种方法相当复杂,因此除非在计算环境中安装新软件包真的很麻烦,否则我不会去那里。 )
答案 1 :(得分:1)
(这会沿顶部和右侧获得刻度线,但不是针对所有构面。在facet_wrap调用中添加scales = "free"
会为所有构面添加刻度,但是也会为所有构面添加标签,这是冗余。)
ggplot(df, aes(x=sm, y=lh, colour=exper, shape=month)) +
geom_point(size=2.5, alpha=0.9) +
facet_wrap(~site, nrow=2) +
theme_bw(base_size=18) +
ylab(bquote('Latent heat ('*W~m^-2*')')) +
xlab(bquote('Soil water content (%)')) +
scale_color_manual(values=c("bberry"="tomato2", "medlyn"="dodgerblue"),
labels=c("bberry"="Ball-Berry", "medlyn"="Medlyn")) +
scale_x_continuous(sec.axis = dup_axis(labels = NULL)) + # NEW
scale_y_continuous(sec.axis = dup_axis(labels = NULL)) + # NEW
theme(panel.grid.minor=element_blank(),
panel.grid.major=element_blank(),
strip.placement = "outside", # NEW
legend.title=element_blank(),
legend.box="horizontal",
legend.position=c(0.4, 0.6),
legend.spacing = unit(-0.2,"cm"))