【问题标题】:justifying labels and label transparency in ggplot在ggplot中证明标签和标签透明度
【发布时间】:2016-04-19 18:47:18
【问题描述】:

我有以下数据:

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("收到的自行车")+ xlab("站")+ geom_text(aes(x = 交互,标签 = 交互),vjust="inward",hjust = "inward",size = 4,nudge_y = 1,fontface="bold")+ theme(axis.text.y=element_blank())+ggtitle("通过重新平衡收到的自行车")+coord_flip()+theme(legend.position = "none") 输入(new_pairs) 结构(列表(x = 结构(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=“因子”),y =结构(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= "因子")、频率 = c(929L, 5032L、1246L、2654L、1828L、957L、1405L、1582L、728L、1748L)、 开始纬度 = 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", "百老汇 &\nW 36 街", "百老汇 &\nW 32 街", "百老汇 &\nW 37 街", “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"), 交互作用 = 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”, “西 33 街和 7 大道 > 百老汇和西 32 街”,“西 33 街和 7 大道 > 百老汇和西 37 街”, “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”、“交互” ), row.names = c(NA, -10L), class= "data.frame")`

【问题讨论】:

  • 您可以使用dput() 来显示您的数据是什么样的吗?
  • 我尝试使用dput() 十几次,但代码被拒绝,因为它说格式错误。我不知道如何包含它
  • 对于透明部分,你只需要在geom_bar()中使用alpha = Freq而不是alpha = 0.7。为了使文本居中,这是一个有用的帖子:stackoverflow.com/questions/6644997/…
  • 我不需要更改条形透明度,但需要更改文本透明度
  • 我已经用dput()添加了数据

标签: r ggplot2 label


【解决方案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")

【讨论】:

  • 我想把它们集中在情节上,但是谢谢,这对以后的参考也很有帮助
【解决方案2】:

这是一个选项:

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")

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

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

【讨论】:

    猜你喜欢
    • 2018-07-26
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 2011-02-21
    • 2013-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多