ggplot2图形,从某个点开始缩放轴线 [英] ggplot2 graph, scale axis from a certain point on

查看:228
本文介绍了ggplot2图形,从某个点开始缩放轴线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在某个点开始以 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)



我想创建一个带有从 1到10乘以1 和之后乘以10 ,y值从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 (plot.margin = unit(c(0,0.5,0,0),lines))

p2 = ggplot(test,aes = 0:10)+
theme (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 $宽度[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和他的大力支持!

解决方案

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

  require 
$ b 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))



更新:使用ggplot2没有简单的方法来做一个破损的轴(因为ggplot2不允许你(很容易)做一些被认为是不好的做法),但是这里有一种方法来获得你想要的东西。 (只是不告诉哈德利我告诉过你)。

  library(data.table)
library(ggplot2)
library(scale)
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 = 5)+
scale_x_continuous(limits = c(0,10))+
coord_cartesian(ylim = c(-0.1,10))+
scale_y_continuous(breaks = 0:10)+
主题(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())+
实验室(y =)

左对齐两个图(基于



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

<1>运行更新后问题中的代码以创建 p1 和 p2 ,其中只有 p1 有一个图例。


2)使用下面的函数将图例解压为单独的grob(来自


How do i scale an axis with ggplot2 beginning at a certain point. Let's say we have a range from 0 to 100 and most values are within the range 1 to 10 and one value is at 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)

I would like to create a graph with an y-scale from 1 to 10 by 1 and afterwards by 10 so the space between the value 9 and 100 gets "smaller" in the graph.

Update:

The way of eipi10 works perfect for what i want to achieve. Just one more detail i am struggling with. How do i get rid of the 2nd legend and keep the right ratio in the final plot?

and the code for the plot:

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))

Update 2:

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

解决方案

A log transformation will do that:

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: There's no easy way to do a broken axis with ggplot2 (because ggplot2 doesn't allow you to (easily) do things that are considered bad practice), but here's a way to get what you're looking for. (Just don't tell Hadley I told you.)

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

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

The overall strategy is to make two separate plots, one for y>=10 and one for y<10 and then put them together. We'll change the plot margins in order to control the amount of space between the top of the bottom plot and the bottom of the top plot. We'll also get rid of the x-axis ticks and labels on the top plot.

Bottom plot (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"))

Top plot (y >= 10). For this one, we get rid of the x axis labels and tick marks:

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="")

Left align the two plots (based on 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)

Arrange both plots together. The heights argument determines the proportion of vertical space allotted to each plot:

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

UPDATE 2: To include a legend, but also ensure that the plots are properly right justified, do the following:

1) Run the code in your updated question to create plots p1 and p2, where only p1 has a legend.

2) Extract legend as a separate grob using the function below (from this SO answer).

3) Remove the legend from p1.

4) Lay out the plots and the legend using 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))

这篇关于ggplot2图形,从某个点开始缩放轴线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