更新:这个问题没有解决。图例键中的垂直线现在是ggplot2中geom_pointrange()的默认值。
对于ggplot2图形,它具有用于点估计的符号和代表该估计范围的垂直线(95%置信区间,四分位间距,最小值和最大值等),我无法得到图例键来显示带有符号的符号垂直线。由于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")
与geom_point
和geom_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_point
和geom_vline
一起尝试会产生所需的图例键,但不遵循图中的ymin
和ymax
值:
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] 删除。
我来说两句