在回答最近的可视化问题时,我真的需要大括号来显示轴上的跨度,我无法弄清楚如何在ggplot2中执行此操作.这是情节:

而不是刻度线,我真的很喜欢y轴标签"双字母名字的第二个字母",从1到10(红色和蓝色第二个字母的垂直跨度)的支撑.但我不确定如何实现这一目标.x轴可以受益于类似的治疗.
代码在链接的CrossValidated问题中可用(并且对于此示例而言不必要地复杂,因此我不会显示它).相反,这是一个最小的例子:
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") )
Run Code Online (Sandbox Code Playgroud)

在这种情况下,对于低类型的(0,1)和对于高类型的(2,3)的大括号将是理想的而不是刻度标记.
我宁愿不使用,geom_rect因为:
我怎么做到这一点?完美的答案是:
breaks选项的范围类型对象scale_x_continuous)小智 18
使用绘制大括号的函数的另一种解决方案.
谢谢古尔!
curly <- function(N = 100, Tilt = 1, Long = 2, scale = 0.1, xcent = 0.5,
ycent = 0.5, theta = 0, col = 1, lwd = 1, grid = FALSE){
# N determines how many points in each curve
# Tilt is the ratio between the axis in the ellipse
# defining the curliness of each curve
# Long is the length of the straight line in the curly brackets
# in units of the projection of the curly brackets in this dimension
# 2*scale is the absolute size of the projection of the curly brackets
# in the y dimension (when theta=0)
# xcent is the location center of the x axis of the curly brackets
# ycent is the location center of the y axis of the curly brackets
# theta is the angle (in radians) of the curly brackets orientation
# col and lwd are passed to points/grid.lines
ymin <- scale / Tilt
y2 <- ymin * Long
i <- seq(0, pi/2, length.out = N)
x <- c(ymin * Tilt * (sin(i)-1),
seq(0,0, length.out = 2),
ymin * (Tilt * (1 - sin(rev(i)))),
ymin * (Tilt * (1 - sin(i))),
seq(0,0, length.out = 2),
ymin * Tilt * (sin(rev(i)) - 1))
y <- c(-cos(i) * ymin,
c(0,y2),
y2 + (cos(rev(i))) * ymin,
y2 + (2 - cos(i)) * ymin,
c(y2 + 2 * ymin, 2 * y2 + 2 * ymin),
2 * y2 + 2 * ymin + cos(rev(i)) * ymin)
x <- x + xcent
y <- y + ycent - ymin - y2
x1 <- cos(theta) * (x - xcent) - sin(theta) * (y - ycent) + xcent
y1 <- cos(theta) * (y - ycent) + sin(theta) * (x - xcent) + ycent
##For grid library:
if(grid){
grid.lines(unit(x1,"npc"), unit(y1,"npc"),gp=gpar(col=col,lwd=lwd))
}
##Uncomment for base graphics
else{
par(xpd=TRUE)
points(x1,y1,type='l',col=col,lwd=lwd)
par(xpd=FALSE)
}
}
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") )
curly(N=100,Tilt=0.4,Long=0.3,scale=0.025,xcent=0.2525,
ycent=par()$usr[3]+0.1,theta=-pi/2,col="red",lwd=2,grid=TRUE)
curly(N=100,Tilt=0.4,Long=0.3,scale=0.025,xcent=0.8,
ycent=par()$usr[3]+0.1,theta=-pi/2,col="red",lwd=2,grid=TRUE)
Run Code Online (Sandbox Code Playgroud)

swi*_*art 11
更新:如果您需要使用ggsave()保存绘图并确保在保存的图像中保留括号,请务必查看此相关的Stackoverflow问答.
OP要求支架离开情节.该解决方案axis.ticks.length结合使用axis.ticks = element_blank()以允许支架在绘图区域之外.这个答案建立在@Pankil和@ user697473的基础之上:我们将使用pBracketsR包 - 并包含图片!
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")
Run Code Online (Sandbox Code Playgroud)
关于@Pankil答案的快速说明:
## 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...
Run Code Online (Sandbox Code Playgroud)
还有一些展示以下功能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")
Run Code Online (Sandbox Code Playgroud)
## go vertical:
the_plot
grid.brackets(235, 200, 235, 300, lwd=2, col="red")
grid.brackets(445, 125, 445, 25, lwd=2, col="red")
Run Code Online (Sandbox Code Playgroud)
And*_*rie 10
这是kludgy解决方案,ggplot它构造了一个模糊地类似于花括号的线条图.
构造一个函数,将花括号的位置和尺寸作为输入.此函数的作用是指定括号的轮廓图的坐标,并使用一些数学缩放来使其达到所需的大小和位置.您可以使用此原则并修改坐标以获得任何所需的形状.原则上,您可以使用相同的概念并添加曲线,椭圆等.
bracket <- function(x, width, y, height){
data.frame(
x=(c(0,1,4,5,6,9,10)/10-0.5)*(width) + x,
y=c(0,1,1,2,1,1,0)/2*(height) + y
)
}
Run Code Online (Sandbox Code Playgroud)
将其传递给ggplot具体而言geom_line
qplot(x=x,y=y) +
scale_x_continuous("",breaks=c(.5,2.5), labels=c("Low types","High types")) +
geom_line(data=bracket(0.5,1,0,-0.2)) +
geom_line(data=bracket(2.5,2,0,-0.2))
Run Code Online (Sandbox Code Playgroud)

正如@ user697473建议的那样pBrackets是优雅的解决方案.
它最适合使用默认的绘图命令,但要使它与GGplot2一起使用,请使用pBracket::grid.brackets.我要包含代码,以便轻松试用.
从你的代码开始..
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") ) +
theme(axis.ticks = element_blank())
Run Code Online (Sandbox Code Playgroud)
最后一行删除了你不想要的刻度线.
现在pBrackets
library(pBrackets) # this will also load grid package
grid.locator(unit="native")
Run Code Online (Sandbox Code Playgroud)
现在使用光标识别图表中括号开始和结束的点.您将获得"原生"单元中的相应坐标.现在在下面的命令中提供它们
grid.brackets(240, 440, 50, 440, lwd=2, col="red")
grid.brackets(570, 440, 381, 440, lwd=2, col="red")
Run Code Online (Sandbox Code Playgroud)
您可以在图表的任何位置添加括号,甚至可以使用添加文本grid.text.
希望这可以帮助!谢谢pBrackets!
Pankil!
| 归档时间: |
|
| 查看次数: |
7145 次 |
| 最近记录: |