ggplot2图,从某一点开始的刻度轴

2024-03-18

我如何缩放轴ggplot2从某一点开始。假设我们的范围是 0 到 100,大多数值都在 1 到 10 范围内,其中一个值是 100。

require('data.table')
require('ggplot2')

test <- data.table(x=1:10,y=c(seq(1,9),100))

ggplot(test, aes(x=x,y=y)) + geom_point(size=5)

我想创建一个 y 尺度的图表1 to 10 by 1然后by 10因此图中值 9 和 100 之间的空间变得“更小”。

Update:

eipi10 的方式非常适合我想要实现的目标。我正在努力解决的还有一个细节。我如何摆脱第二个图例并在最终情节中保持正确的比例?

和情节的代码:

test <- data.table(x=1:10,y=c(seq(1,9),100))

p1 = ggplot(test, aes(x=x,y=y,color=x)) + 
  geom_point(size=5) +
  scale_x_continuous(limits=c(0,10)) +
  coord_cartesian(ylim=c(-0.1,10)) +
  scale_y_continuous(breaks=0:10) +
  theme(plot.margin=unit(c(0,0.5,0,0),"lines"))

p2 = ggplot(test, aes(x=x,y=y,color=x)) + 
  geom_point(size=5) + #geom_point(size=5,show.legend=FALSE) +
  scale_x_continuous(limits=c(0,10)) +
  coord_cartesian(ylim=c(40,110)) +
  scale_y_continuous(breaks=c(50,100)) +
  theme(plot.margin=unit(c(0,0.5,-0.5,0), "lines"),
       axis.title.x=element_blank(),
       axis.ticks.x=element_blank(),
       axis.text.x=element_blank(),
       legend.position="none") +
 labs(y="")

gA <- ggplotGrob(p1)
gB <- ggplotGrob(p2)
maxWidth = grid::unit.pmax(gA$widths[2:5], gB$widths[2:5])
gA$widths[2:5] <- as.list(maxWidth)
gB$widths[2:5] <- as.list(maxWidth)
grid.arrange(gB, gA, ncol=1, heights=c(0.15,0.85))

更新2:

An example of the final result. Thanks again to eipi10 and his great support! enter image description here


对数转换将做到这一点:

require('data.table')
require('ggplot2')
library(scales)

test <- data.table(x=1:10,y=c(seq(1,9),100))

ggplot(test, aes(x=x,y=y)) + 
  geom_point(size=5) +
  scale_y_log10(breaks=c(1,3,10,30,100))

UPDATE:没有简单的方法可以用 ggplot2 来制作断轴(因为 ggplot2 不允许您(轻松)做被认为是不好的做法的事情),但这里有一种方法可以得到您正在寻找的东西。 (只是不要告诉哈德利是我告诉你的。)

library(data.table)
library(ggplot2)
library(scales)
library(grid)
library(gridExtra)

test <- data.table(x=1:10,y=c(seq(1,9),100))

总体策略是制作两张单独的图,一张用于 y>=10,一张用于 y

底部图(y

p1 = ggplot(test[test$y<10,], aes(x=x,y=y)) + 
  geom_point(size=5) +
  scale_x_continuous(limits=c(0,10)) +
  coord_cartesian(ylim=c(-0.1,10)) +
  scale_y_continuous(breaks=0:10) +
  theme(plot.margin=unit(c(0,0.5,0,0),"lines"))

顶部图 (y >= 10)。对于这一点,我们去掉了 x 轴标签和刻度线:

p2 = ggplot(test[test$y>=10,], aes(x=x,y=y)) + 
  geom_point(size=5) +
  scale_x_continuous(limits=c(0,10)) +
  coord_cartesian(ylim=c(10.0,110)) +
  scale_y_continuous(breaks=c(50,100)) +
  theme(plot.margin=unit(c(0,0.5,-0.5,0), "lines"),
        axis.title.x=element_blank(),
        axis.ticks.x=element_blank(),
        axis.text.x=element_blank()) +
  labs(y="")

将两个图左对齐(基于这个答案 https://stackoverflow.com/a/13295880/496488):

gA <- ggplotGrob(p1)
gB <- ggplotGrob(p2)
maxWidth = grid::unit.pmax(gA$widths[2:5], gB$widths[2:5])
gA$widths[2:5] <- as.list(maxWidth)
gB$widths[2:5] <- as.list(maxWidth)

将两个图排列在一起。这heights参数决定分配给每个图的垂直空间的比例:

grid.arrange(gB, gA, ncol=1, heights=c(0.15,0.85))

更新2:要包含图例,同时还要确保绘图正确右对齐,请执行以下操作:

1)运行更新问题中的代码来创建绘图p1 and p2,其中仅p1有一个传说。

2)使用下面的函数将图例提取为单独的grob(来自这个答案 https://stackoverflow.com/a/12539820/496488).

3)删除图例p1.

4)使用布局图和图例grid.arrange and arrangeGrob.

# Function to extract the legend as a stand-alone grob
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 the legend from p1
leg = g_legend(p1)

# Remove the legend from p1
p1 = p1 + theme(legend.position="none")

# Left justify the two plots
gA <- ggplotGrob(p1)
gB <- ggplotGrob(p2)
maxWidth = grid::unit.pmax(gA$widths[2:5], gB$widths[2:5])
gA$widths[2:5] <- as.list(maxWidth)
gB$widths[2:5] <- as.list(maxWidth)

