r - R ggplot2 - 在所有面的顶部和右侧添加刻度
问题描述
我正在使用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
。
基本上,我需要的是:
- 在所有面的上轴和右轴上添加刻度(内部或外部);
- 仅将 x 轴标签保留在底部,将 y 轴标签仅保留在刻面的左侧。
关于如何到达那里的任何提示?
解决方案
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 hacking 就可以达到相同的结果,但这种方法相当复杂,所以我不会去那里,除非在计算环境中安装新包真的很麻烦......)
推荐阅读
- linux - 如何防止cmake剥离创建的共享库
- excel - 如果另一张纸上的值大于/小于任一值,则在 Excel 中突出显示 2 个单元格
- origen-sdk - Origen.load_target 是否允许增加初始化选项?
- spring-boot - Minikube VM hyperkit:Spring Boot:连接到本地机器
- python - 没有输出到文件
- sql - 如何在 SQL Server 的表中创建简单的索引列?
- java - 如何在java中单击下拉切换ng-binding selenium click元素
- java - 更改数据库后出现休眠错误“您的 SQL 语法有错误”
- javascript - 复选框启用/禁用 DIV - Localstorage - Onload 加载状态
- python - 如何使用 boto3 在 s3 上的文件夹中迭代?