如何使用 ggplot2 在多面图中定义 y 轴中断?

2024-04-15

我必须绘制具有不同范围值的数据。我正在使用 ggplot2 的构面设计和选项facet_grid(variable ~ ., scales = "free")。但是,我想设置 y 轴上的中断值,以便对于所有变量,中断都是c(0, max(variable)/2, max(variable))。我尝试使用scale_y_continuous,但没有成功。

可重现的例子:

v1 <- sample(rnorm(100, 10, 1), 30)
v2 <- sample(rnorm(100, 20, 2), 30)
v3 <- sample(rnorm(100, 50, 5), 30)
fac1 <- factor(rep(rep(c("f1", "f2", "f3"), each = 10), 3))

library(reshape2)
library(ggplot2)
df1 <- melt(data.frame(fac1, v1, v2, v3))
ggplot(df1, aes(fac1, value, group = variable)) +
  geom_point() +
  facet_grid(variable ~ ., scales = "free") +
  theme_bw()

这将绘制三个单独的图,其中 y 间隔设置为 0、0.5*max(value) 和 max(value)。使用 gtable rbind 函数将这三个图组合起来,然后绘制。

v1 <- sample(rnorm(100, 10, 1), 30)
v2 <- sample(rnorm(100, 20, 2), 30)
v3 <- sample(rnorm(100, 50, 5), 30)
fac1 <- factor(rep(rep(c("f1", "f2", "f3"), each = 10), 3))

library(reshape2)
library(ggplot2)
library(grid)
library(gridExtra)

df1 <- melt(data.frame(fac1, v1, v2, v3))

# Draw the three charts, each with a facet strip.
# But drop off the bottom margin material
dfp1 = subset(df1, variable == "v1")
p1 = ggplot(dfp1, aes(fac1, value, group = variable)) +
  geom_point() +
  facet_grid(variable ~ .) +
  scale_y_continuous(limits = c(0, ceiling(max(dfp1$value))),
      breaks = c(0, ceiling(max(dfp1$value))/2, ceiling(max(dfp1$value)))) +
  theme_bw()
g1 = ggplotGrob(p1)
pos = g1$layout[grepl("xlab-b|axis-b", g1$layout$name), "t"]
g1 = g1[-pos, ]

# Drop off the bottom margin material
dfp2 = subset(df1, variable == "v2")
p2 = ggplot(dfp2, aes(fac1, value, group = variable)) +
  geom_point() +
  facet_grid(variable ~ .) +
  scale_y_continuous(limits = c(0, ceiling(max(dfp2$value))), 
     breaks = c(0, ceiling(max(dfp2$value))/2, ceiling(max(dfp2$value)))) +
  theme_bw()
g2 = ggplotGrob(p2)
g2 = g2[-pos,]

dfp3 = subset(df1, variable == "v3")
p3 = ggplot(dfp3, aes(fac1, value, group = variable)) +
  geom_point() +
  facet_grid(variable ~ .) +
  scale_y_continuous(limits = c(0, ceiling(max(dfp3$value))), 
     breaks = c(0, ceiling(max(dfp3$value))/2, ceiling(max(dfp3$value)))) +
  theme_bw()
g3 = ggplotGrob(p3)

# Combine the three gtables
g = rbind.gtable(g1, g2, size = "max") 
g = rbind.gtable(g, g3, size = "max")

