证明ggplot中的标签和标签透明度

时间:2016-01-14 12:28:37

标签: r ggplot2 label

我有以下数据:

new_pairs
     x    y Freq start.latittude start.longitude              start.station end.latitude
1  359  519  929        40.75188       -73.97770         Pershing\nSquare N     40.75510
2  477  465 5032        40.75514       -73.98658        Broadway &\nW 41 St     40.75641
3  484  519 1246        40.75188       -73.97770         Pershing\nSquare N     40.75500
4  484  318 2654        40.75320       -73.97799 E 43 St &\nVanderbilt\nAve     40.75500
5  492  267 1828        40.75098       -73.98765        Broadway &\nW 36 St     40.75020
6  492  498  957        40.74855       -73.98808        Broadway &\nW 32 St     40.75020
7  492  362 1405        40.75173       -73.98754        Broadway &\nW 37 St     40.75020
8  493  477 1582        40.75641       -73.99003           W 41 St &\n8 Ave     40.75680
9  493  529  728        40.75757       -73.99099           W 42 St &\n8 Ave     40.75680
10 529 2021 1748        40.75929       -73.98860           W 45 St &\n8 Ave     40.75757
   end.longitude        end.station                             interaction
1      -73.97499 E 47 St &\nPark Av     E 47 St &Park Av > PershingSquare N
2      -73.99003   W 41 St &\n8 Ave      W 41 St &8 Ave > Broadway &W 41 St
3      -73.98014   W 44 St &\n5 Ave       W 44 St &5 Ave > PershingSquare N
4      -73.98014   W 44 St &\n5 Ave W 44 St &5 Ave > E 43 St &VanderbiltAve
5      -73.99093   W 33 St &\n7 Ave      W 33 St &7 Ave > Broadway &W 36 St
6      -73.99093   W 33 St &\n7 Ave      W 33 St &7 Ave > Broadway &W 32 St
7      -73.99093   W 33 St &\n7 Ave      W 33 St &7 Ave > Broadway &W 37 St
8      -73.98291   W 45 St &\n6 Ave         W 45 St &6 Ave > W 41 St &8 Ave
9      -73.98291   W 45 St &\n6 Ave         W 45 St &6 Ave > W 42 St &8 Ave
10     -73.99099   W 42 St &\n8 Ave         W 42 St &8 Ave > W 45 St &8 Ave

我想更改图表,以便所有标签都适合中心并根据Freq更改透明度,以便较低Freq更透明,更高Freq不太透明

ggplot(data= new_pairs, aes(x= reorder(interaction, -Freq), y=Freq))+ geom_bar(stat="identity", aes(fill = Freq, alpha = .7)) + ylab("Bikes received")+ xlab("Station")+ geom_text(aes(x = interaction, label = interaction), vjust="inward",hjust = "inward", size = 4, nudge_y = 1, fontface ="bold")+theme(axis.text.y=element_blank())+ggtitle("Bikes received viarebalancing")+coord_flip()+theme(legend.position = "none")

