轻推geom_segments,其中x | xend = -Inf和段按组进行颜色编码

时间:2017-12-08 21:28:10

标签: r ggplot2

我有三个地下水监测井的16个分析物(方面变量)的数据(井=颜色编码的因子基础),每个都以不同的间隔进行筛选。对于每个分析物(facet),目的是覆盖每个孔的数据并沿y轴显示相应的筛选间隔。一些屏幕重叠,因此不容易区分。目标是让它们以这种方式沿着y轴以等距间距对齐:|||。问题是我的facetting变量的级别有很大的不同。以下是使用钻石数据集的粗略示例。

require(dplyr)
require(ggplot2)

# Create mock dataframe, where facet variable ("mockvar") has different x-axis scales
mockdf <- filter(diamonds, cut=="Fair"|cut=="Good"|cut=="Ideal") %>% 
droplevels() %>% mutate(mockvar=ifelse(clarity=="SI2", 10*table,
              ifelse(clarity=="SI1", 100*table, 
              ifelse(clarity=="VS2", 1000*table, table))))

#Plot Code
ggplot(mockdf, aes(mockvar, depth, color=cut)) + scale_y_reverse() +
geom_point() + facet_wrap(~clarity, scales="free") +
geom_segment(data=mockdf[mockdf$cut=="Fair",], aes(x=-Inf, xend=-Inf, y=55, yend=65)) +
geom_segment(data=mockdf[mockdf$cut=="Good",], aes(x=-Inf, xend=-Inf, y=60, yend=70)) + 
geom_segment(data=mockdf[mockdf$cut=="Ideal",], aes(x=-Inf, xend=-Inf, y=65, yend=75)) 

#calls to position = position_dodge(width = #.#)) ...didn't work 

enter image description here

如果给出不同的缩放比例,我如何处理片段?另一种长篇解决方案是在每个方面进一步细化,例如:

ggplot(mockdf, aes(mockvar, depth, color=cut)) + scale_y_reverse() +
geom_point() + facet_wrap(~clarity, scales="free") +
geom_segment(data=mockdf[mockdf$cut=="Fair"& mockdf$clarity=="I1",], aes(x=49, xend=49, y=55, yend=65)) + 
geom_segment(data=mockdf[mockdf$cut=="Good"& mockdf$clarity=="I1",], aes(x=49.5, xend=49.5, y=60, yend=70)) + 
geom_segment(data=mockdf[mockdf$cut=="Ideal"& mockdf$clarity=="I1",], aes(x=50, xend=50, y=65, yend=75)) 
#and so on for all remaining facet levels....

但这充其量只是代码和原始的'jerry-rig'。有关保持第一组的初始x | xend = -Inf的建议,然后相对于-Inf轻推接下来的2个段,并且在整个面上具有一致的间距?

1 个答案:

答案 0 :(得分:1)

您必须关闭自动轴扩展,然后才能在需要的位置绘制细分。

require(dplyr)
require(ggplot2)

# Create mock dataframe, where facet variable ("mockvar") has different x-axis scales
mockdf <- filter(diamonds, cut=="Fair"|cut=="Good"|cut=="Ideal") %>% 
  droplevels() %>% mutate(mockvar=ifelse(clarity=="SI2", 10*table,
                                         ifelse(clarity=="SI1", 100*table, 
                                                ifelse(clarity=="VS2", 1000*table, table))))

# variables to control axis range and segment spacing
s1 = 0.9 # controls distance to minimum point
s2 = 0.03 # controls distance between segment lines

# add min and range variables
mockdf <- group_by(mockdf, clarity) %>%
  mutate(min = s1*min(mockvar),
         range = (2-s1)*max(mockvar) - s1*min(mockvar))

ggplot(mockdf, aes(mockvar, depth, color=cut)) + scale_y_reverse() +
  # switch off automatic axis expansion
  scale_x_continuous(expand = c(0, 0)) +
  geom_point() + facet_wrap(~clarity, scales="free") +
  geom_segment(data=mockdf[mockdf$cut=="Fair",], 
               aes(x=min, xend=min, y=55, yend=65)) +
  geom_segment(data=mockdf[mockdf$cut=="Good",],
               aes(x=min + s2*range, xend=min + s2*range, y=60, yend=70)) + 
  geom_segment(data=mockdf[mockdf$cut=="Ideal",],
               aes(x=min + 2*s2*range, xend=min + 2*s2*range, y=65, yend=75)) +
  # draw invisible segment to set end of x axis range
  geom_segment(data=mockdf[mockdf$cut=="Fair",],
               aes(x=min + range, xend=min + range, y=60, yend=70), color = NA)

enter image description here