gpt4 book ai didi

r - ggplot2 图,从某个点开始的刻度轴

转载 作者:行者123 更新时间:2023-12-04 11:48:34 26 4
gpt4 key购买 nike

我如何使用 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)

enter image description here

我想创建一个来自 1 to 10 by 1 的 y 比例图之后 by 10所以图表中值 9 和 100 之间的空间变得“更小”。

更新:

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

enter image description here

和情节的代码:
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:

最终结果的一个例子。再次感谢eipi10和他的大力支持!
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))

enter image description here

更新:没有简单的方法可以用 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<10,然后将它们放在一起。我们将更改绘图边距以控制底部绘图顶部和顶部绘图底部之间的空间量。我们还将去掉顶部图中的 x 轴刻度和标签。

底部图 (y < 10):
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="")

左对齐两个图(基于 this SO answer ):
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))

enter image description here

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

1) 运行更新问题中的代码以创建绘图 p1p2 ,这里只有 p1有一个传说。

2) 使用下面的函数(来自 this SO answer )将图例提取为单独的 grob。

3) 从 p1 中删除图例.

4) 使用 grid.arrange 布置图和图例和 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))

enter image description here

关于r - ggplot2 图,从某个点开始的刻度轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33942586/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com