R 如何在ggplot2中使用ylab()创建动态轴标签?
我正在ggplot2中进行一系列绘图,每列数据一个,我希望能够对其进行编码,以便Y轴标签动态更改为绘图变量的名称(以及标签中的一些其他常量信息) 我的数据来自R 如何在ggplot2中使用ylab()创建动态轴标签?,r,ggplot2,axis-labels,R,Ggplot2,Axis Labels,我正在ggplot2中进行一系列绘图,每列数据一个,我希望能够对其进行编码,以便Y轴标签动态更改为绘图变量的名称(以及标签中的一些其他常量信息) 我的数据来自dput(): df我想我可以帮你标记出图的ylab,这似乎是你问题的核心。这会产生三个绘图,但我遇到了各种各样的ggplot错误(根本不理解),这需要我拿出一堆与主题和比例相关的代码。后来,我用一种不同的顺序把它放回去,这似乎让ggplot机器很高兴 plotfun <- function(x,y) { a <- gg
dput()
:
df我想我可以帮你标记出图的ylab
,这似乎是你问题的核心。这会产生三个绘图,但我遇到了各种各样的ggplot错误(根本不理解),这需要我拿出一堆与主题和比例相关的代码。后来,我用一种不同的顺序把它放回去,这似乎让ggplot机器很高兴
plotfun <- function(x,y) {
a <- ggplot(data=x, aes(x=station, y=Mean)) +
geom_errorbar(aes(ymin=(Mean-StErr), ymax=(Mean+StErr)),
colour="black", width=0.1)+
geom_point(size=2) +
xlab(NULL) +
ylab( bquote(.(y)~"#"/m^3) )+
scale_y_log10()+
theme_bw()
pdf(file=paste(y,'pdf',sep="."))
print(a)
dev.off()
}
mapply(plotfun, x=split(df, df$Bug), y=levels(df$Bug))
plotfun谢谢,这正是我想要的!你能给我解释一下如何在变量名之后和3/m^3
之前加逗号吗?我试过在逗号前后加上“”和不加“”。使用以下形式:*,“*
。。。在输出中插入(不带引号的)逗号字符。表达式标记需要波浪形、星形或数学符号来分隔它们。还修改了答案。非常感谢,这非常有用,并且与我的数据完美配合!
library(ggplot2)
df$day<-factor(df$day, levels = c("5-Aug","10-Aug","17-Aug")) #corrects day order
allplots <- ggplot(data=df, aes(x=station, y=Mean)) +
geom_errorbar(aes(ymin=(Mean-StErr), ymax=(Mean+StErr)), colour="black", width=0.1)+
geom_point(size=2) +
xlab(NULL) +
ylab(expression(paste('Copepods,'~'#/m'^3))) +
theme_bw() +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()
) +
scale_x_continuous(expand=c(.3,0), breaks=c(1:3), labels=c("In", "FL", "Off")) +
annotation_logticks(sides = "l") +
scale_y_log10() +
theme(axis.text.x=element_text(size=12)) +
theme(axis.text.y=element_text(size=12)) +
facet_grid(Bug ~ day)
plotfun <- function(x,y) {
a <- ggplot(data=x, aes(x=station, y=Mean)) +
geom_errorbar(aes(ymin=(Mean-StErr), ymax=(Mean+StErr)), colour="black", width=0.1)+
geom_point(size=2) +
xlab(NULL) +
ylab(expression(paste('Copepods,'~'#/m'^3))) + #I'd like Copepods to change with variable that is being plotted
theme_bw() +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()
) +
scale_x_continuous(expand=c(.3,0), breaks=c(1:3), labels=c("In", "FL", "Off")) +
annotation_logticks(sides = "l") +
scale_y_log10() +
theme(axis.text.x=element_text(size=12)) +
theme(axis.text.y=element_text(size=12)) +
facet_grid(.~day)
pdf(file=paste(y,'pdf',sep="."))
print(a)
dev.off()
}
mapply(plotfun, split(df, df$Bug), levels(df$Bug))
plotfun <- function(x,y) {
a <- ggplot(data=x, aes(x=station, y=Mean)) +
geom_errorbar(aes(ymin=(Mean-StErr), ymax=(Mean+StErr)),
colour="black", width=0.1)+
geom_point(size=2) +
xlab(NULL) +
ylab( bquote(.(y)~"#"/m^3) )+
scale_y_log10()+
theme_bw()
pdf(file=paste(y,'pdf',sep="."))
print(a)
dev.off()
}
mapply(plotfun, x=split(df, df$Bug), y=levels(df$Bug))
plotfun <- function(x,y) {
a <- ggplot(data=x, aes(x=station, y=Mean)) +
geom_errorbar(aes(ymin=(Mean-StErr), ymax=(Mean+StErr)), colour="black", width=0.1)+
geom_point(size=2) +
xlab(NULL) +
ylab(bquote(.(y)*","*~"#"/m^3) )+
scale_y_log10() + annotation_logticks(sides = "l") +
theme_bw() +
theme( panel.grid.major = element_blank(),
panel.grid.minor = element_blank() ) + facet_grid(.~day)
pdf(file=paste(y,'pdf',sep="."))
print(a)
dev.off()
}
mapply(plotfun, x=split(df, df$Bug), y=levels(df$Bug))
allplots <- ggplot(data=df, aes(x=station, y=Mean)) +
geom_errorbar(aes(ymin=(Mean-StErr), ymax=(Mean+StErr)), colour="black", width=0.1)+
geom_point(size=2) +
facet_grid(Bug ~ day) +
xlab(NULL) +
ylab(expression(Copepods *","* ~'#'/m^3)) +scale_x_continuous(expand=c(.3,0), breaks=c(1:3), labels=c("In", "FL", "Off")) +annotation_logticks(sides = "l") + scale_y_log10() +
theme_bw() +
theme( panel.grid.major = element_blank(),
panel.grid.minor = element_blank() ) +
theme(axis.text.x=element_text(size=12)) +
theme(axis.text.y=element_text(size=12))
print(allplots)