# Lay out the plots and the legend
grid.arrange(arrangeGrob(gB, gA, ncol=1, heights=c(0.15,0.85)),
             leg, ncol=2, widths=c(0.9,0.1))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ggplot2图,从某一点开始的刻度轴 的相关文章

随机推荐

  • 如何清除powershell中的变量内容

    我昨天刚开始学习powershell powershell 非常新 我创建了很多变量用于测试目的 以下是我关于变量的问题 如何列出我之前创建的所有变量 那么如何清除变量的所有内容呢 如何移除 删除变量 如何列出我之前创建的所有变量 这会获取
  • Golang:如何创建未知(动态)地图长度

    我可以通过创建 静态 地图 type m map int map int map int bool 但 键 的长度是动态的 unknown len m 1 2 3 4 2 0 true or unk len m 1 2 3 4 true 我
  • 在 Android Kotlin 中查找日期之间的天数差异

    所以 我已经在 google 上搜索 其中大多数使用 Java 有一个使用 Kotlin 并且经常使用与我使用的时间格式不同的时间格式 yyyy mm dd HH mm ss 所以我尝试编码并卡住了 所以 这是代码 import java
  • Spark中使用Map替换列值

    我必须将列列表映射到 Spark 数据集中的另一列 想像这样 val translationMap Map Column Column Map lit foo gt lit bar lit baz gt lit bab 我有一个像这样的数据
  • 如何在R中将时差转换为分钟?

    我有以下程序 timeStart lt Sys time timeEnd lt Sys time difference lt timeEnd timeStart anyVector lt c difference 最后我需要将该数据放入向量
  • 类方法作为 scipy.optimize.curve_fit 的模型函数

    说明书上有这样一句话curve fit that 模型函数 f x 它必须将自变量作为第一个参数 并将参数作为单独的剩余参数进行拟合 但是 我想使用该类的方法作为模型函数 其定义为 def model fun self x par 因此 如
  • 在 WPF 中重复背景画笔

    谢谢你 这个问题与这里这个古老的 未解答的问题非常相似 如何将类似笔记本的线条绘制为 TextBox 背景 https stackoverflow com questions 4041642 wpf how to paint noteboo
  • 带有参数列表的简洁查询

    我正在尝试使用 Dapper 运行带有一组已知参数的查询 但带有这些参数的值列表 我想做的一个简单的例子是 DateTime endDate DateTime Now DateTime startDate endDate AddHours
  • 为什么处理多个异常需要元组而不是列表?

    考虑以下示例 def main list error type try if error type runtime raise RuntimeError list error if error type valueerror raise V
  • 如何在slickgrid中进行多列分组?

    我是 slickgrid 的新手 我已经浏览了一些 slickgrid 的例子 并且基础知识很好 我有一个场景 我需要基于多列进行分组 但 slickgrid 分组是基于单列 如何在 slickgrid 中完成多列分组 并在每个组上具有展开
  • Javafx 中的动态条形图

    您好 我正在尝试创建显示字段值的条形图 字段值通过排序算法更改 图表应显示任何更改 public class FXMLDocumentController implements Initializable static int pole n
  • 在 Android 设计库中使用 TabLayout 的带有图标的选项卡

    我正在尝试使用 android 设计库中的新 TabLayout 来创建仅包含图标的应用程序栏 like this 我该如何使用新的 TabLayout Android 设计库来做到这一点 有没有一个简单的解决方案 或者我只能使用 setC
  • 使用JuMP时如何转换变量的类型

    我正在使用 Julia JuMP 来实现算法 在一部分中 我定义了一个具有连续变量的模型并求解线性模型 我做了一些其他计算 在此基础上向模型添加了一些约束 然后我想用整数变量来解决相同的问题 我无法使用convert 函数 因为它不带变量
  • Chrome 开发工具和 Genymotion Android 模拟器

    参考 Genymotion 如何使用 Chrome 开发工具进行调试 https stackoverflow com questions 21627328 genymotion how to debug with chrome dev to
  • iOS 7 UITextView:重新打开应用程序后 nstextattachment 的大小变为 2 倍

    我正在使用 ios7 中的文本工具包构建一个笔记编辑器 早些时候 我在渲染自定义大小的 NSTextAttachment 时遇到了麻烦 因为它在很大程度上减慢了渲染速度 我通过缩放图像然后将它们添加到 textview 解决了这个问题 您可
  • 如何在 javascript 中使用 scriptlet

    有人可以测试这个例子并分享结果吗 http timothypowell net blog p 23 http timothypowell net blog p 23当我做 var myVar alert myVar I get 从 给出语法
  • Emacs:左侧边缘的 TODO 指示器有一个奇怪的副作用 - 删除字符

    我刚刚读过Emacs 左侧的 TODO 指示器 https stackoverflow com questions 2242572 emacs todo indicator at left side 并尝试了一下 看起来很有趣 小指示三角形
  • 如何获取允许用户访问的存储库的完整列表?

    我发现 bitbucket api 如下 https bitbucket org api 2 0 repositories teamname 但此链接返回 301 状态 已永久移至 api 2 0 repositories teamname
  • 从字符串到整数的映射 - 各种方法的性能

    假设我需要从以下位置进行映射String为一个整数 整数是唯一的 并且形成从0开始的连续范围 即 Hello gt 0 World gt 1 Foo gt 2 Bar gt 3 Spam gt 4 Eggs gt 5 etc 至少有两种简单
  • ggplot2图,从某一点开始的刻度轴

    我如何缩放轴ggplot2从某一点开始 假设我们的范围是 0 到 100 大多数值都在 1 到 10 范围内 其中一个值是 100 require data table require ggplot2 test lt data table