dput(new_pairs)
structure(list(x = structure(c(146L, 253L, 260L, 260L, 268L, 
268L, 268L, 269L, 269L, 304L), .Label = c("72", "79", "82", "83", 
"116", "119", "120", "127", "128", "137", "143", "144", "146", 
"147", "150", "151", "152", "153", "157", "160", "161", "164", 
"167", "168", "173", "174", "195", "212", "216", "217", "218", 
"223", "224", "225", "228", "229", "232", "233", "236", "237", 
"238", "239", "241", "242", "243", "244", "245", "247", "248", 
"249", "250", "251", "252", "253", "254", "257", "258", "259", 
"260", "261", "262", "263", "264", "265", "266", "267", "268", 
"270", "271", "274", "275", "276", "278", "279", "280", "281", 
"282", "284", "285", "289", "290", "291", "293", "294", "295", 
"296", "297", "298", "300", "301", "302", "303", "304", "305", 
"306", "307", "308", "309", "310", "311", "312", "313", "314", 
"315", "316", "317", "318", "319", "320", "321", "322", "323", 
"324", "325", "326", "327", "328", "329", "330", "331", "332", 
"334", "335", "336", "337", "339", "340", "341", "342", "343", 
"344", "345", "346", "347", "348", "349", "350", "351", "352", 
"353", "354", "355", "356", "357", "358", "359", "360", "361", 
"362", "363", "364", "365", "366", "367", "368", "369", "372", 
"373", "375", "376", "377", "379", "380", "382", "383", "384", 
"385", "386", "387", "388", "389", "390", "391", "392", "393", 
"394", "395", "396", "397", "398", "399", "400", "401", "402", 
"403", "404", "405", "406", "407", "408", "409", "410", "411", 
"412", "414", "415", "416", "417", "418", "419", "420", "421", 
"422", "423", "426", "427", "428", "430", "431", "432", "433", 
"434", "435", "436", "437", "438", "439", "440", "441", "442", 
"443", "444", "445", "446", "447", "448", "449", "450", "453", 
"454", "455", "456", "457", "458", "459", "460", "461", "462", 
"463", "464", "465", "466", "467", "468", "469", "470", "471", 
"472", "473", "474", "475", "476", "477", "478", "479", "480", 
"481", "482", "483", "484", "485", "486", "487", "488", "489", 
"490", "491", "492", "493", "494", "495", "496", "497", "498", 
"499", "500", "501", "502", "503", "504", "505", "507", "508", 
"509", "510", "511", "512", "513", "514", "515", "516", "517", 
"518", "519", "520", "521", "522", "523", "524", "525", "526", 
"527", "528", "529", "530", "531", "532", "533", "534", "536", 
"537", "538", "539", "540", "545", "546", "2000", "2002", "2003", 
"2004", "2005", "2006", "2008", "2009", "2010", "2012", "2017", 
"2021", "2022", "2023", "3002"), class = "factor"), y = structure(c(294L, 
241L, 294L, 107L, 66L, 274L, 149L, 253L, 304L, 327L), .Label = c("72", 
"79", "82", "83", "116", "119", "120", "127", "128", "137", "143", 
"144", "146", "147", "150", "151", "152", "153", "157", "160", 
"161", "164", "167", "168", "173", "174", "195", "212", "216", 
"217", "218", "223", "224", "225", "228", "229", "232", "233", 
"236", "237", "238", "239", "241", "242", "243", "244", "245", 
"247", "248", "249", "250", "251", "252", "253", "254", "257", 
"258", "259", "260", "261", "262", "263", "264", "265", "266", 
"267", "268", "270", "271", "274", "275", "276", "278", "279", 
"280", "281", "282", "284", "285", "289", "290", "291", "293", 
"294", "295", "296", "297", "298", "300", "301", "302", "303", 
"304", "305", "306", "307", "308", "309", "310", "311", "312", 
"313", "314", "315", "316", "317", "318", "319", "320", "321", 
"322", "323", "324", "325", "326", "327", "328", "329", "330", 
"331", "332", "334", "335", "336", "337", "339", "340", "341", 
"342", "343", "344", "345", "346", "347", "348", "349", "350", 
"351", "352", "353", "354", "355", "356", "357", "358", "359", 
"360", "361", "362", "363", "364", "365", "366", "367", "368", 
"369", "372", "373", "375", "376", "377", "379", "380", "382", 
"383", "384", "385", "386", "387", "388", "389", "390", "391", 
"392", "393", "394", "395", "396", "397", "398", "399", "400", 
"401", "402", "403", "404", "405", "406", "407", "408", "409", 
"410", "411", "412", "414", "415", "416", "417", "418", "419", 
"420", "421", "422", "423", "426", "427", "428", "430", "431", 
"432", "433", "434", "435", "436", "437", "438", "439", "440", 
"441", "442", "443", "444", "445", "446", "447", "448", "449", 
"450", "453", "454", "455", "456", "457", "458", "459", "460", 
"461", "462", "463", "464", "465", "466", "467", "468", "469", 
"470", "471", "472", "473", "474", "475", "476", "477", "478", 
"479", "480", "481", "482", "483", "484", "485", "486", "487", 
"488", "489", "490", "491", "492", "493", "494", "495", "496", 
"497", "498", "499", "500", "501", "502", "503", "504", "505", 
"507", "508", "509", "510", "511", "512", "513", "514", "515", 
"516", "517", "518", "519", "520", "521", "522", "523", "524", 
"525", "526", "527", "528", "529", "530", "531", "532", "533", 
"534", "536", "537", "538", "539", "540", "545", "546", "2000", 
"2002", "2003", "2004", "2006", "2008", "2009", "2010", "2012", 
"2017", "2021", "2022", "2023", "3002"), class = "factor"), Freq = c(929L, 
5032L, 1246L, 2654L, 1828L, 957L, 1405L, 1582L, 728L, 1748L), 
    start.latittude = c(40.75188406, 40.75513557, 40.75188406, 
    40.75320159, 40.75097711, 40.74854862, 40.75172632, 40.75640548, 
    40.7575699, 40.75929124), start.longitude = c(-73.97770164, 
    -73.98658032, -73.97770164, -73.9779874, -73.98765428, -73.98808416, 
    -73.98753523, -73.9900262, -73.99098507, -73.98859651), start.station = c("Pershing\nSquare N", 
    "Broadway &\nW 41 St", "Pershing\nSquare N", "E 43 St &\nVanderbilt\nAve", 
    "Broadway &\nW 36 St", "Broadway &\nW 32 St", "Broadway &\nW 37 St", 
    "W 41 St &\n8 Ave", "W 42 St &\n8 Ave", "W 45 St &\n8 Ave"
    ), end.latitude = c(40.75510267, 40.75640548, 40.75500254, 
    40.75500254, 40.75019995, 40.75019995, 40.75019995, 40.7568001, 
    40.7568001, 40.7575699), end.longitude = c(-73.97498696, 
    -73.9900262, -73.98014437, -73.98014437, -73.99093085, -73.99093085, 
    -73.99093085, -73.98291153, -73.98291153, -73.99098507), 
    end.station = c("E 47 St &\nPark Av", "W 41 St &\n8 Ave", 
    "W 44 St &\n5 Ave", "W 44 St &\n5 Ave", "W 33 St &\n7 Ave", 
    "W 33 St &\n7 Ave", "W 33 St &\n7 Ave", "W 45 St &\n6 Ave", 
    "W 45 St &\n6 Ave", "W 42 St &\n8 Ave"), interaction = c("E 47 St &Park Av > PershingSquare N", 
    "W 41 St &8 Ave > Broadway &W 41 St", "W 44 St &5 Ave > PershingSquare N", 
    "W 44 St &5 Ave > E 43 St &VanderbiltAve", "W 33 St &7 Ave > Broadway &W 36 St", 
    "W 33 St &7 Ave > Broadway &W 32 St", "W 33 St &7 Ave > Broadway &W 37 St", 
    "W 45 St &6 Ave > W 41 St &8 Ave", "W 45 St &6 Ave > W 42 St &8 Ave", 
    "W 42 St &8 Ave > W 45 St &8 Ave")), .Names = c("x", "y", 
"Freq", "start.latittude", "start.longitude", "start.station", 
"end.latitude", "end.longitude", "end.station", "interaction"
), row.names = c(NA, -10L), class = "data.frame")`

enter image description here

2 个答案:

答案 0 :(得分:7)

这是一个选项:

ggplot(data= new_pairs, aes(x= reorder(interaction, -Freq), y=Freq))+ 
  geom_bar(stat="identity", aes(fill = Freq, alpha = Freq)) + 
  ylab("Bikes received")+ xlab("Station")+ 
  ylim(0, max(new_pairs$Freq)+50) +
  geom_text(aes(label = interaction,y=(max(new_pairs$Freq)+50)/2,alpha = Freq), vjust="center",hjust = "center", size = 4, nudge_y = 1, fontface ="bold")+
  theme(axis.text.y=element_blank())+ggtitle("Bikes received viarebalancing")+
  coord_flip()+theme(legend.position = "none")

您可以在y geom_text设置aes值,将标签放在您想要的位置(使用coord_flip,以便更改y控制水平放置文本)。

我手动设置ylim,以便能够max(new_pairs$Freq)+50将文字标签居中。

enter image description here

答案 1 :(得分:1)

如果您想将文本居中放在每个栏中,这里有一个解决方案(基于我上面链接的主题):

library(plyr)
new_pairs <- ddply(new_pairs, .(interaction), transform, pos = cumsum(Freq) - (0.5 * Freq))

ggplot(data= new_pairs, aes(x= reorder(interaction, -Freq), y=Freq))+ 
  geom_bar(stat="identity", aes(fill = Freq, alpha = Freq)) + 
  ylab("Bikes received")+ xlab("Station")+ 
  geom_text(aes(label = interaction, y = pos, alpha = Freq), vjust="center",hjust = "center", size = 4, nudge_y = 1, fontface ="bold")+
  theme(axis.text.y=element_blank())+ggtitle("Bikes received via rebalancing")+
  coord_flip()+theme(legend.position = "none")

enter image description here