仅显示图例

时间:2017-12-26 19:32:23

标签: r ggplot2 shiny

考虑以下示例:

library(ggplot2)
df = data.frame(x = 1:5, y = 1:5, z = c('a', 'a', 'a', 'b', 'b'))

ggplot(df, aes(x, y, col = z)) + geom_line() + geom_point() +
  coord_cartesian(xlim = c(1, 2.5))

enter image description here

仅显示a广告资源中的数据,但图例中仍显示ab。我该如何解决这个问题,以便只有实际绘制的存储桶出现在图例中?

对于上下文 - 我在shiny中尝试zooming into plots时遇到此问题。

1 个答案:

答案 0 :(得分:6)

z上的过滤是否涵盖了您的实际用例?例如:

library(tidyverse)

df = data.frame(x = 1:5, y = 1:5, z = c('a', 'a', 'a', 'b', 'b'))

ggplot(df %>% filter(z %in% z[between(x,1,2.5)]), 
       aes(x, y, col = z)) + 
  geom_line() + geom_point() +
  coord_cartesian(xlim = c(1, 2.5))

或者可以进一步推广用户输入的美学变量的函数。 (我还更新了函数,即使在不包含数据点的绘图区域中也使用插值绘制线条,只要点之间至少有一条连接线穿过绘图区域。)

my_plot = function(xrng, data=df, step=0.01) {

  levs = unique(data[["z"]])  
  n = length(levs)

  # Generate interpolated data frame so we can plot lines even if 
  # no points appear in the graph region 
  dat_interp = split(data, data$z) %>% 
    map_df(function(d) {
      x = seq(min(d$x), max(d$x), step)
      data.frame(z=rep(unique(d$z), each=length(x)),
                 x, y=rep(approx(d$x, d$y, xout=x)$y, n))
    })

  ggplot(dat_interp %>% filter(z %in% z[between(x,xrng[1],xrng[2])]), 
         aes(x, y, col = z)) + 
    geom_point(data=data %>% filter(z %in% z[between(x,xrng[1],xrng[2])])) +
    geom_line() + 
    coord_cartesian(xlim = xrng) +
    scale_color_manual(values=setNames(hcl(seq(15,375,length=n+1)[1:n],100,65), levs))
}


gridExtra::grid.arrange(
  my_plot(c(1,2.5)),
  my_plot(c(1,4)),
  my_plot(c(3,4)),
  my_plot(c(4.3,6)),
  my_plot(c(1.1,1.6)),
  my_plot(c(4.2,4.9)))

enter image description here