r - 如何使用 ggplot2 for geom_pointrange() 类型图形获取图例键中的垂直线

标签 r plot ggplot2

更新:这个问题没有实际意义。图例键中的垂直线现在是 ggplot2 中 geom_pointrange() 的默认值。

对于具有点估计符号和表示该估计范围的垂直线(95% 置信区间、四分位数范围、最小值和最大值等)的 ggplot2 图形,我无法获取图例键来显示带有垂直线的符号。自 geom_pointrange()仅具有 ymin 的参数和ymax ,我认为 geom_pointrange(show_guide=T) 的预期(默认)功能将会有垂直线(我说默认是因为我知道使用 coord_flip 可以在图中绘制水平线)。我还了解到,当图例位置位于右侧或左侧时,图例键中的垂直线将使垂直线“一起运行”...但是对于顶部或底部的图例来说,通过符号有一条垂直线意味着该键将与图中出现的内容相匹配。

但是我尝试过的方法仍然在图例键中放置水平线:

## set up
library(ggplot2)
set.seed(123)
ru <- 2*runif(10) - 1
dt <- data.frame(x   = 1:10, 
                 y   = rep(5,10)+ru, 
                 ylo = rep(1,10)+ru, 
                 yhi = rep(9,10)+ru,
                 s   = rep(c("A","B"),each=5),
                 f   = rep(c("facet1", "facet2"), each=5))

默认show_guide=T对于 geom_pointrange产生所需的图,但在图例键中具有水平线,其中需要垂直(以便匹配图):

ggplot(data=dt)+
  geom_pointrange(aes(x     = x, 
                      y     = y, 
                      ymin  = ylo, 
                      ymax  = yhi, 
                      shape = s), 
                  size=1.1,
                  show_guide=T)+
  theme(legend.position="bottom")

enter image description here

尝试 geom_pointgeom_segment一起产生所需的图,但在图例键中具有水平线,其中需要垂直(以便匹配图):

ggplot(data=dt)+
  geom_point(aes(    x = x, 
                     y = y, 
                 shape = s), 
             size=3,
             show_guide=T)+
  geom_segment(aes(   x = x, 
                   xend = x, 
                      y = ylo, 
                   yend = yhi), 
               show_guide=T)+
  theme(legend.position="bottom")

enter image description here

尝试 geom_pointgeom_vline一起产生所需的图例键,但不尊重 yminymax图中的值:

ggplot(data=dt)+
  geom_point(aes(x=x, y=y, shape=s), show_guide=T, size=3)+
  geom_vline(aes(xintercept=x, ymin=ylo, ymax=yhi ), show_guide=T)+
  theme(legend.position="bottom")

enter image description here

如何获取第三张图的图例键以及前两张图之一的图?

最佳答案

我的解决方案涉及使用geom_vline(show_guide=T)绘制一条垂直线,以获取超出显示x轴范围的x值,同时绘制geom_segment(show_guide) =F):

ggplot(data=dt)+
  geom_point(aes(x=x, y=y, shape=s), show_guide=T, size=3)+
  geom_segment(aes(x=x, xend=x, y=ylo, yend=yhi), show_guide=F)+
  geom_vline(xintercept=-1, show_guide=T)+
  theme(legend.position="bottom")+
  coord_cartesian(xlim=c(0.5,10.5))

enter image description here

对于数字 x 轴使用 coord_cartesian() 的解决方案很好,但 facet_grid(scales='free_x') 可能会出现问题:

# problem:  coord_cartesian with numeric x and facetting with scales=free_x
ggplot(data=dt)+
  geom_point(aes(x=x, y=y, shape=s), show_guide=T, size=3)+
  geom_segment(aes(x=x, xend=x, y=ylo, yend=yhi), show_guide=F)+
  geom_vline(xintercept=-1, show_guide=T)+
  theme(legend.position="bottom")+
  coord_cartesian(xlim=c(0.5,10.5))+
  facet_grid(.~f, scales="free_x")

enter image description here

因此,在这种情况下,另一种解决方案可能不适用于所有情况,但将 x 值更改为因子的某个有意义的字符,然后调整 xlim:

## hack solution: adjust xlim after change x to factor or character 
## (carefully -- double check conversion):
dt$x <- factor(dt$x)
ggplot(data=dt)+
  geom_point(aes(x=x, y=y, shape=s), show_guide=T, size=3)+
  geom_segment(aes(x=x, xend=x, y=ylo, yend=yhi), show_guide=F)+
  geom_vline(xintercept=-1, show_guide=T)+
  theme(legend.position="bottom")+
  coord_cartesian(xlim=c(0.5,5.5))+
  facet_grid(.~f, scales="free_x")

enter image description here

关于r - 如何使用 ggplot2 for geom_pointrange() 类型图形获取图例键中的垂直线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33319235/

相关文章:

mysql - 如何使用 RODBC 将数据帧添加到 mysql 表

python - 如何在 Python 中绘制 e^(-t^2) 从 x=0 到 x=3 的积分?

python - matplotlib 图形的选项卡式窗口,这可能吗?

r - 如何在ggplot2中绘制绘图区域之外的线?

r - sparklyr 特征转换函数导致错误

r - 根据向量中的不同条件构建矩阵

r - 制作箱线图时如何在函数内部的ggplot2中对组使用aes_string

r - ggplot 2's continuous color scale incompatible with knitr' 是 tikzDevice 吗?

sql - 组内行之间的差异

python - 将 pandas.dataframe.plot() 与 matplotlib.pyplot.plot() 结合的最佳方法是什么?