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

斯威哈特

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

对于ggplot2图形,它具有用于点估计的符号和代表该估计范围的垂直线(95%置信区间,四分位间距,最小值和最大值等),我无法得到图例键来显示带有符号的符号垂直线。由于geom_pointrange()只有论点yminymax,我想的预期(默认值)功能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=Tgeom_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")

在此处输入图片说明

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")

在此处输入图片说明

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")

在此处输入图片说明

如何获得第三个图的图例键,但前两个图之一的图?

斯威哈特

我的解决方案涉及到绘制一条垂直线,并绘制出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))

在此处输入图片说明

coord_cartesian()x轴数字的解决方案很好,但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")

在此处输入图片说明

因此,在那种情况下,另一种解决方案可能并不适用于每种情况,而是将x值更改为factor的某些有意义的特征,然后调整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")

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用ggplot2中的geom_pointrange()自动消除线型图例的形状和图例的线型?

如何在ggplot2中为带有图例的垂直线添加标签

如何在ggplot中为垂直线添加图例?

如何使用ggplot2在R中的散射极坐标图中获得垂直线

如何添加图例以标识ggplot中的垂直线?

在ggplot图例中旋转垂直线

ggplot2构面中的单个垂直线

删除图例中由ggplot中的geom_vline引起的垂直线

如何在单个ggplot2中对齐图层(密度图和垂直线)

在 R ggplot2 中,如何自动绘制垂直线

ggplot2:如何在时间x轴上将文本添加到多条垂直线(geom_vlines)?

ggplot2:geom_errorbarh结尾处的两条垂直线的大小

带有重要性注释的ggplot2 geom_pointrange中按方面不同的颜色值

ggplot2垂直线未插入所需的位置

ggplot2 boxplot仅显示垂直线

限制R ggplot2中的垂直线长度

使用ggplot2在多个异常点的日期时间添加垂直线

如何填充geom_pointrange()后面的ggplot区域?

在ggplot点图中为垂直线添加第二个图例

ggplot2:具有coord_flip()和自由刻度的geom_pointrange()facet_grid()

如何使用带有时间序列数据的ggplot为graf添加垂直垂直线?

如何在ggplot2中为图例键混合不同的符号?

如何使用HighStocks或HighCharts在图表中添加垂直线?

将ggplot2中的图例标题和图例键居中对齐以获取长图例标题

ggplot2:如何在图例键中分隔geom_polygon和geom_line?

如何在ggplot2中获取堆叠的geom_bar和geom_point的通用图例?

在ggplot2中,如何使用geom_hline函数显示图例?

如何替换ggplot2中的图例键?

如何在holoviews + bokeh中获得带有图例标签的全高垂直线?