在y轴上的下一个值之间留出空间

时间:2019-12-26 13:41:12

标签: r ggplot2

这是我的图表代码:

  ggplot(dane, aes(x=państwo, y=całkowiteZarobki)) + 
  geom_point(aes(col=rodzajSportu, size=całkowiteZarobki)) +
  geom_smooth(method="loess", se=F) + 
  labs(subtitle="Area Vs Population", 
       y="Population", 
       x="Area", 
       title="Scatterplot", 
       caption = "Source: midwest")+
  theme(text = element_text(size=15),axis.text.x = element_text(angle=90, hjust=1))

几个小时以来,我一直在寻找如何解决此问题的方法,可惜我未能找到解决方案。我还按照您的建议添加了数据。这是它的样子:

Rank              Name      Sport    Total.Pay Salary.Winnings Endorsements             Nation Gender Year.of.birth
1    55     Aaron Rodgers   Football  $22,000,000     $14,500,000   $7,500,000      United States   Male          1983
2    95        Adam Scott       Golf  $17,700,000      $8,700,000   $9,000,000          Australia   Male          1980
3    60   Adrian Gonzalez   Baseball  $21,500,000     $21,100,000     $400,000      United States   Male          1982
4    48    Alex Rodriguez   Baseball  $22,900,000     $22,600,000     $300,000      United States   Male          1975
5    93   Alfonso Soriano   Baseball  $18,050,000     $18,000,000      $50,000 Dominican Republic   Male          1976
6    27 Amar'e Stoudemire Basketball  $26,700,000     $21,700,000   $5,000,000      United States   Male          1982
7    75        Barry Zito   Baseball  $20,400,000     $20,300,000     $100,000      United States   Male          1978
8    47     Blake Griffin Basketball  $23,000,000     $16,500,000   $6,500,000      United States   Male          1989
9    91    Branden Albert   Football  $18,435,000     $18,400,000      $35,000      United States   Male          1984
10   97  Brandon Marshall   Football  $17,500,000     $16,800,000     $700,000      United States   Male          1984
11   74     Carl Crawford   Baseball  $20,500,000     $20,200,000     $300,000      United States   Male          1981
12   84     Carlos Dunlap   Football  $18,750,000     $18,700,000      $50,000      United States   Male          1989
13   24   Carmelo Anthony Basketball  $29,400,000     $21,400,000   $8,000,000      United States   Male          1984
14   37       CC Sabathia   Baseball  $24,200,000     $23,000,000   $1,200,000      United States   Male          1980
15   37        Chris Paul Basketball  $24,200,000     $18,700,000   $5,500,000      United States   Male          1985
16   57        Chris Bosh Basketball  $21,800,000     $19,300,000   $2,500,000      United States   Male          1984
17   30         Cliff Lee   Baseball  $25,300,000     $25,100,000     $200,000      United States   Male          1978
18   70       Cole Hamels   Baseball  $20,700,000     $20,500,000     $200,000      United States   Male          1983
19   40    Darrelle Revis   Football  $23,800,000     $23,000,000     $800,000      United States   Male          1985
20   80       David Ortiz   Baseball  $19,800,000     $14,800,000   $5,000,000 Dominican Republic   Male          1975
21   35       Derek Jeter   Baseball  $24,300,000     $15,300,000   $9,000,000      United States   Male          1974
22   79    Deron Williams Basketball  $20,000,000     $18,500,000   $1,500,000      United States   Male          1984
23   13      Derrick Rose Basketball  $36,600,000     $17,600,000  $19,000,000      United States   Male          1988
24   45     Dirk Nowitzki Basketball  $23,200,000     $22,700,000     $500,000            Germany   Male          1978
25   33      Zack Greinke   Baseball  $24,450,000     $24,400,000      $50,000      United States   Male          1983
26   66        Drew Brees   Football  $21,000,000     $10,000,000  $11,000,000      United States   Male          1979
27   29     Dwight Howard Basketball  $25,500,000     $20,500,000   $5,000,000      United States   Male          1985
28   23       Dwyane Wade Basketball  $29,900,000     $18,900,000  $11,000,000      United States   Male          1982
29    6       Tiger Woods       Golf  $61,200,000      $6,200,000  $55,000,000      United States   Male          1975
30   66       Eli Manning   Football  $21,000,000     $13,500,000   $7,500,000      United States   Male          1981
31   11    Manny Pacquiao     Boxing  $41,800,000     $41,000,000     $800,000        Philippines   Male          1978
32   77   Felix Hernandez   Baseball  $20,300,000     $20,100,000     $200,000          Venezuela   Male          1986
33   21   Fernando Alonso     Racing  $31,000,000     $29,000,000   $2,000,000              Spain   Male          1981
34   63   Fernando Torres     Soccer  $21,300,000     $17,800,000   $3,500,000              Spain   Male          1984
35    1  Floyd Mayweather     Boxing $105,000,000    $105,000,000           $0      United States   Male          1977
36   92    Franck Ribéry     Soccer  $18,300,000     $16,800,000   $1,500,000             France   Male          1983
37   14       Gareth Bale     Soccer  $36,400,000     $25,400,000  $11,000,000              Wales   Male          1989
38   53       Geno Atkins   Football  $22,350,000     $22,300,000      $50,000      United States   Male          1988
39   59       Yaya Touré     Soccer  $21,700,000     $19,200,000   $2,500,000        Ivory Coast   Male          1983
40   64    Henrik Stenson       Golf  $21,200,000     $18,000,000   $3,200,000             Sweden   Male          1976
41   94       Jairus Byrd   Football  $18,000,000     $17,900,000     $100,000      United States   Male          1986
42   70      Jason Peters   Football  $20,750,000     $20,700,000      $50,000      United States   Male          1982
43   97      Jayson Werth   Baseball  $17,500,000     $17,300,000     $200,000      United States   Male          1979
44   86       Jeff Gordon     Racing  $18,700,000     $13,700,000   $5,000,000      United States   Male          1971
45   42    Jimmie Johnson     Racing  $23,500,000     $17,000,000   $6,500,000      United States   Male          1975
46   52       Joe Johnson Basketball  $22,500,000     $21,500,000   $1,000,000      United States   Male          1981
47   48     Johan Santana   Baseball  $22,900,000     $22,500,000     $400,000          Venezuela   Male          1979
48   18  Matthew Stafford   Football  $33,000,000     $31,500,000   $1,500,000      United States   Male          1988
49   77     Albert Pujols   Baseball  $20,300,000     $18,300,000   $2,000,000 Dominican Republic   Male          1980
50   32    Miguel Cabrera   Baseball  $24,600,000     $22,600,000   $2,000,000          Venezuela   Male          1983
51   31         Joe Mauer   Baseball  $24,800,000     $23,000,000   $1,800,000      United States   Male          1983
52   62         Joe Haden   Football  $21,350,000     $21,200,000     $150,000      United States   Male          1989
53   69    Julius Peppers   Football  $20,800,000     $20,400,000     $400,000      United States   Male          1980
54   70  Justin Verlander   Baseball  $20,700,000     $20,200,000     $500,000      United States   Male          1983
55   20      Kevin Durant Basketball  $31,900,000     $17,900,000  $14,000,000      United States   Male          1988
56    5       Kobe Bryant Basketball  $61,500,000     $30,500,000  $31,000,000      United States   Male          1978
57   89  Larry Fitzgerald   Football  $18,500,000     $17,000,000   $1,500,000      United States   Male          1983
58    3      LeBron James Basketball  $72,300,000     $19,300,000  $53,000,000      United States   Male          1984
59   19    Lewis Hamilton     Racing  $32,000,000     $29,000,000   $3,000,000            England   Male          1985
60   41             Li Na     Tennis  $23,600,000      $5,600,000  $18,000,000              China Female          1982
61    4      Lionel Messi     Soccer  $64,700,000     $41,700,000  $23,000,000          Argentina   Male          1987
62  100      Luis Suárez     Soccer  $17,300,000     $13,300,000   $4,000,000            Uruguay   Male          1987

As you can see y axis looks terrible please advise me how can I make space between next positions on it to make it look clearer

PS。知道为什么说1亿美元是最低价值吗?显然,它应该在y轴的顶部,但仍在底部。

1 个答案:

答案 0 :(得分:1)

根据@RonakShah和@TobiO提供的注释,您的całkowiteZarobki变量似乎被读取为字符而不是数字(可能是由于$符号和逗号)。要仅提取数字并将其转换为数字,可以使用以下代码:

as.numeric(unlist(lapply(stringr::str_extract_all(dane$całkowiteZarobki,"[0-9]+"),
          function(entry) paste0(entry,collapse=""))))

然后,您可以使用生成的ggplot代码,并添加以下代码以将y轴的格式设置为带逗号的美元。

scale_y_continuous(labels = scales::dollar)