使用facet_wrap和geom_segment删除不必要的y轴点 [英] Remove unnecessary y-axis points with facet_wrap and geom_segment

查看:284
本文介绍了使用facet_wrap和geom_segment删除不必要的y轴点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 geom_segment()然后使用 facet_wrap 来将y轴点映射到x轴。将数据分成两块地块;然而,这两个地块的y轴点都出现了。



如何获得与每个 facet_wrap

$ b相关的必要y轴点
$ b

示例代码

  dat < -  structure(list(临时= C(1,2,3,4,5,1,2,3,4,5,1,2,3,
4,5,1,2,3,4,5,1, 1,2,3,4,5,1,2,3,4,5),rev = c(-5,
-11,-20,-29,-40,-9,-20,-32 ,-45,-57,-12,-24,-37,-50,
-62,-7,-20,-36,-52,-67,-5,-13,-23, -35,-47,-12,-24,
-36,-48,-58),式= C( 类型1, 类型1, 类型1, 类型1,
类型1, 1型, 类型1, 1型, 类型1, 1型, 1型,
类型1,类型1\" , 类型1, 类型1, 2型, 类型2, 类型2,
类型2, 类型2, 类型2,2型, 类型2, 类型2, 类型2,
类型2, 类型2, 类型2, 类型2, 类型2),型号= C ( A,
A, A, A, A, b, b, b, b, b, C, C,C,C,
,C,A,A,A,A, ,B,B,C,C,
C,C,C) ),.Names = c(temp,rev,type,model),row.names = c(NA,
-30L),class =data.frame)

p1 <-ggplot(dat,aes(temp,rev,color = model))+
geom_line()+ geom_point()+ geom_segment(aes(x = 0,xend = temp, $ = b $ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b

情节

< a href =https://i.stack.imgur.com/RRUov.png =nofollow noreferrer>

  library(dplyr)
$ c $ d>
进入我的控制台使它变得不快乐。
df< - expand.grid(temp = 1:5,model = LETTERS [1:3],type = 1:2)%>%
group_by(model,type)%> %
mutate(rev = -sort(sample.int(20,length(temp))))

#这相当于您原来的数据,structurewise






  df.labeled<  -  df%>%
ungroup()%>%group_by(type,rev)%>%
mutate(label = c(rev [1],rep(NA,length(rev) - 1)))

在这里,我为每个组面板中显示的每个y值创建一个组。然后我创建一个标签列,其中包含1个观察到的y值,并填充 NA s。所以如果一个面板有两个模型,每个模型都有 rev < -5 ,现在它会是 -5,NA 而不是 -5,-5 。为什么我这样做会变得更清晰。






  ggplot(df。标记为aes(temp,rev,color = model))+ 
geom_segment(aes(xend = 0,yend = rev),linetype =dashed,color =gray)+
geom_text aes(label = label,x = -0.1),color =black,hjust = 1)+
geom_vline(xintercept = 0)+
geom_point()+ geom_line()+ facet_grid(〜type )+
scale_y_continuous(breaks = NULL)+
scale_x_continuous(limits = c(-0.5,NA))+
theme_bw()+ theme(panel.grid = element_blank())



如果我留在重复项中(这里是-7,-15代表类型1,-11代表类型2), geom_text 应该是一个杂乱的粗体模糊。重复的文本标签在 ggplot2 中不能很好地呈现。既然按照你想要的方式不可能做到这一点,那么我们只是为每个面板制作一个假的比例。如果你不喜欢这个数字左边的y轴上有一个额外的行,那么可以去掉它:

  ... + 
theme(panel.grid = element_blank(),
panel.border = element_blank(),
axis.line.x = element_line())


I'm mapping y-axis points to the x-axis using geom_segment() and then using facet_wrap to separate data into two plots; however, the y-axis points are showing up on both plots.

How can I have only the necessary y-axis points pertaining to each facet_wrap?

Sample Code

dat <- structure(list(temp = c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 
4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5), rev = c(-5, 
-11, -20, -29, -40, -9, -20, -32, -45, -57, -12, -24, -37, -50, 
-62, -7, -20, -36, -52, -67, -5, -13, -23, -35, -47, -12, -24, 
-36, -48, -58), type = c("Type 1", "Type 1", "Type 1", "Type 1", 
"Type 1", "Type 1", "Type 1", "Type 1", "Type 1", "Type 1", "Type 1", 
"Type 1", "Type 1", "Type 1", "Type 1", "Type 2", "Type 2", "Type 2", 
"Type 2", "Type 2", "Type 2", "Type 2", "Type 2", "Type 2", "Type 2", 
"Type 2", "Type 2", "Type 2", "Type 2", "Type 2"), model = c("A", 
"A", "A", "A", "A", "B", "B", "B", "B", "B", "C", "C", "C", "C", 
"C", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "C", "C", 
"C", "C", "C")), .Names = c("temp", "rev", "type", "model"), row.names = c(NA, 
-30L), class = "data.frame")

p1 <- ggplot(dat, aes(temp, rev, color = model)) + 
  geom_line() + geom_point() + geom_segment(aes(x = 0, xend = temp, yend = rev), linetype = "dashed", color = "grey") + 
  facet_wrap(~type, scales = "free") + scale_y_continuous(breaks = dat$rev)
p1

Plot

解决方案

I made up dummy data because for some reason pasting your dput into my console made it unhappy.

library(dplyr)

df <- expand.grid(temp = 1:5, model = LETTERS[1:3], type = 1:2) %>% 
  group_by(model, type) %>% 
  mutate(rev = -sort(sample.int(20, length(temp)))) 

# this is equivalent to your data as-is, structurewise


df.labeled <- df %>% 
  ungroup() %>% group_by(type, rev) %>% 
  mutate(label = c(rev[1], rep(NA, length(rev) - 1)))

Here I created a group for each y value that shows up in each group panel. Then I create a label column which consists of 1 observation of that y value, padded with NAs. So if a panel would have had two models that each had a rev of -5, now it would be -5, NA instead of -5, -5. Why I'm doing this will become clearer below.


ggplot(df.labeled, aes(temp, rev, color = model)) + 
  geom_segment(aes(xend = 0, yend = rev), linetype = "dashed", color = "grey") +
  geom_text(aes(label = label, x = -0.1), colour = "black", hjust = 1) +
  geom_vline(xintercept = 0) +
  geom_point() + geom_line() + facet_grid(~type) + 
  scale_y_continuous(breaks = NULL) + 
  scale_x_continuous(limits = c(-0.5, NA)) +
  theme_bw() + theme(panel.grid = element_blank())

If I had left in the duplicates (here would have been -7, -15 for type 1 and -11 for type 2), the geom_text would have been a messy bolded blur. Duplicated text labels don't render well in ggplot2. Since it's not really possible to do it the way you wanted, here we're just making a fake scale for each panel. If you don't like the fact that there's an extra line on the y-axis to the left of the numbers, that can be gotten rid of:

  ... + 
  theme(panel.grid = element_blank(),
        panel.border = element_blank(),
        axis.line.x = element_line())

这篇关于使用facet_wrap和geom_segment删除不必要的y轴点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