我想堆叠theese 3列,但是我不能这样做,因为它们的值在它们旁边包含$。我试图使用as.numeric来执行此操作,但是没有任何效果。有人可以帮我解决这个问题吗?
这是我的代码:
ggplot(dane, aes(rodzajSportu, fill=(as.numeric(calkowiteZarobki))))+
geom_bar(fill="green",position="stack")+
geom_bar(aes(rodzajSportu, fill=as.numeric(wynagrodzenie)), fill="red",position="stack")+
geom_bar(aes(rodzajSportu, fill=as.numeric(inneZarobki)), fill="gold",position="stack")+
theme(text = element_text(size=15),axis.text.x = element_text(angle=90, hjust=1))+
labs(title = "Diagram ", x="Rodzaj Sportu", y="Liczba zawodnikow")
如果您能告诉我如何将它们堆叠或至少将它们分组到一张图表上,我将不胜感激。
以下是请求的示例数据:
structure(list(Rank = c(55L, 95L, 60L, 48L, 93L, 27L, 75L, 47L,
91L, 97L, 74L, 84L, 24L, 37L, 37L, 57L, 30L, 70L, 40L, 80L),
Name = structure(c(1L, 2L, 3L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
13L, 14L, 15L, 16L, 18L, 17L, 19L, 20L, 23L, 24L), .Label = c("Aaron Rodgers",
"Adam Scott", "Adrian Gonzalez", "Albert Pujols", "Alex Rodriguez",
"Alfonso Soriano", "Amar'e Stoudemire", "Barry Zito", "Blake Griffin",
"Branden Albert", "Brandon Marshall", "Canelo Alvarez", "Carl Crawford",
"Carlos Dunlap", "Carmelo Anthony", "CC Sabathia", "Chris Bosh",
"Chris Paul", "Cliff Lee", "Cole Hamels", "Cristiano Ronaldo",
"Dale Earnhardt Jr.", "Darrelle Revis", "David Ortiz", "Derek Jeter",
"Deron Williams", "Derrick Rose", "Dirk Nowitzki", "Drew Brees",
"Dwight Howard", "Dwyane Wade", "Eli Manning", "Felix Hernandez",
"Fernando Alonso", "Fernando Torres", "Floyd Mayweather",
"Franck Ribéry", "Gareth Bale", "Geno Atkins", "Henrik Stenson",
"Jairus Byrd", "Jason Peters", "Jayson Werth", "Jeff Gordon",
"Jimmie Johnson", "Joe Haden", "Joe Johnson", "Joe Mauer",
"Johan Santana", "Julius Peppers", "Justin Verlander", "Kevin Durant",
"Kobe Bryant", "Larry Fitzgerald", "LeBron James", "Lewis Hamilton",
"Li Na", "Lionel Messi", "Luis SuĂ\241rez", "Mahendra Singh Dhoni",
"Manny Pacquiao", "Maria Sharapova", "Mark Teixeira", "Matt Cain",
"Matt Holliday", "Matt Kemp", "Matt Ryan", "Matthew Stafford",
"Mesut Ă–zil", "Miguel Cabrera", "Neymar", "Novak Djokovic",
"Pau Gasol", "Peyton Manning", "Phil Mickelson", "Prince Fielder",
"Radamel Falcao", "Rafael Nadal", "Robin van Persie", "Robinson Cano",
"Roger Federer", "Rory McIlroy", "Rudy Gay", "Russell Westbrook",
"Ryan Howard", "Sebastian Vettel", "Serena Williams", "Sergio AgĂĽero",
"Steven Gerrard", "Terrell Suggs", "Tiger Woods", "Tim Lincecum",
"Usain Bolt", "Vernon Wells", "Wayne Rooney", "Wladimir Klitschko",
"Yaya Touré", "Zach Randolph", "Zack Greinke", "Zlatan Ibrahimović"
), class = "factor"), Sport = structure(c(5L, 6L, 1L, 1L,
1L, 2L, 1L, 2L, 5L, 5L, 1L, 5L, 2L, 1L, 2L, 2L, 1L, 1L, 5L,
1L), .Label = c("Baseball", "Basketball", "Boxing", "Cricket",
"Football", "Golf", "Racing", "Soccer", "Tennis", "Track"
), class = "factor"), Total.Pay = c(2.2e+07, 17700000, 21500000,
22900000, 18050000, 26700000, 20400000, 2.3e+07, 18435000,
17500000, 20500000, 18750000, 29400000, 24200000, 24200000,
21800000, 25300000, 20700000, 23800000, 19800000), Salary.Winnings = structure(c(10L,
75L, 44L, 52L, 25L, 48L, 38L, 16L, 27L, 17L, 37L, 29L, 46L,
54L, 29L, 32L, 58L, 40L, 54L, 12L), .Label = c("$10,000,000",
"$105,000,000", "$11,000,000", "$12,100,000", "$12,500,000",
"$13,200,000", "$13,300,000", "$13,500,000", "$13,700,000",
"$14,500,000", "$14,700,000", "$14,800,000", "$14,900,000",
"$15,100,000", "$15,300,000", "$16,500,000", "$16,800,000",
"$17,000,000", "$17,200,000", "$17,300,000", "$17,400,000",
"$17,600,000", "$17,800,000", "$17,900,000", "$18,000,000",
"$18,300,000", "$18,400,000", "$18,500,000", "$18,700,000",
"$18,900,000", "$19,200,000", "$19,300,000", "$19,500,000",
"$2,400,000", "$20,000,000", "$20,100,000", "$20,200,000",
"$20,300,000", "$20,400,000", "$20,500,000", "$20,700,000",
"$200,000", "$21,000,000", "$21,100,000", "$21,200,000",
"$21,400,000", "$21,500,000", "$21,700,000", "$21,800,000",
"$22,300,000", "$22,500,000", "$22,600,000", "$22,700,000",
"$23,000,000", "$23,500,000", "$24,000,000", "$24,400,000",
"$25,100,000", "$25,400,000", "$29,000,000", "$30,500,000",
"$31,500,000", "$32,400,000", "$36,400,000", "$4,000,000",
"$4,200,000", "$4,300,000", "$41,000,000", "$41,700,000",
"$42,000,000", "$5,200,000", "$5,600,000", "$52,000,000",
"$6,200,000", "$8,700,000"), class = "factor"), Endorsements = structure(c(44L,
48L, 33L, 28L, 37L, 35L, 6L, 43L, 30L, 45L, 28L, 37L, 46L,
3L, 36L, 15L, 17L, 17L, 47L, 35L), .Label = c("$0", "$1,000,000",
"$1,200,000", "$1,500,000", "$1,800,000", "$100,000", "$11,000,000",
"$12,000,000", "$14,000,000", "$150,000", "$16,000,000",
"$18,000,000", "$19,000,000", "$2,000,000", "$2,500,000",
"$20,000,000", "$200,000", "$21,000,000", "$22,000,000",
"$23,000,000", "$250,000", "$26,000,000", "$28,000,000",
"$3,000,000", "$3,200,000", "$3,500,000", "$30,000,000",
"$300,000", "$31,000,000", "$35,000", "$4,000,000", "$4,500,000",
"$400,000", "$48,000,000", "$5,000,000", "$5,500,000", "$50,000",
"$500,000", "$52,000,000", "$53,000,000", "$55,000,000",
"$6,000,000", "$6,500,000", "$7,500,000", "$700,000", "$8,000,000",
"$800,000", "$9,000,000"), class = "factor"), Nation = structure(c(24L,
2L, 24L, 24L, 6L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L,
24L, 24L, 24L, 24L, 24L, 24L, 6L), .Label = c("Argentina",
"Australia", "Brazil", "China", "Colombia", "Dominican Republic",
"England", "France", "Germany", "India", "Ivory Coast", "Jamaica",
"Mexico", "Netherlands", "Northern Ireland", "Philippines",
"Portugal", "Russia", "Serbia", "Spain", "Sweden", "Switzerland",
"Ukraine", "United States", "Uruguay", "Venezuela", "Wales"
), class = "factor"), Gender = structure(c(2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L), .Label = c("Female", "Male"), class = "factor"), Year.of.birth = c(1983L,
1980L, 1982L, 1975L, 1976L, 1982L, 1978L, 1989L, 1984L, 1984L,
1981L, 1989L, 1984L, 1980L, 1985L, 1984L, 1978L, 1983L, 1985L,
1975L), Birth.Date = structure(c(38L, 20L, 96L, 62L, 90L,
23L, 9L, 18L, 82L, 48L, 86L, 64L, 66L, 42L, 87L, 54L, 75L,
63L, 12L, 32L), .Label = c("08/04/1986", "1/10/1984", "10/02/1986",
"10/10/1974", "11/04/1980", "11/10/1982", "12/11/1988", "13/03/1979",
"13/05/1978", "13/05/1983", "14/04/1989", "14/07/1985", "15/01/1979",
"15/01/1980", "15/06/1984", "15/10/1988", "16/01/1980", "16/03/1989",
"16/06/1970", "16/07/1980", "16/07/1981", "16/07/1989", "16/11/1982",
"17/01/1982", "17/05/1985", "17/08/1986", "17/09/1975", "17/12/1978",
"18/01/1980", "18/04/1983", "18/07/1990", "18/11/1975", "19/04/1983",
"19/04/1987", "19/06/1978", "19/11/1979", "2/06/1988", "2/12/1983",
"20/02/1983", "20/03/1984", "20/05/1979", "21/07/1980", "21/08/1986",
"21/10/1983", "22/01/1982", "22/05/1987", "22/11/1982", "23/03/1984",
"23/08/1978", "23/09/1984", "24/01/1987", "24/02/1977", "24/03/1976",
"24/03/1984", "24/06/1987", "24/10/1985", "25/03/1976", "26/02/1982",
"26/06/1974", "26/06/1984", "26/09/1981", "27/07/1975", "27/12/1983",
"28/02/1989", "28/03/1988", "29/05/1984", "29/06/1981", "29/07/1981",
"29/09/1988", "3/01/1981", "3/06/1986", "3/07/1987", "3/10/1981",
"30/05/1980", "30/08/1978", "30/12/1975", "30/12/1984", "31/08/1983",
"4/05/1989", "4/08/1971", "4/10/1988", "4/11/1984", "5/02/1985",
"5/02/1992", "5/04/1976", "5/08/1981", "6/05/1985", "6/07/1980",
"6/08/1983", "7/01/1976", "7/01/1985", "7/02/1988", "7/04/1983",
"7/07/1981", "7/10/1986", "8/05/1982", "8/08/1981", "8/12/1978",
"8/12/1985", "9/05/1984"), class = "factor"), Place.of.Birth = structure(c(19L,
1L, 75L, 55L, 76L, 40L, 41L, 58L, 69L, 66L, 36L, 56L, 15L,
88L, 27L, 23L, 12L, 75L, 3L, 79L), .Label = c("Adelaide, Australia",
"Akron, Ohio", "Aliquippa, Pennsylvania, United States",
"Annapolis, Maryland", "Atlanta, Georgia, United States",
"Austin, Texas", "Baltimore", "Barcelona, Spain", "Basel, Switzerland",
"Belgrade, Serbia", "Bellevue, Washington", "Benton, Arkansas",
"Bouaké, Ivory Coast", "Boulogne-sur-Mer, France", "Brooklyn, New York City, New York, United States",
"Buenos Aires", "Cardiff, Wales", "Chicago, Illinois, United States",
"Chico, California, United States", "County Down, Northern Ireland, United Kingdom",
"Croxteth, Liverpool, England, United Kingdom", "Cypress, California",
"Dallas, Texas, United States", "Dothan, Alabama", "El Cajon, California, United States",
"Exton, Pennsylvania, United States", "Forsyth County, North Carolina, United States",
"Fort Washington, Maryland, United States", "Fuenlabrada, Spain",
"Funchal, Madeira, Portugal", "Gelsenkirchen, Germany", "Gothenburg, Sweden",
"Grand Rapids, Michigan, United States", "Guadalajara, Jalisco, Mexico",
"Heppenheim, Hesse, Germany", "Houston, United States", "Kannapolis, North Carolina, United States",
"Kazakhstan", "Kibawe, Bukidnon, General Santos, Philippines",
"Lake Wales, Florida", "Las Vegas Valley, Nevada, United States",
"Little Rock, Arkansas", "Long Beach, California", "MĂ©rida, Venezuela",
"Malmö, Sweden", "Manacor, Balearic Islands, Spain", "Manakin-Sabot, Virginia",
"Marion, Indiana", "Midwest City, Oklahoma", "Minneapolis",
"Minneapolis, Minnesota, United States", "Mogi das Cruzes, Brazil",
"New Orleans", "New Orleans, Louisiana, United States", "New York City",
"North Charleston, South Carolina", "Nyagan, Russia", "Oklahoma City, Oklahoma, United States",
"Ontario, California", "Orlando, Florida", "Oviedo, Asturias, Spain",
"Parkersburg, West Virginia", "Pembroke Pines, Florida, United States",
"Pequannock Township, New Jersey", "Philadelphia", "Pittsburgh",
"Queen City, Texas, United States", "Ranchi, Bihar, India",
"Rochester, New York, United States", "Rosario, Santa Fe, Argentina",
"Rotterdam, Netherlands", "Saginaw, Michigan, United States",
"Saint Paul, Minnesota", "Salto, Uruguay", "San Diego, California, United States",
"San Pedro de MacorĂs, Dominican Republic", "Santa Marta, Colombia",
"Santo Domingo", "Santo Domingo, Dominican Republic", "Shreveport, Louisiana",
"Springfield, Illinois", "St. Louis", "Stevenage, Hertfordshire, England, United Kingdom",
"Stillwater, Oklahoma", "Tampa, Florida, United States",
"Trelawny Parish, Jamaica", "Valencia, Venezuela", "Vallejo, California",
"Vallejo, California, United States", "Venezuela, Maracay",
"Washington, D.C.", "West Germany, WĂĽrzburg", "Whiston, Merseyside, England, United States",
"Wilson, North Carolina, United States", "Wuhan, Hubei, China"
), class = "factor"), Height..cm. = c(188L, 183L, 188L, 190L,
185L, 211L, 188L, 208L, 196L, 193L, 188L, 198L, 203L, 201L,
183L, 211L, 190L, 193L, 180L, 195L)
由于包含太多数据(删除链接至Wiki页面,描述以及其他我不使用的列),因此不得不删除几列
答案 0 :(得分:2)
您提供的数据集的列名与代码中的列名不同。.因此,我使用了您提供的数据的子集,它应该适用于较大的数据框。不清楚要绘制什么内容,所以我使用的是体育运动,其中三列都有收益。
数据的一部分:
df = structure(list(Sport = structure(c(3L, 4L, 1L, 1L, 1L, 2L, 1L,
2L, 3L, 3L, 1L, 3L, 2L, 1L, 2L, 2L, 1L, 1L, 3L, 1L), .Label = c("Baseball",
"Basketball", "Football", "Golf"), class = "factor"), Total.Pay = c(2.2e+07,
17700000, 21500000, 22900000, 18050000, 26700000, 20400000, 2.3e+07,
18435000, 17500000, 20500000, 18750000, 29400000, 24200000, 24200000,
21800000, 25300000, 20700000, 23800000, 19800000), Salary.Winnings = structure(c(1L,
18L, 12L, 15L, 5L, 14L, 10L, 3L, 6L, 4L, 9L, 7L, 13L, 16L, 7L,
8L, 17L, 11L, 16L, 2L), .Label = c("$14,500,000", "$14,800,000",
"$16,500,000", "$16,800,000", "$18,000,000", "$18,400,000", "$18,700,000",
"$19,300,000", "$20,200,000", "$20,300,000", "$20,500,000", "$21,100,000",
"$21,400,000", "$21,700,000", "$22,600,000", "$23,000,000", "$25,100,000",
"$8,700,000"), class = "factor"), Endorsements = structure(c(12L,
16L, 7L, 5L, 10L, 8L, 2L, 11L, 6L, 13L, 5L, 10L, 14L, 1L, 9L,
3L, 4L, 4L, 15L, 8L), .Label = c("$1,200,000", "$100,000", "$2,500,000",
"$200,000", "$300,000", "$35,000", "$400,000", "$5,000,000",
"$5,500,000", "$50,000", "$6,500,000", "$7,500,000", "$700,000",
"$8,000,000", "$800,000", "$9,000,000"), class = "factor")), row.names = c(NA,
20L), class = "data.frame")
将包含$的“ $”更改为数字:
df$Salary.Winnings = as.numeric(gsub("[$,]","",as.character(df$Salary.Winnings)))
df$Endorsements = as.numeric(gsub("[$,]","",as.character(df$Endorsements)))
library(dplyr)
library(tidyr)
library(ggplot2)
df %>% pivot_longer(-Sport) %>%
ggplot(aes(x=Sport,y=value,fill=name)) + geom_col()