“无单位”,定性或相对轴标度ggplot2 [英] "Unitless", qualitative, or relative axis scales ggplot2

查看:113
本文介绍了“无单位”,定性或相对轴标度ggplot2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下图表:

  dat<  -  data.frame(x = 1:10,y = 1 :10)

ggplot(dat,aes(x = x,y = y))+ geom_point()

但实际上我对两轴上的值 2.5,5,7.5,10 没有兴趣。我想标记的是从低到高。我知道我可以标记 + xlab(低到高),但我更喜欢低位于轴的最左侧(在原点下)和高在最右边(在 10.0 下),也许从低到高的箭头。我可以手动指定休息时间,但这似乎太过分了。

解决方案

类似这样的东西可能会有所帮助,

 < $ c $ dat> data.frame(x = 1:10,y = 1:10)

p <-ggplot(dat,aes(x = x,y = y) )+ geom_point()+
scale_x_continuous('',breaks = NULL)+
scale_y_continuous('',breaks = NULL)

g < - ggplotGrob(p)
library(gtable)
library(grid)

my_axis< - function(low =low,high =high,axis = c(x,y ),...){

轴< - match.arg(axis)

if(axis ==x){
g1< ; textGrob(low,x = unit(0,npc),hjust = 0)
g3 < - textGrob(high,x = unit(1,npc),hjust = 1)$ b (g1)+单位(2,mm),单位(0.5,npc),
单位(1,npc) - grobWidth(g3) - 单位(2,mm),
单位(0.5,npc),...)

} else if(axis ==y){
g1 < - textGrob(low,y = unit(0,npc),rot = 90,hjust = 0)
g3 < - textGrob(high,y = unit(1,npc),rot = 90,hjust = 1)
g2 < - segmentsGrob(单位(0.5,npc),grobHeight(g1)+单位(2,mm),
单位(0.5,npc),
单位(1,npc) - grobHeight(g3) - 单位(2,mm),
...)

}

grobTree(g1,g2,g3)
}

g < - gtable_add_grob(g,my_axis(arrow = arrow(length = unit(2,mm))),$ b $ (g)-2,b = nrow(g)-1,l = 4)
g <-gtable_add_grob(g,my_axis(axis =y,arrow = arrow(length = unit ,mm))),
t = 3,l = 1,r = 3)
grid.newpage()
grid.draw(g)


Suppose I have the following chart

dat <- data.frame(x = 1:10, y = 1:10)

ggplot(dat, aes(x=x, y=y)) + geom_point()

but I'm not actually interested in the values 2.5, 5, 7.5, 10 on either axis. All I want to mark is something like "Low to High". I know I can mark + xlab("Low to High") but I would prefer "Low" to be at the far left of the axis (under the origin) and "High" to be at the far right (under 10.0) with perhaps an arrow from Low ---> High. I could specify the breaks manually, but that seems too much of a kludge.

解决方案

Something like this might help,

dat <- data.frame(x = 1:10, y = 1:10)

p <- ggplot(dat, aes(x=x, y=y)) + geom_point() +
  scale_x_continuous('', breaks=NULL)+
  scale_y_continuous('', breaks=NULL)

g <- ggplotGrob(p)
library(gtable)
library(grid)

my_axis <- function(low="low", high="high", axis=c("x", "y"), ...){

  axis <- match.arg(axis)

  if(axis == "x"){
  g1 <- textGrob(low, x=unit(0,"npc"), hjust=0)
  g3 <- textGrob(high, x=unit(1,"npc"), hjust=1)
  g2 <- segmentsGrob(grobWidth(g1) + unit(2,"mm"), unit(0.5,"npc"),
               unit(1,"npc") - grobWidth(g3)- unit(2,"mm"), 
               unit(0.5,"npc"), ...)

  } else if(axis == "y"){
    g1 <- textGrob(low, y=unit(0,"npc"), rot=90, hjust=0)
    g3 <- textGrob(high, y=unit(1,"npc"), rot=90, hjust=1)
    g2 <- segmentsGrob(unit(0.5,"npc"),grobHeight(g1) + unit(2,"mm"), 
                       unit(0.5,"npc"),
                       unit(1,"npc") - grobHeight(g3)- unit(2,"mm"), 
                       ...)

  }

  grobTree(g1,g2,g3)
}

g <- gtable_add_grob(g, my_axis(arrow=arrow(length=unit(2,"mm"))), 
                     t=nrow(g)-2, b=nrow(g)-1, l=4)
g <- gtable_add_grob(g, my_axis(axis="y", arrow=arrow(length=unit(2,"mm"))), 
                     t=3, l=1,r=3)
grid.newpage()
grid.draw(g)

这篇关于“无单位”,定性或相对轴标度ggplot2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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