R GG用bty=绘制曲线图;n‘;,或者如何将栅格坐标添加到绘图坐标
我有一个问题是关于一个问题的。基本上,我想以适当的方式将R GG用bty=绘制曲线图;n‘;,或者如何将栅格坐标添加到绘图坐标,r,ggplot2,r-grid,R,Ggplot2,R Grid,我有一个问题是关于一个问题的。基本上,我想以适当的方式将bty=“n”添加到ggplot2图形中。这里强调正确,因为另一个问题中的解决方案几乎是我想要的,除了这个细节:我希望轴线能一直延伸到刻度线的末端,而不是中间。首先,图形的代码: library(ggplot2) library(grid) graph = ggplot(faithful, aes(x=eruptions, y=waiting)) + geom_point(shape=21) + theme( # t
bty=“n”
添加到ggplot2图形中。这里强调正确,因为另一个问题中的解决方案几乎是我想要的,除了这个细节:我希望轴线能一直延伸到刻度线的末端,而不是中间。首先,图形的代码:
library(ggplot2)
library(grid)
graph = ggplot(faithful, aes(x=eruptions, y=waiting)) +
geom_point(shape=21) +
theme(
# tick width, a bit exaggerated as example
axis.ticks = element_line(size = 5, color = "gray")
)
graph # graph with no axis lines
# get axis limits
gb = ggplot_build(graph)
xLim = range(gb$layout$panel_ranges[[1]]$x.major_source)
yLim = range(gb$layout$panel_ranges[[1]]$y.major_source)
# add lines
graph +
geom_segment(y = -Inf, yend = -Inf, x = xLim[1], xend = xLim[2]) +
geom_segment(x = -Inf, xend = -Inf, y = yLim[1], yend = yLim[2])
所以问题是:我从50到90画x轴。但是,记号以50和90为中心,因此它们在每一侧延伸了一半size=5
<代码>?元素线告诉我线/边框大小默认以毫米为单位。因此,我想画一条从50-5mm/2到90+5mm/2的线。我尝试了以下方法(多种变体):
xLim = range(gb$layout$panel_ranges[[1]]$x.major_source)
yLim = range(gb$layout$panel_ranges[[1]]$y.major_source)
uType = "npc"
uType2 = "mm"
# attempt conversion of units
xLim[1] = xLim[1] - convertWidth(unit(2.5, units = uType2),
unitTo = uType, valueOnly = TRUE)
xLim[2] = xLim[2] + convertWidth(unit(2.5, units = uType2),
unitTo = uType, valueOnly = TRUE)
yLim[1] = yLim[1] - convertHeight(unit(2.5, units = uType2),
unitTo = uType, valueOnly = TRUE)
yLim[2] = yLim[2] - convertHeight(unit(2.5, units = uType2),
unitTo = uType, valueOnly = TRUE)
# redraw graph
cairo_pdf("Rplot.pdf")
graph +
geom_segment(y = -Inf, yend = -Inf, x = xLim[1], xend = xLim[2]) +
geom_segment(x = -Inf, xend = -Inf, y = yLim[1], yend = yLim[2])
dev.off()
但是一点运气都没有。有什么想法吗?我相信您必须编写一个drawDetails方法或类似方法,以便在绘图时进行单位计算 或者(可能更容易),您可以编写一个扩展到轴线的自定义记号grob (请注意,由于z阶IIRC,这两个轴具有不同的线宽;我认为该错误已经修复)
库(ggplot2)
图书馆(网格)
element_grob.element_custom_x我相信您必须编写一个drawDetails方法或类似方法,以便在绘图时进行单位计算
或者(可能更容易),您可以编写一个扩展到轴线的自定义记号grob
(请注意,由于z阶IIRC,这两个轴具有不同的线宽;我认为该错误已经修复)
库(ggplot2)
图书馆(网格)
element\u grob.element\u custom\u x现在简单多了:使用包ggthemes()
中的geom\u rangeframe()
。我想它正是你想要的。现在简单多了:使用包ggthemes()
中的geom\u rangeframe()
。我认为它正是您想要的。来自?geom_Range Frame
的示例提供了一个坐标图,其中轴线超过x轴上的外部断点,但短于y轴上的外部断点。有没有办法使用geom_rangeframe
使轴线从最小断点变为最大断点?仔细想想,geom_rangeframe
中的方法更适合ggplot2样式,更易于实现(尽管它确实需要一些修改)。谢谢?geom_rangeframe
中的示例提供了一个坐标图,其中轴线超过x轴上的外部断点,但短于y轴上的外部断点。有没有办法使用geom_rangeframe
使轴线从最小断点变为最大断点?仔细想想,geom_rangeframe
中的方法更适合ggplot2样式,更易于实现(尽管它确实需要一些修改)。谢谢
library(ggplot2)
library(grid)
element_grob.element_custom_x <- function (element, x = 0:1, y = 0:1, colour = NULL, size = NULL,
linetype = NULL, lineend = "butt", default.units = "npc", id.lengths = NULL,
...)
{
gp <- gpar(lwd = ggplot2:::len0_null(size * .pt), col = colour, lty = linetype,
lineend = lineend)
element_gp <- gpar(lwd = ggplot2:::len0_null(element$size * .pt), col = element$colour,
lty = element$linetype, lineend = element$lineend)
arrow <- if (is.logical(element$arrow) && !element$arrow) {
NULL
}
else {
element$arrow
}
g1 <- polylineGrob(x, y, default.units = default.units,
gp = utils::modifyList(element_gp, gp),
id.lengths = id.lengths, arrow = arrow, ...)
vertical <- length(unique(element$x)) == 1 && length(unique(element$y)) >= 1
g2 <- grid::editGrob(g1, y=y + unit(1,"mm"), gp=utils::modifyList(gp, list(col="green")), name="new")
grid::grobTree(g2, g1)
}
element_grob.element_custom_y <- function (element, x = 0:1, y = 0:1, colour = NULL, size = NULL,
linetype = NULL, lineend = "butt", default.units = "npc", id.lengths = NULL,
...)
{
gp <- gpar(lwd = ggplot2:::len0_null(size * .pt), col = colour, lty = linetype,
lineend = lineend)
element_gp <- gpar(lwd = ggplot2:::len0_null(element$size * .pt), col = element$colour,
lty = element$linetype, lineend = element$lineend)
arrow <- if (is.logical(element$arrow) && !element$arrow) {
NULL
}
else {
element$arrow
}
g1 <- polylineGrob(x, y, default.units = default.units,
gp = utils::modifyList(element_gp, gp),
id.lengths = id.lengths, arrow = arrow, ...)
g2 <- grid::editGrob(g1, x=x + unit(1,"mm"), gp=utils::modifyList(gp, list(col="green")), name="new")
grid::grobTree(g2, g1)
}
## silly wrapper to fool ggplot2
x_custom <- function(...){
structure(
list(...), # this ... information is not used, btw
class = c("element_custom_x","element_blank", "element") # inheritance test workaround
)
}
y_custom <- function(...){
structure(
list(...), # this ... information is not used, btw
class = c("element_custom_y","element_blank", "element") # inheritance test workaround
)
}
graph = ggplot(faithful, aes(x=eruptions, y=waiting)) +
geom_point(shape=21) + theme_minimal() +
theme(
axis.ticks.x = x_custom(size = 5, colour = "red") ,
axis.ticks.y = y_custom(size = 5, colour = "red") ,
axis.ticks.length = unit(2,"mm")
)
graph # graph with no axis lines
gb <- ggplot_build(graph)
xLim = range(gb$layout$panel_ranges[[1]]$x.major_source)
yLim = range(gb$layout$panel_ranges[[1]]$y.major_source)
graph +
geom_segment(y = -Inf, yend = -Inf, x = xLim[1], xend = xLim[2],lwd=2) +
geom_segment(x = -Inf, xend = -Inf, y = yLim[1], yend = yLim[2],lwd=2)