可以向ggplot2添加第三个y轴?

问题描述 投票:0回答:3
有可能在使用GGPLOT2的图中添加第三Y轴? 我想在图中显示三个不同的数据源。我已经添加了第二个Y轴,对于下一个数据集,该量表再次大不相同,为什么我现在正在寻找解决方案。

到目前为止,我只找到了如何添加第二个轴,例如[此处]。 但是我是否有可能再添加一个...谢谢!

这同样笨拙,但显示了如何仅使用Cran Resources从头开始进行。 library(cowplot) library(patchwork) p1 <- ggplot(df, aes(Sepal.Width, Sepal.Length)) + geom_point() + theme(axis.line = element_line()) p2 <- ggplot(df, aes(Sepal.Width, Petal.Width)) + geom_point() + theme(axis.line = element_line()) p3 <- ggplot(df, aes(Sepal.Width, Petal.Length)) + geom_point(aes(color = "Petal.Length")) + geom_point(aes(y = Sepal.Length/100, color = "Sepal.Length")) + geom_point(aes(y = Petal.Width / 1000, color = "Petal.Width")) + theme(axis.line = element_line(), plot.margin = margin(10, 10, 10, 30)) wrap_elements(get_plot_component(p1, "ylab-l")) + wrap_elements(get_y_axis(p1)) + wrap_elements(get_plot_component(p2, "ylab-l")) + wrap_elements(get_y_axis(p2)) + p3 + plot_layout(widths = c(3, 1, 3, 1, 40))

r ggplot2
3个回答
8
投票


data使用了

enter image description heredf <- iris df$Sepal.Length <- df$Sepal.Length * 100 df$Petal.Width <- df$Petal.Width * 1000


这是一个非常笨拙的解决方案,基于从先前绘制的图和编辑网格对象中提取元素。它可能会或不给您一个可行的解决方案。 source("https://raw.githubusercontent.com/davidearn/plague_growth/master/analysis/plots/3axes.R") set.seed(101) dd <- data.frame(x=rnorm(20),y=rnorm(20)) library(ggplot2) gg0 <- ggplot(dd) g1A <- gg0 + geom_point(aes(x,y)) g1B <- gg0 + geom_point(aes(x,10*y)) g1C <- gg0 + geom_point(aes(x,100*y)) ## use return_gtable = TRUE if planning to add further axes g2 <- combine_axes(g1A,g1B,add_pos="l", return_gtable=TRUE) g3 <- combine_axes(g2,g1C,add_pos="l") print(g3)


5
投票

有一个解决方案,不会制造另一个轴,而是使用ggtext软件包将第二轴上的标签作为HTML操纵。
首先,您会找到可以重新反弹其他2个变量的系数。

## Packages library(ggtext);library(tidyverse) ## Data df <- data.frame(category = paste0(rep("type_", 30), c("A", "B", "C")), x_val = rep(1:10, 3), y_val = abs(rnorm(30))*c(100,10,1)) ## Rebase rebase_coef <- df %>% group_by(category) %>% summarise(rebase_coef = max(y_val)) %>% ungroup() %>% mutate(rebase_coef = rebase_coef / max(rebase_coef)) df_rebase <- df %>% mutate(y_val = case_when(category == "type_B" ~ y_val/rebase_coef$rebase_coef[2], category == "type_C" ~ y_val/rebase_coef$rebase_coef[3], TRUE ~ y_val)) ugly graph with triple left axes,然后创建一个数据框架,该数据框将使您的标签从向量中用作主轴断裂的向量(我使用一个容易将标签转换为HTML的函数):

## Y Axis Breaks vbreaks <- max(df$y_val)*c(1, 0.75, 0.5, 0.25, 0) ## Category colors vcolor_cat <- c("#0077B7","#8B4500","#4DAF4A"); names(vcolor_cat) <- unique(df$category) ## HTML function html_fun <- function(x, color = 'white', size = 12) { paste0("<span style = 'color:", color,";font-size:", size, "pt'><b>", x, "</b></span>") } ## 2nd Labels axis_labs <- data.frame(y_A = vbreaks, y_B = vbreaks/10, y_C = vbreaks/100) %>% mutate(ylab_txt = paste0(html_fun(round(y_B,2), color = vcolor_cat[2]), "<br>", html_fun(round(y_C,2), color = vcolor_cat[3]) ))

0
投票
然后您情节:

df_rebase %>% ggplot(aes(x = x_val, y = y_val, color = category)) + geom_point() + scale_color_manual(values = vcolor_cat) + scale_y_continuous(breaks = vbreaks, labels = function(x) round(x, 2), name = "1 Thing", sec.axis = sec_axis(~.*1, labels = axis_labs$ylab_txt, breaks = vbreaks, name = "2 Things")) + theme( axis.title.y.left = element_textbox_simple( hjust = 0.5, vjust = 0.5, halign = 0.5, valign = 0.5, orientation = "left-rotated", width = 0.75, fill = "grey90", padding = margin(4, 2, 3, 2), margin = margin(2, 2, 2, 0)), axis.title.y.right = element_textbox_simple( hjust = 0.5, vjust = 0.5, halign = 0.5, valign = 0.5, orientation = "right-rotated", width = 0.75, fill = "grey90", padding = margin(4, 2, 3, 2), margin = margin(2, 0, 2, 2)), axis.text.y.left = element_markdown( colour = vcol_metric[1], fill = 'transparent', size = 12, face = "bold", hjust = 1, vjust = 0.5, halign = 1, valign = 0.5, lineheight = 0.75, padding = ggplot2::margin(3, 1, 1, 1), margin = ggplot2::margin(0, 0, 0, 0), align_widths = FALSE, align_heights = FALSE ), axis.text.y.right = element_markdown( fill = 'transparent', hjust = 0, vjust = 0.5, halign = 0, valign = 0.5, lineheight = 0.75, padding = ggplot2::margin(3, 1, 1, 1), margin = ggplot2::margin(0, 0, 0, 0), align_widths = FALSE, align_heights = FALSE ))

3y轴图

thanks to this Thread我设法构建了我需要的绘图。
我设法以特定的颜色为y轴着色。
无论如何,我一直在尝试以相同的方式(而不是默认的红色,绿色,蓝色)持续数小时,都找不到解决方案...有什么帮助吗?非常感谢!


最新问题
© www.soinside.com 2019 - 2025. All rights reserved.