无法在ggplot中使用min()和max()设置xlim和ylim [英] unable to set xlim and ylim using min() and max() in ggplot

查看:106
本文介绍了无法在ggplot中使用min()和max()设置xlim和ylim的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里缺少重要的东西,看不到.

I am missing something crucial here and can't see it.

为什么最小和最大不能设置轴限制?

Why does min and max not work to set the axis limits?

mtcars %>%  
  select(mpg, cyl, disp, wt) %>% 
  filter(complete.cases(disp)) %>% 
  ggplot() +
  geom_point(aes(x=mpg, y=disp, colour=cyl), size=3) +
  xlim(min(mpg, na.rm=TRUE),max(mpg, na.rm=TRUE)) +
  ylim(min(disp, na.rm=TRUE),max(disp, na.rm=TRUE)) +
  scale_colour_gradient(low="red",high="green", name = "cyl")

这有效:

    mtcars %>%  
      select(mpg, cyl, disp, wt) %>% 
      filter(complete.cases(disp)) %>% 
      ggplot() +
      geom_point(aes(x=mpg, y=disp, colour=cyl), size=3) +
#      xlim(min(mpg, na.rm=TRUE),max(mpg, na.rm=TRUE)) +
#      ylim(min(disp, na.rm=TRUE),max(disp, na.rm=TRUE)) +
      scale_colour_gradient(low="red",high="green", name = "cyl")

推荐答案

ggplot无法以dplyr可以访问的方式访问列值.

ggplot cannot access the column values in the way that dplyr can.

您需要添加数据:

mtcars %>%  
  select(mpg, cyl, disp, wt) %>% 
  filter(complete.cases(disp)) %>% 
  ggplot() +
  geom_point(aes(x=mpg, y=disp, colour=cyl), size=3) +
  xlim(min(mtcars$mpg, na.rm=TRUE),max(mtcars$mpg, na.rm=TRUE)) +
  ylim(min(mtcars$disp, na.rm=TRUE),max(mtcars$disp, na.rm=TRUE)) +
  scale_colour_gradient(low="red",high="green", name = "cyl")

这篇关于无法在ggplot中使用min()和max()设置xlim和ylim的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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