ggplot2:轴上的花括号? [英] ggplot2: Curly braces on an axis?

查看:283
本文介绍了ggplot2:轴上的花括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在,请务必查看括号在保存的图像中保留。






OP请求括号离开图。该解决方案使用 axis.ticks.length axis.ticks = element_blank()结合以允许大括号在绘图区域之外。
这个答案建立在@Pankil和@ user697473的基础上:我们将使用 pBrackets R包 - 并包含图片!





$ bx < - c(runif(10),runif b $ b $ p $ library $($) (10)+2)
y< -c(runif(10),runif(10)+2)
the_plot < - qplot(x = x,y = y)+
scale_x_continuous(,breaks = c(.5,2.5),labels = c(Low types,High types))+
theme(axis.ticks = element_blank(),
axis .ticks.length =单位(.85,cm))


#运行grid.locator几次获得坐标的外部
#支架,确保
#bottom_y坐标位于灰色区域的底部。
#退出grid.locator命令esc;在设置坐标
#后,在grid.bracket中注释掉grid.locator()行
the_plot
grid.locator(unit =native)
bottom_y < - 284
grid.brackets(220,bottom_y,80,bottom_y,lwd = 2,col =red)
grid.brackets(600,bottom_y,440,bottom_y,lwd = 2,col =red)



关于@ Pankil答案的简要说明:

  ##括号坐标取决于图的大小
##例如
## Pankil建议的支架坐标不能用
##进行以下尺寸调整:
the_plot
grid.brackets(240,440,50 ,440,lwd = 2,col =red)
grid.brackets(570,440,381,440,lwd = 2,col =red)
## 440似乎关闭图表...



还有一些展示 pBrackets 的功能:

 #注意,如果反转x1和x2,则括号翻转:
the_plot
grid.brackets(80,bottom_y,220,bottom_y,lwd = 2,col =red)
grid .brackets(440,bottom_y,600,bottom_y,lwd = 2,col =red)

  ##垂直:
the_plot
grid.brackets(235,200,235,300,lwd = 2,col =red)
grid.brackets(445,125,445,25,lwd = 2,col =red)


In answering a recent visualization question I really needed braces to show a span on an axis, and I can't figure out how to do it in ggplot2. Here's the plot:

Instead of a tick mark, I'd really like the y axis label "Second letter of two-letter names" to have a brace extending from 1 to 10 (the vertical span of the red and blue second letters). But I'm not sure how to make that happen. The x axis could benefit from similar treatment.

Code is available in the linked CrossValidated question (and unnecessarily complicated for this example, so I won't show it). Instead, here's a minimal example:

library(ggplot2)
x <- c(runif(10),runif(10)+2)
y <- c(runif(10),runif(10)+2)
qplot(x=x,y=y) +
  scale_x_continuous("",breaks=c(.5,2.5),labels=c("Low types","High types") )

In this case, a brace from (0,1) for low types and from (2,3) for the high types would be ideal instead of tick marks.

I'd rather not use geom_rect because:

  • The tick marks will remain
  • I'd prefer braces
  • It will be inside the plot instead of outside it

How would I accomplish this? The perfect answer would have:

  • A nice, smooth, thin curly brace
  • Drawn outside the plotting area
  • Specified via a high-level argument (ideally a range-type object passed to the breaks option in scale_x_continuous)

解决方案

Update: Be sure to see this related Stackoverflow Q&A if you need to save the plot with ggsave() and have the brackets persist in the saved image.


The OP requested the bracket be off the plot. This solution uses axis.ticks.length in combination with axis.ticks = element_blank() to allow the brace to be outside the plotting area. This answer builds upon those of @Pankil and @user697473: we will use pBrackets R package -- and include pictures!

library(ggplot2)
library(grid)
library(pBrackets) 
x <- c(runif(10),runif(10)+2)
y <- c(runif(10),runif(10)+2)
the_plot <- qplot(x=x,y=y) +
  scale_x_continuous("",breaks=c(.5,2.5),labels=c("Low types","High types") ) +
  theme(axis.ticks = element_blank(),
        axis.ticks.length = unit(.85, "cm"))


#Run grid.locator a few times to get coordinates for the outer
#most points of the bracket, making sure the 
#bottom_y coordinate is just at the bottom of the gray area.
# to exit grid.locator hit esc; after setting coordinates
# in grid.bracket comment out grid.locator() line
the_plot
grid.locator(unit="native") 
bottom_y <- 284
grid.brackets(220, bottom_y,   80, bottom_y, lwd=2, col="red")
grid.brackets(600, bottom_y,  440, bottom_y, lwd=2, col="red")

A quick note on @Pankil's answer:

## Bracket coordinates depend on the size of the plot
## for instance,
## Pankil's suggested bracket coordinates do not work
## with the following sizing:
the_plot
grid.brackets(240, 440, 50, 440, lwd=2, col="red")
grid.brackets(570, 440, 381, 440, lwd=2, col="red")
## 440 seems to be off the graph...

And a couple more to showcase functionality of pBrackets:

#note, if you reverse the x1 and x2, the bracket flips:
the_plot
grid.brackets( 80, bottom_y, 220, bottom_y, lwd=2, col="red")
grid.brackets(440, bottom_y, 600, bottom_y, lwd=2, col="red")

## go vertical:
the_plot
grid.brackets(235, 200, 235, 300, lwd=2, col="red")
grid.brackets(445, 125, 445,  25, lwd=2, col="red")

这篇关于ggplot2:轴上的花括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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