使用带有facet_wrap的ggplot2显示不同的轴标签

Ben*_*Ben 21 r ggplot2

我有一个包含不同变量和不同单位的时间序列,我想在同一个图上显示.

ggplot不支持多轴(如此处所述),所以我按照建议并尝试使用facet绘制曲线:

x <- seq(0, 10, by = 0.1)
y1 <- sin(x)
y2 <- sin(x + pi/4)
y3 <- cos(x)

my.df <- data.frame(time = x, currentA = y1, currentB = y2, voltage = y3)
my.df <- melt(my.df, id.vars = "time")
my.df$Unit <- as.factor(rep(c("A", "A", "V"), each = length(x)))

ggplot(my.df, aes(x = time, y = value)) + geom_line(aes(color = variable)) + facet_wrap(~Unit, scales = "free_y", nrow = 2)
Run Code Online (Sandbox Code Playgroud)

结果如下: 在此输入图像描述

事情是,只有一个y标签,说"值",我想要两个:一个带"电流(A)",另一个带"电压(V)".

这可能吗?

aos*_*ith 40

ggplot2_2.2.1中,您可以使用strip.position参数in 将面板条移动为y轴标签facet_wrap.但是,使用此方法既没有条带标签,也没有不同的y轴标签,这可能并不理想.

将条带标签放在y轴("左侧")后,可以通过给出一个命名向量来更改标签,labeller以用作查找表.

条带标签可以通过strip.placementin 移动到y轴外theme.

删除条带背景和y轴标签以获得具有两个窗格和不同y轴标签的最终图形.

ggplot(my.df, aes(x = time, y = value) ) + 
     geom_line( aes(color = variable) ) + 
     facet_wrap(~Unit, scales = "free_y", nrow = 2, 
                strip.position = "left", 
                labeller = as_labeller(c(A = "Currents (A)", V = "Voltage (V)") ) )  +
     ylab(NULL) +
     theme(strip.background = element_blank(),
           strip.placement = "outside")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述 从顶部卸下条带使两个窗格非常靠近.要更改您可以添加的间距,例如,panel.margin = unit(1, "lines")theme.

  • 该解决方案需要`theme(strip.placement =“ outside”)`才能使用ggplot2 ver正确定位标签。2.2.1。 (2认同)

bVa*_*bVa 5

这是一个手动解决方案,使用其他方法可以更快,更轻松地解决此问题:

情节

如果您真的需要,使用边距和实验室将允许您坚持2绘图.

x <- seq(0, 10, by = 0.1)
y1 <- sin(x)
y2 <- sin(x + pi/4)
y3 <- cos(x)

my.df <- data.frame(time = x, currentA = y1, currentB = y2, voltage = y3)
my.df <- melt(my.df, id.vars = "time")
my.df$Unit <- as.factor(rep(c("A", "A", "V"), each = length(x)))


# Create 3 plots :
# A: currentA and currentB plot
A = ggplot(my.df, aes(x = time, y = value, color  = variable, alpha = variable)) + 
geom_line() + ylab("A") + 
scale_alpha_manual(values = c("currentA" = 1, "currentB" = 1, "voltage" = 0)) + 
guides(alpha = F, color = F)
# B: voltage plot
B =  ggplot(my.df, aes(x = time, y = value, color  = variable, alpha = variable)) + 
geom_line() + ylab("A") + 
scale_alpha_manual(values = c("currentA" = 0, "currentB" = 0, "voltage" = 1)) + 
guides(alpha = F, color = F)
# C: get the legend
C = ggplot(my.df, aes(x = time, y = value, color  = variable)) + geom_line() + ylab("A")


library(gridExtra) 
# http://stackoverflow.com/questions/12539348/ggplot-separate-legend-and-plot
# use this trick to get the legend as a grob object
g_legend<-function(a.gplot){
    tmp <- ggplot_gtable(ggplot_build(a.gplot))
    leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
    legend <- tmp$grobs[[leg]]
    legend
}

#extract legend from C plot
legend = g_legend(C)

#arrange grob (the 2 plots)
plots = arrangeGrob(A,B) 

# arrange the plots and the legend
grid.arrange(plots, legend , ncol = 2, widths = c(3/4,1/4))
Run Code Online (Sandbox Code Playgroud)