2016-11-23 43 views
2

我希望由方面分隔因素,如Hadley的ggplot2書第154頁所示,但我的代碼包含了每個方面的所有因素。不知道我做錯了什麼。下面的代碼:將因素分解爲多個方面?

tw %>% 
    ggplot(mapping = aes(x = Likes, y = PlayerName)) + 
    ylab("Players") + xlab("Likes per Tweet") + 
    ggtitle("Engagement per Player") + 
    scale_color_manual(values=c("#5DADE2", "#333333", "#A569BD")) + 
    facet_grid(Sport ~., space = "free") + 
    geom_point() 

tw樣本如下:

tw <- structure(list(PlayerName = structure(c(6L, 11L, 1L, 1L, 3L, 
15L, 12L, 15L, 3L, 14L, 15L, 13L, 15L, 11L, 15L, 11L, 15L, 12L, 
8L, 12L, 2L, 5L, 1L, 14L, 15L, 15L, 3L, 5L, 15L, 13L, 9L, 12L, 
11L, 11L, 14L, 5L, 3L, 6L, 10L, 1L, 13L, 9L, 8L, 14L, 11L, 4L, 
15L, 12L, 6L, 13L, 11L, 13L, 5L, 8L, 1L, 5L, 6L, 13L, 14L, 10L, 
10L, 13L, 10L, 6L, 14L, 12L, 2L, 3L, 12L, 5L, 13L, 14L, 10L, 
6L, 9L, 5L, 6L, 13L, 5L, 6L, 15L, 8L, 4L, 4L, 12L, 5L, 8L, 10L, 
15L, 12L, 11L, 3L, 10L, 6L, 3L, 13L, 12L, 8L, 14L, 13L, 15L, 
11L, 9L), .Label = c("Chris Paul", "Dwayne Wade", "Jeremy Lin", 
"LeBron James", "Steph Curry", "J.J. Watt", "Odell Beckham Jr", 
"Richard Sherman", "Rob Gronkowski", "Russell Wilson", "Cristiano Ronaldo", 
"Gareth Bale", "Mesut Ozil", "Neymar Jr", "Paul Pogba"), class = "factor"), 
    Sport = c("Football", "Soccer", "Basketball", "Basketball", 
    "Basketball", "Soccer", "Soccer", "Soccer", "Basketball", 
    "Soccer", "Soccer", "Soccer", "Soccer", "Soccer", "Soccer", 
    "Soccer", "Soccer", "Soccer", "Football", "Soccer", "Basketball", 
    "Basketball", "Basketball", "Soccer", "Soccer", "Soccer", 
    "Basketball", "Basketball", "Soccer", "Soccer", "Football", 
    "Soccer", "Soccer", "Soccer", "Soccer", "Basketball", "Basketball", 
    "Football", "Football", "Basketball", "Soccer", "Football", 
    "Football", "Soccer", "Soccer", "Basketball", "Soccer", "Soccer", 
    "Football", "Soccer", "Soccer", "Soccer", "Basketball", "Football", 
    "Basketball", "Basketball", "Football", "Soccer", "Soccer", 
    "Football", "Football", "Soccer", "Football", "Football", 
    "Soccer", "Soccer", "Basketball", "Basketball", "Soccer", 
    "Basketball", "Soccer", "Soccer", "Football", "Football", 
    "Football", "Basketball", "Football", "Soccer", "Basketball", 
    "Football", "Soccer", "Football", "Basketball", "Basketball", 
    "Soccer", "Basketball", "Football", "Football", "Soccer", 
    "Soccer", "Soccer", "Basketball", "Football", "Football", 
    "Basketball", "Soccer", "Soccer", "Football", "Soccer", "Soccer", 
    "Soccer", "Soccer", "Football"), Likes = c(644L, 15369L, 
    656L, 758L, 752L, 10776L, 1838L, 4557L, 728L, 7100L, 7506L, 
    7907L, 1933L, 32945L, 1619L, 9450L, 7241L, 1154L, 442L, 5315L, 
    2300L, 7300L, 2500L, 6700L, 7778L, 8454L, 1600L, 3500L, 1615L, 
    7296L, 647L, 3753L, 13887L, 22775L, 13000L, 11000L, 1600L, 
    1400L, 948L, 933L, 6904L, 3100L, 104L, 12000L, 31200L, 13700L, 
    2674L, 16891L, 1800L, 27629L, 56053L, 6257L, 12000L, 1600L, 
    2400L, 3900L, 275L, 6208L, 8300L, 3900L, 1200L, 6500L, 1900L, 
    18000L, 2200L, 9522L, 95L, 800L, 6406L, 14000L, 4976L, 12000L, 
    1200L, 3000L, 1000L, 25000L, 741L, 9761L, 12000L, 559L, 2079L, 
    541L, 1129L, 4389L, 2197L, 34000L, 1200L, 1900L, 2222L, 1890L, 
    26767L, 2452L, 651L, 7300L, 3300L, 3823L, 4941L, 215L, 13000L, 
    11994L, 926L, 11946L, 561L)), row.names = c(194L, 529L, 902L, 
925L, 794L, 295L, 677L, 366L, 828L, 960L, 291L, 390L, 378L, 517L, 
310L, 578L, 293L, 678L, 238L, 635L, 713L, 736L, 928L, 946L, 281L, 
303L, 793L, 753L, 349L, 430L, 89L, 649L, 541L, 527L, 987L, 743L, 
804L, 140L, 48L, 894L, 426L, 71L, 227L, 1026L, 501L, 861L, 329L, 
595L, 186L, 393L, 523L, 461L, 776L, 242L, 887L, 758L, 171L, 429L, 
937L, 57L, 7L, 432L, 21L, 133L, 934L, 675L, 725L, 795L, 583L, 
779L, 467L, 1007L, 49L, 185L, 106L, 766L, 158L, 443L, 780L, 143L, 
321L, 229L, 840L, 872L, 600L, 769L, 260L, 63L, 372L, 670L, 492L, 
830L, 1L, 160L, 798L, 441L, 660L, 221L, 1005L, 478L, 344L, 526L, 
114L), class = "data.frame", .Names = c("PlayerName", "Sport", 
"Likes")) 

這裏的情節: enter image description here

+3

你想'尺度= 「免費」'? – aosmith

+0

查看'facet_grid()'的[ggplot2文檔](http://docs.ggplot2.org/current/facet_grid.html)。 –

回答

3

隨着scales = "free",而不是space = "free"在調用facet_grid()我得到

enter image description here

這是你所期望的嗎?

代碼:

library(ggplot2) 
ggplot(tw, aes(x = Likes, y = PlayerName)) + 
    ylab("Players") + xlab("Likes per Tweet") + 
    ggtitle("Engagement per Player") + 
    scale_color_manual(values = c("#5DADE2", "#333333", "#A569BD")) + 
    facet_grid(Sport ~., scales = "free") + 
    geom_point()