- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图在 ggplot2 中生成一个轴线中断(轴线上有一个白色段),但遇到了一些问题。
使用信息性帖子 annotate-ggplot-with-an-extra-tick-and-label我能够在给定位置生成自定义grob,同时还关闭面板以在绘图区域外“绘制”。
我也熟悉其他软件包,例如 plotrix并且能够在基础上复制损坏的轴,但最重要的是我有兴趣了解为什么我创建的轴不会覆盖线。下面是一些示例代码:
library(ggplot2) # devtools::install_github("hadley/ggplot2")
library(grid)
library(scales)
data("economics_long")
econ <- economics_long
econ$value01 <- (econ$value01/2)
x <- ggplot(econ, aes(date, value01,group=1)) + scale_y_continuous(labels=c(0.0,0.1,0.2,0.3,0.4,0.5,1.0), breaks=c(0.0,0.1,0.2,0.3,0.4,0.5,0.6),limits = c(0,.6),expand = c(0, 0)) +
geom_smooth(colour="deepskyblue", show.legend = TRUE ) + theme_bw()
theme_white <- theme(panel.background=element_blank(),
panel.border=element_rect(color="white"),
plot.margin = unit(c(.2, 0, .2, .2), "cm"),
panel.grid.major.y=element_blank(),
panel.grid.major.x=element_blank(),
panel.grid.minor.x=element_blank(),
panel.grid.minor.y=element_blank(),
axis.title.y = element_blank(),
axis.line.x=element_line(color="gray", size=1),
axis.line.y=element_line(color="gray", size=1),
axis.text.x=element_text(size=12),
axis.text.y=element_text(size=12),
axis.ticks=element_line(color="gray", size=1),
legend.position="none"
)
x <- x + theme_white
gline = linesGrob(y = c(0, 1.5),x = c(-.015, .015), gp = gpar(col = "black", lwd = 2.5))
gline2 = linesGrob(y = c(-0.25, 0.5),x = c(0, 0), gp = gpar(col = "red", lwd = 5))
p = x + annotation_custom(gline, ymin=.55, ymax=.575, xmin=-Inf, xmax=Inf) +
annotation_custom(gline, ymin=.525, ymax=.55, xmin=-Inf, xmax=Inf) +
annotation_custom(gline2, ymin=.55, ymax=.575, xmin=-Inf, xmax=Inf)
# grobs are placed under the axis lines....
g = ggplotGrob(p)
g$layout$clip[g$layout$name=="panel"] <- "off"
grid.draw(g)
最佳答案
你很接近。布局数据框是您关闭剪辑的。布局数据框中的另一列给出了绘制各种绘图元素的顺序 - z。第二个绘制绘图面板(包括注释)(在背景之后),然后绘制轴。将绘图面板的 z 值更改为大于轴的 z 值。
library(ggplot2) # devtools::install_github("hadley/ggplot2")
library(grid)
library(scales)
data("economics_long")
econ <- economics_long
econ$value01 <- (econ$value01/2)
x <- ggplot(econ, aes(date, value01,group=1)) + scale_y_continuous(labels=c(0.0,0.1,0.2,0.3,0.4,0.5,1.0), breaks=c(0.0,0.1,0.2,0.3,0.4,0.5,0.6),limits = c(0,.6),expand = c(0, 0)) +
geom_smooth(colour="deepskyblue", show.legend = TRUE ) + theme_bw()
theme_white <- theme(panel.background=element_blank(),
panel.border=element_rect(color="transparent"),
plot.margin = unit(c(.2, 0, .2, .2), "cm"),
panel.grid.major.y=element_blank(),
panel.grid.major.x=element_blank(),
panel.grid.minor.x=element_blank(),
panel.grid.minor.y=element_blank(),
axis.title.y = element_blank(),
axis.line.x=element_line(color="gray", size=1),
axis.line.y=element_line(color="gray", size=1),
axis.text.x=element_text(size=12),
axis.text.y=element_text(size=12),
axis.ticks=element_line(color="gray", size=1),
legend.position="none"
)
x <- x + theme_white
gline = linesGrob(y = c(0, 1.5),x = c(-.015, .015), gp = gpar(col = "black", lwd = 2.5))
gline2 = linesGrob(y = c(-0.25, 0.5),x = c(0, 0), gp = gpar(col = "red", lwd = 5))
p = x + annotation_custom(gline, ymin=.55, ymax=.575, xmin=-Inf, xmax=Inf) +
annotation_custom(gline, ymin=.525, ymax=.55, xmin=-Inf, xmax=Inf) +
annotation_custom(gline2, ymin=.55, ymax=.575, xmin=-Inf, xmax=Inf)
# grobs are placed under the axis lines....
g = ggplotGrob(p)
g$layout$clip[g$layout$name=="panel"] <- "off"
g$layout # Note that z for panel is 1. Change it to something bigger.
g$layout$z[g$layout$name=="panel"] = 17
grid.newpage()
grid.draw(g)
关于r - ggplot2 - 在轴线上自定义 grob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39733972/
如何在图表中显示每个柱的轴线?我只有第二个,第四个......, alt text http://img35.imageshack.us/img35/6106/chartiu.png 最佳答案 我找到
我在 jqPlot 样式方面遇到了一些麻烦。目前我有这个: 这需要相当多的摆弄才能得到它,但现在我遇到了一个问题——左边的线!我不知道如何删除它,因为我实际上不知道它是什么! 这是我到目前为止的代码。
这是使用基数,我可以在其中控制 x 和 y Axis 范围,即应准确绘制线条的位置。 plot(mtcars$mpg, mtcars$hp, ylim = c(0, 400), xlim = c(0,
我想在 0 处向 Matlab 图形添加一条 x 轴线,以便在将图形保存为 jpg 时比较我的数据以查看它是正数还是负数。做这个的最好方式是什么?我知道您可以使用 line() 但它看起来很麻烦,因为
我想放入一个类似于 matplotlib 中提供的以下内容的 axspan,以便我的图表中有一条垂直线来指出一个位置。这是使用 pygal 的折线图 plt.axvspan(fin_loc, fin_
Plnkr 示例: http://plnkr.co/edit/19D5cnrVYdUrMlblQARy?p=preview 我的应用程序的屏幕截图: 我尝试修改 CSS,但是我只能删除第二条(右 Y
在上图中,有一条线表示 y = 0(标记 x 轴)。 x = 0 没有(标记 y 轴)。 如何让一个出现在 x = 0 处? 我试过... MyZedGraphObject.ZedGraphContr
我有一个显示标准偏差曲线(钟形曲线)的 Highchart。我想在 x=0 时绘制一条 x 轴垂直线。(请参阅我当前的图表)。当我包含代码以包含图表时,图表消失了,所以我相信我需要改变一些东西,很明显
我是 d3.js 的新手,我还不明白为什么我的 X 轴线放在顶部而不是底部(请看下面的图片)。 这是我正在使用的代码(经过简化以减少代码量): var data = []; var days = ["
我知道这个问题有点重复 jqPlot Styling - How to remove Y axis line? 但是因为那个问题没有得到确认的答案(发布的那个对我不起作用),所以我在这里重新提问。 我
我正在使用 flot 来显示条形图。当我将tickLength设置为0时,它会隐藏垂直线和水平线,但也会隐藏x轴和y轴线。我需要 x 轴和 y 轴,而不需要垂直和水平网格线。有办法做到这一点吗? 请参
我正在开发一个应用程序,为此我使用了 androidplot 库。我的要求是绘制曲线图 - 与显示的非常相似 here 我可以很好地绘制平滑图形,但我想删除与图形一起显示的 X 轴和 Y 轴。我只需要
对于下图 有谁知道如何得到那些上面有数字的水平线?我所能做的就是给刻度线本身和它们右边的数字上色。有什么想法可以实现吗? 最佳答案 您需要在轴上使用 majorGridLineStyle 属性。还要检
我是一名优秀的程序员,十分优秀!