如何使用ggplot2中的ylab()创建动态轴标签?

时间:2013-11-23 00:47:32

标签: r ggplot2 axis-labels

我在ggplot2中创建了一系列图表,每列数据一个,我希望能够对其进行编码,以便Y轴标签动态更改为绘制变量的名称(以及其他一些标签中的常量信息)。

来自dput()的数据:

df <- structure(list(day = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L), .Label = c("5-Aug", "10-Aug", "17-Aug"), class = "factor"), 
station = c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L
), Bug = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 
1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 
3L, 3L, 3L), .Label = c("Copepod", "Copepod nauplii", "Harpactioid copepods"
), class = "factor"), Mean = c(382.4048896, 1017.171389, 
1519.141298, 233.3753491, 167.1086422, 530.7227507, 20.55943305, 
482.9992303, 10.49548997, 698.0193699, 5398.533995, 2458.635058, 
36.90431458, 124.4956045, 20.8459728, 8.414735929, 4014.649407, 
7.279486288, 346.1246378, 5722.82018, 6253.357325, 427.6856315, 
1768.984975, 1486.635443, 4.825526305, 46.50462845, 1.07692853
), StErr = c(83.18963396, 100.9187504, 73.45417607, 88.08491141, 
44.57580452, 44.03459391, 4.663586058, 112.4238952, 2.551982788, 
284.3933876, 3417.741042, 689.5558519, 9.798626545, 49.16103906, 
4.268815029, 3.76465482, 1803.977156, 1.328408258, 53.67873047, 
1796.827256, 732.573507, 86.56594682, 198.7842421, 229.0132055, 
1.129940095, 16.48065742, 0.283417726)), .Names = c("day", 
"station", "Bug", "Mean", "StErr"), row.names = c(NA, -27L), class = "data.frame")

我使用以下代码自动创建并保存一个新的,动态命名的.pdf:

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

我尝试在ylab()中使用paste(levels(df$Bug))但无法使其工作。我不知道还有什么可以尝试,并希望有人能够提出我可以研究的建议。我希望每个Y轴读取“VariableName,每平方米3”,其中VariableName取自Bug的水平(即Copepod,Copepod nauplii和Harpacticoid copepods)。有谁知道如何做到这一点?如果我能让它发挥作用,这看起来真的很酷很有用!感谢您的任何建议。

1 个答案:

答案 0 :(得分:2)

我想我可以帮助标记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 <- 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))

我只想发一页: enter image description here

还修复了我使用allplots版本进行的错误:

 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)