温馨提示:本文翻译自stackoverflow.com,查看原文请点击:plot - Hiding/Removing part of a curve in R
graphics plot r curve

plot - 在R中隐藏/删除曲线的一部分

发布于 2020-03-27 10:38:45

我想从一组数据绘制曲线时,隐藏满足某些条件的零件,例如,隐藏y轴上所有值大于10的东西。

我不能只是将值设置为0或设置为一个很高的数字,而只能使用xlim或ylim,因为从绘制线型时起,我会有一条垂直线,而我不希望这样。

x <- seq(from=-50,to=50,by=0.1)
#I'd like every part of the curve above 1000 to disappear for example
y<--x^2+2500
plot(x,y,type="l")
y[y>1000]<-0
#this will create two vertical lines
plot(x,y,type="l")

想要:

伊姆古尔

实际结果:

img

查看更多

查看更多

提问者
Philippe Ear
被浏览
266
Max Teflon 2019-07-03 23:38
x <- seq(from=-50,to=50,by=0.1)
y<--x^2+2500
ylims <- range(y)
plot(x,y,type="l",ylim = ylims)

y[y>1000]<-NA
plot(x,y,type="l", ylim = ylims)


## tidyverse ====
x <- seq(from=-50,to=50,by=0.1)
y<--x^2+2500
library(tidyverse)

p <- tibble(x,y) %>%
  mutate(yCutoff = ifelse(y>1000, NA, y)) %>%
  ggplot(aes(x,y)) +
  geom_line(aes(y = yCutoff)) +
  ylim(range(y)) +
  theme_minimal()
p

# your x-Values:
p$data  %>% filter(is.na(yCutoff))%>% select(x)
#> # A tibble: 775 x 1
#>        x
#>    <dbl>
#>  1 -38.7
#>  2 -38.6
#>  3 -38.5
#>  4 -38.4
#>  5 -38.3
#>  6 -38.2
#>  7 -38.1
#>  8 -38  
#>  9 -37.9
#> 10 -37.8
#> # … with 765 more rows