如何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)
Run Code Online (Sandbox Code Playgroud)
我想创建一个y-scale从1 to 10 by 1之后和之后的图形,by 10因此值9和100之间的空间在图中变得"更小".
更新:
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))
Run Code Online (Sandbox Code Playgroud)
更新2:
日志转换将执行此操作:
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))
Run Code Online (Sandbox Code Playgroud)
更新:没有简单的方法可以使用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))
Run Code Online (Sandbox Code Playgroud)
总体策略是制作两个单独的图,一个用于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"))
Run Code Online (Sandbox Code Playgroud)
顶部图(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="")
Run Code Online (Sandbox Code Playgroud)
左对齐两个图(基于此SO答案):
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)
Run Code Online (Sandbox Code Playgroud)
将两个地块排列在一起.该heights参数确定分配给每个图的垂直空间的比例:
grid.arrange(gB, gA, ncol=1, heights=c(0.15,0.85))
Run Code Online (Sandbox Code Playgroud)
更新2:要包含图例,还要确保图正确对齐,请执行以下操作:
1)在更新的问题,以创建地块运行代码p1和p2,只有p1拥有一个传说.
2)使用下面的函数将图例提取为单独的grob(来自此SO答案).
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))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1840 次 |
| 最近记录: |