使用ggplot绘图

时间:2018-09-25 04:54:32

标签: r ggplot2

我使用以下代码:

library(ggplot2)

ggplot(data = mpg) +  
  geom_point(aes(x = displ^2, x = hwy)
             mpgfil = fliter(mpg, cyl = 8)
             mpg_manu= select(mpg, starts_with("man") & ends_with("rv")

会发生很多各种各样的错误。

任何建议都值得赞赏。

1 个答案:

答案 0 :(得分:0)

对于ggplot部分,您在代码中输入了错字:第二个参数应从x替换为y,因为要绘制的点应具有两个坐标。

对于filterselect助手,您有多种错别字,并且不能使用&运算符来创建列选择条件:

请在下面查看更正的代码

library(ggplot2)
library(dplyr)

ggplot(data = mpg) +  geom_point(aes(x = displ^2, y = hwy)) 
mpgfil <- filter(mpg, cyl == 8)
head(mpgfil)
# # A tibble: 6 x 11
# manufacturer model              displ  year   cyl trans    drv     cty   hwy fl    class  
# <chr>        <chr>              <dbl> <int> <int> <chr>    <chr> <int> <int> <chr> <chr>  
#   1 audi         a6 quattro           4.2  2008     8 auto(s6) 4        16    23 p     midsize
# 2 chevrolet    c1500 suburban 2wd   5.3  2008     8 auto(l4) r        14    20 r     suv    
# 3 chevrolet    c1500 suburban 2wd   5.3  2008     8 auto(l4) r        11    15 e     suv    
# 4 chevrolet    c1500 suburban 2wd   5.3  2008     8 auto(l4) r        14    20 r     suv    
# 5 chevrolet    c1500 suburban 2wd   5.7  1999     8 auto(l4) r        13    17 r     suv    
# 6 chevrolet    c1500 suburban 2wd   6    2008     8 auto(l4) r        12    17 r     suv   

mpg_manu <- select(mpg, starts_with("man")) %>%  select(ends_with("rv"))
head(mpg_manu)
# A tibble: 6 x 0

输出: graph