# Draw the plot
grid.newpage()
grid.draw(g)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 ggplot2 在多面图中定义 y 轴中断? 的相关文章

  • R - 基于列名称的子集

    我的数据框有超过 120 列 变量 我想根据列名称创建子集 例如 我想创建一个子集 其中列名称包含字符串 心情 这可能吗 我一般用 SubData lt myData grep whatIWant colnames myData 我很清楚
  • 如何像在facet_grid中一样在facet_wrap中定位条带标签

    我想在使用时删除多余的条带标签facet wrap 并用两个变量进行分面 并且都是自由尺度的 例如 这个facet wrap下图的版本 library ggplot2 dt lt txhousing txhousing year in 20
  • rvest 函数 html_nodes 返回 {xml_nodeset (0)}

    我正在尝试抓取以下网站的数据框 http stats nba com game 0041700404 playbyplay http stats nba com game 0041700404 playbyplay 我想创建一个表格 其中包
  • HTTR GET 新错误:SSL 证书问题:证书已过期

    我已经运行这段代码几个月了 没有出现任何问题 今天我突然开始在我的两台 AWS 服务器上收到以下错误消息 错误 curl curl fetch memory url handle handle SSL证书问题 证书已过期 当尝试运行以下代码
  • 无法编译包“maps”

    当我安装 maps 包时 安装中出现警告 ld warning ignoring file Library Developer CommandLineTools SDKs MacOSX10 14 sdk usr lib libSystem
  • 获取包含矩阵行内最大值的列名称,该矩阵在数组内包含单独的最大值

    例如给出 dim1 lt c P PO C T dim2 lt c LL RR R Y dim3 lt c Jerry1 Jerry2 Jerry3 Q lt array 1 48 c 4 4 3 dimnames list dim1 di
  • 是否有weighted.median()函数?

    我正在寻找类似形式的东西weighted mean 我通过搜索找到了一些解决方案 这些解决方案写出了整个函数 但希望有一些更用户友好的解决方案 以下软件包都有计算加权中位数的函数 aroma light isotone limma cwhm
  • ggplot2:带有 geom_line 的 x 轴因子不起作用

    我想要一个线图 其中value绘制为函数expt每级一行var 这是我的数据 lines lt expt var value 1 none p 0 183065327746799 2 none p 0 254234138384241 3 n
  • 如何在R中分离两个图?

    每当我运行这段代码时 第一个图就会简单地覆盖前一个图 R中有没有办法分开得到两个图 plot pc title main abc xlab xx ylab yy plot pcs title main sdf xlab sdf ylab x
  • 更改ggplot2中的字体

    曾几何时 我改变了我的ggplot2字体使用windowsFonts Times windowsFont TT Times New Roman 现在 我无法摆脱这一切 在尝试设置family in ggplot2 theme 当我用不同的字
  • 闪亮井板宽度

    library shiny library shinydashboard ui lt dashboardPage dashboardHeader dashboardSidebar dashboardBody wellPanel tags d
  • 如何在 foreach( ... , .packages="pkg") %dopar% 中指定 R 包的位置

    我的 包 安装在其他地方 我如何告诉 foreach 在哪里可以找到该包 foreach i 1 2 packages pkg dopar 这给我错误消息 worker initialization failed there is no p
  • 将文件名附加到 R 中的数据框

    我想将文件名附加到我的表中 但它似乎并没有真正起作用 我正在做的是迭代文件名列表 打开它们 将所有数据附加到一个数据帧 对于每个附加文件 我想添加其文件名 我希望将其附加到每一行 以便稍后当我查看数据时 我会知道给定行源自哪个文件 但似乎并
  • rPlot 工具提示问题

    我有一个使用 rCharts 工具提示的简单示例 但似乎不起作用 set seed 1 test lt data frame x rnorm 100 y rnorm 100 rPlot y x data test type point to
  • 如何在 R 中 fork 进程

    我试图了解 R 多核包实现的分叉系统 包的例子是 p lt fork if inherits p masterProcess cat I m a child Sys getpid n exit I was a child cat I m t
  • 使用行内 r 代码作为 R markdown 标头的一部分

    我希望使用行 R 代码作为 r markdown 文件中标头的一部分 然而 当我编织文件时 标题上使用的字体是不同的 如何确保字体相同 下面是一个简单的例子 r 1 1 Header 您可以将内容括在反引号中以表示内联 r 代码 如下所示
  • 将动物园转换为数据框

    我转换了一个zoo time series到数据框中R日期成为数据框的索引 有没有办法将日期表示为数据框中的普通列 monthly df lt data frame monthly zoo head monthly zoo head mon
  • 函数速度测试的奇怪结果

    我编写了一个使用递归来查找最大公因数 分母 的函数 gt gcd function a b if length a length b gt 1 warning Only scalars allowed using first element
  • Linux 中的 R 有哪些可用的 IDE? [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 Linux 中的 R 有哪些好的 IDE 我尝试过 Rcmdr 和 Eclipse 但似乎都不具有与 Windows 中的 Tinn R
  • 如何绘制 Voronoi 曲面细分的多边形而不是线段?

    我找到了一种使用 ggplot2 绘制 Voronoi 曲面细分的分段的方法 library deldir library ggplot2 library ggthemes set seed 123 df lt data frame lat

随机推荐