2017-10-18 216 views
2

如何在R中为频率图添加次Y轴?如何在R中的ggplot中添加第二个Y轴?

Frequency Plt

我的代码:

 subset(Test_sorted, freq>500) %>% 
    + ggplot(aes(x=reorder(Test_sorted$word,-freq), y=Test_sorted$freq,fill=Test_sorted$Resi(width=100) + 
        + ggtitle("Top 50 used words")+ 
        + labs(x="Words",y="Count of words") + 
        + geom_bar(stat="identity", position='dodge') + 
        + theme(axis.text.x=element_text(angle=45, hjust=1))) 

structure(list(word = structure(c(277L, 58L, 576L, 445L, 106L, 
472L, 554L, 553L, 289L, 25L, 156L, 38L, 567L, 76L, 630L, 550L, 
635L, 568L, 527L, 320L, 38L, 556L, 609L, 216L, 228L, 228L, 277L, 
212L, 445L, 518L, 96L, 537L, 417L, 550L, 80L, 554L, 141L, 194L, 
605L, 435L, 544L, 54L, 591L, 187L, 412L, 136L, 323L, 112L, 138L, 
571L, 55L, 77L, 261L, 420L, 635L, 260L, 472L, 512L, 219L, 293L, 
339L, 21L, 522L, 48L, 173L, 496L, 25L, 430L, 470L, 51L, 529L, 
433L, 156L, 18L), .Label = c("aberdeen", "academic", "aegean", 
"air", "alice", "amy", "and", "anthony", "apartment", "apleichau", 
"apliu", "arch", "argyle", "ascot", "asia", "attn", "austin", 
"avenue", "baguio", "bailey", "bank", "banyan", "baptist", "bauhinia", 
"bay", "bayview", "beach", "beacon", "bedford", "bel", "belair", 
"belchers", "bellagio", "belleview", "belvedere", "beverly", 
"blk", "block", "blue", "bonham", "boulevard", "bowen", "braemar", 
"broadcast", "broadview", "broadway", "broadwood", "building", 
"business", "caine", "canton", "carnarvon", "casa", "castle", 
"causeway", "celestial", "central", "centre", "century", "cha", 
"chai", "chak", "chan", "chang", "charming", "chatham", "chau", 
"che", "chee", "chelsea", "chen", "cheng", "cheong", "cherry", 
"cheuk", "cheung", "chi", "chik", "chin", "china", "chinese", 
"ching", "chiu", "cho", "choi", "chong", "chow", "chu", "chuen", 
"chui", "chuk", "chun", "chung", "circuit", "cite", "city", "clear", 
"clearwater", "close", "cloud", "club", "coast", "coastal", "college", 
"commercial", "company", "conduit", "connaught", "corporation", 
"cou", "cour", "court", "cove", "crescent", "crest", "crystal", 
"dat", "davis", "deep", "department", "des", "design", "development", 
"diamond", "discovery", "district", "double", "dragon", "drive", 
"dynasty", "east", "eastern", "education", "edward", "electric", 
"electronics", "engineering", "estate", "evergreen", "express", 
"factory", "fai", "fairview", "fan", "fanling", "fat", "fei", 
"ferry", "festival", "finance", "five", "fla", "flat", "flata", 
"flatrm", "floor", "flora", "flt", "fok", "fong", "foo", "fook", 
"foon", "for", "fortress", "fortune", "front", "fuk", "full", 
"fun", "fung", "gap", "garden", "gardens", "global", "gloucester", 
"gold", "golden", "grace", "grand", "grandview", "great", "green", 
"greenview", "greenwood", "ground", "group", "hai", "hall", "han", 
"hang", "hanoi", "happy", "harbour", "harbourview", "harmony", 
"hau", "haven", "hei", "height", "heights", "heng", "hennessy", 
"heung", "hhp", "high", "hill", "hills", "hillwood", "him", "hin", 
"hing", "hip", "hiu", "hkg", "hoi", "hok", "hollywood", "hom", 
"hon", "hong", "hongkong", "hop", "horizon", "horizons", "hospital", 
"hotel", "house", "huang", "hui", "hung", "hunghom", "imperial", 
"industrial", "international", "island", "jade", "jardines", 
"java", "jockey", "john", "johnston", "jordan", "joyful", "jubilee", 
"kai", "kam", "kan", "kar", "kau", "kee", "kei", "kellet", "kellett", 
"keng", "kennedy", "kenswood", "keung", "kin", "king", "kings", 
"kingswood", "kip", "kit", "kiu", "kln", "kok", "kon", "kong", 
"koon", "kornhill", "kotewall", "kow", "kowl", "kowlo", "kowloo", 
"kowloon", "ktz", "ktza", "kuen", "kui", "kung", "kwa", "kwai", 
"kwan", "kwok", "kwong", "kwu", "kwun", "laguna", "lai", "lake", 
"lam", "lan", "lane", "lantau", "lap", "larvotto", "lau", "law", 
"lee", "lei", "leighton", "leng", "leung", "level", "levels", 
"liberte", "life", "lift", "lim", "limited", "lin", "ling", "link", 
"liu", "lockhart", "lodge", "loft", "logistics", "lohas", "loi", 
"lok", "long", "lookout", "lot", "lower", "ltd", "lucky", "luen", 
"luk", "lun", "lung", "lyttelton", "macdonnell", "magazine", 
"main", "mak", "man", "management", "manhattan", "mansion", "marina", 
"market", "masterpiece", "mau", "may", "mayfair", "medical", 
"mei", "melody", "merton", "metro", "michael", "mid", "midlevels", 
"ming", "miss", "miu", "mong", "monte", "moon", "mount", "mrs", 
"mtr", "mui", "muk", "mun", "nai", "nam", "nan", "nassau", "natalie", 
"nathan", "nelson", "new", "nga", "ngai", "ngan", "ngau", "nil", 
"ning", "noble", "north", "oasis", "ocean", "office", "old", 
"one", "orchards", "oxford", "pacific", "pak", "palm", "pan", 
"pang", "par", "paradiso", "parc", "park", "parkview", "pat", 
"paterson", "path", "pau", "peace", "peak", "pearl", "peninsula", 
"perth", "peter", "phase", "pik", "pin", "pine", "ping", "place", 
"plaza", "poi", "point", "pok", "pool", "poon", "poplar", "portion", 
"praya", "prince", "private", "pro", "pui", "pun", "qdm", "quarry", 
"queen", "queens", "redhill", "repulse", "reservoir", "residence", 
"resort", "richland", "rise", "riviera", "robinson", "room", 
"route", "royal", "ruby", "run", "sai", "salle", "sam", "sammi", 
"san", "sang", "sar", "sau", "sceneway", "scenic", "school", 
"sci", "science", "sea", "seasons", "seaview", "section", "seng", 
"serene", "serenity", "services", "seymour", "sha", "sham", "shan", 
"shang", "shanghai", "shap", "shatin", "shau", "shaw", "shek", 
"sheng", "shenzhen", "sheung", "shi", "shin", "shing", "shirley", 
"shiu", "shop", "shopping", "shores", "shouson", "shu", "shue", 
"shui", "shuk", "shum", "shun", "shung", "sik", "silver", "sin", 
"sing", "site", "siu", "sky", "skyline", "smart", "smithfield", 
"south", "square", "stanley", "star", "stubbs", "student", "sui", 
"suite", "suites", "sum", "summit", "sun", "sung", "sunshine", 
"systems", "sze", "tai", "taikoo", "tak", "tam", "tan", "tang", 
"tao", "tat", "tau", "tech", "technology", "tei", "telford", 
"temple", "ter", "terrace", "territor", "territories", "territory", 
"thai", "the", "third", "tierra", "tin", "ting", "toi", "tong", 
"tower", "town", "trading", "transportation", "tregunter", "tsai", 
"tsang", "tse", "tseng", "tseung", "tsim", "tsin", "tsing", "tsuen", 
"tsui", "tsun", "tsz", "tuen", "tung", "twisk", "two", "union", 
"unit", "united", "university", "upper", "valley", "ventris", 
"verde", "victoria", "view", "villa", "village", "villas", "vista", 
"voeux", "wah", "wai", "wan", "wang", "warehouse", "wat", "water", 
"waterfront", "waterloo", "way", "wei", "well", "wellington", 
"wendy", "wes", "west", "western", "whampoa", "win", "wing", 
"wong", "woo", "workshop", "world", "wui", "wun", "wylie", "xkt", 
"yam", "yan", "yang", "yat", "yau", "yaumatei", "yee", "yen", 
"yeung", "yick", "yin", "ying", "yip", "yiu", "yoho", "yuan", 
"yue", "yuen", "yuet", "yuk", "yum", "yun", "yung", "zhang", 
"zheng", "ztw", "cent", "container", "far", "units"), class = "factor"), 
    freq = c(16634, 13318, 10373, 9003, 7451, 7420, 6251, 5627, 
    5588, 4430, 4213, 3922, 3687, 3639, 3515, 3178, 3175, 3075, 
    2808, 2668, 2646, 2301, 2270, 2229, 2032, 2017, 1914, 1823, 
    1749, 1699, 1601, 1573, 1536, 1494, 1487, 1390, 1357, 1348, 
    1325, 1268, 1264, 1212, 1201, 1103, 1098, 1087, 1064, 1061, 
    1020, 982, 978, 976, 972, 948, 945, 934, 914, 908, 896, 895, 
    868, 859, 846, 844, 823, 800, 796, 793, 791, 786, 754, 749, 
    748, 748), Resi = c("N", "N", "N", "N", "N", "N", "N", "N", 
    "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", 
    "Y", "N", "N", "N", "Y", "N", "Y", "N", "Y", "N", "N", "N", 
    "N", "Y", "N", "Y", "N", "N", "N", "N", "N", "N", "N", "N", 
    "N", "N", "N", "Y", "Y", "N", "N", "N", "N", "N", "Y", "N", 
    "Y", "N", "N", "N", "N", "N", "N", "Y", "Y", "Y", "Y", "N", 
    "N", "N", "N", "N", "Y", "N")), .Names = c("word", "freq", 
"Resi"), row.names = c("37", "9", "75", "541", "15", "571", "701", 
"69", "381", "3", "24", "4", "72", "10", "821", "68", "84", "73", 
"62", "41", "26", "71", "80", "29", "144", "321", "177", "28", 
"290", "601", "14", "661", "49", "366", "12", "368", "22", "27", 
"79", "531", "671", "6", "78", "261", "48", "19", "42", "70", 
"88", "74", "7", "113", "35", "50", "415", "34", "309", "59", 
"31", "391", "45", "2", "61", "32", "111", "327", "18", "51", 
"56", "510", "64", "52", "99", "1"), class = "data.frame") 
+0

我会尝试的功能sec_axis()或dup_axis()的定义如下:HTTP://ggplot2.tidyverse。 org/reference/sec_axis.html –

+0

@ RichardLusch,我的辅助轴的尺寸也应该很小 –

+0

您可以使用'dput(Test_排序)'? – brettljausn

回答

1
df$word <- factor(df$word, levels=unique(as.character(df$word))) 

ggplot(df, aes(x=word, y=freq, fill=Resi)) + 
    geom_bar(stat="identity", position='dodge') + 
    scale_y_continuous("Count of words", sec.axis = sec_axis(~., name = "Count of words")) + 
    labs(title = "Top 50 used words", x="Words") + 
    theme(axis.text.x=element_text(angle=45, hjust=1)) 

enter image description here

+0

如何将Y和N重命名为标签中的其他名称 –

+0

将Resi变量中的标签更改为类似'df $ Resi < - factor(df $在运行ggplot之前,Resi,levels = c(“N”,“Y”),labels = c(“no”,“yes”))。 –

相关问题