简体   繁体   English

分面躲避条形图上的geom_text标签

[英]geom_text labels on a faceted dodged barplot

I have used ggplot2 to create the image below. 我用ggplot2来创建下面的图像。 I want to explore viewing the data by themes (foreign, economic, etc) as individual facets of facet_wrap. 我想探索按主题(外国,经济等)查看数据作为facet_wrap的各个方面。 I can restructure everything I want except I can no longer put the labels (73(1.02%), 67(.93%), etc) back on the bars. 我可以重新构建我想要的一切,除了我不能再将标签(73(1.02%),67(.93%)等)放回栏上。 How can I plot the labels? 我该如何绘制标签?

在此输入图像描述

Here's my code: 这是我的代码:

dat3 <- structure(list(grouping.var = structure(c(1L, 3L, 1L, 3L, 1L, 
    3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L), class = "factor", .Label = c("BIDEN", 
    "RADDATZ", "RYAN")), word.count = c(7240, 7153, 7240, 7153, 7240, 
    7153, 7240, 7153, 7240, 7153, 7240, 7153, 7240, 7153, 7240, 7153
    ), variable = structure(c(4L, 4L, 2L, 2L, 1L, 1L, 5L, 5L, 3L, 
    3L, 7L, 7L, 6L, 6L, 8L, 8L), .Label = c("foreign", "economic", 
    "rebublican_people", "health", "democratic_people", "romney", 
    "obama_any_name", "obama_by_name"), class = "factor"), value = c(29, 
    36, 27, 47, 67, 73, 13, 28, 45, 24, 13, 20, 26, 21, 1, 14), labs = c("29(.0040)", 
    "36(.0050)", "27(.0037)", "47(.0066)", "67(.0093)", "73(.0102)", 
    "13(.0018)", "28(.0039)", "45(.0062)", "24(.0034)", "13(.0018)", 
    "20(.0028)", "26(.0036)", "21(.0029)", "1(.0001)", "14(.0020)"
    ), loc = c(22.5, 29.5, 20.5, 40.5, 60.5, 65.75, 6.5, 21.5, 38.5, 
    17.5, 6.5, 13.5, 19.5, 14.5, 7, 7.5), cols = c("black", "white", 
    "white", "white", "white", "white", "white", "white", "white", 
    "white", "white", "white", "white", "white", "white", "white"
    )), .Names = c("grouping.var", "word.count", "variable", "value", 
    "labs", "loc", "cols"), row.names = c(NA, -16L), class = "data.frame")


ggplot(dat3, aes(x=variable,  y=value, fill=grouping.var)) + 
    geom_bar(position="dodge", stat="identity")  +
    coord_flip() + theme_bw() + 
    theme(legend.position="top", legend.background = element_rect(color="white"),
        panel.grid.major=element_blank(),panel.grid.minor=element_blank(),
        legend.title=element_blank(), axis.ticks.y = element_blank(), 
        axis.text.y = element_blank()) +
    ylab("") +  xlab("") + facet_wrap(~variable, ncol=2, scales = "free_x") +
    scale_fill_manual(values=c("#0000FF", "#FF0000"),
        guide = guide_legend(reverse=TRUE))
    geom_text(aes(label = labs,  y = loc, x = variable),
              size = 5, position = position_dodge(width=0.9), color=dat3$cols) 

I got rid of the color=dat3$cols and it appears to work 我摆脱了color=dat3$cols ,它似乎工作

ggplot(dat3, aes(x=variable,  y=value, fill=grouping.var)) + 
  geom_bar(position="dodge", stat="identity")  +
  coord_flip() + theme_bw() + 
  theme(legend.position="top", legend.background = element_rect(color="white"),
        panel.grid.major=element_blank(),panel.grid.minor=element_blank(),
        legend.title=element_blank(), axis.ticks.y = element_blank(), 
        axis.text.y = element_blank()) +
          ylab("") +  xlab("") + facet_wrap(~variable, ncol=2, scales = "free_x") +
          scale_fill_manual(values=c("#0000FF", "#FF0000"),
                            guide = guide_legend(reverse=TRUE))+
geom_text(aes(label = labs,  y = loc, x = variable),
          size = 5, position = position_dodge(width=0.9))

在此输入图像描述

I encountered a similar issue, not really a bug. 我遇到了类似的问题,不是真正的错误。 but not well documented 但没有很好的记录

Like your hack of 就像你的黑客一样

"I got rid of the color=dat3$cols and it appears to work" “我摆脱了color=dat3$cols ,它似乎工作”

The solution is to verbosely specify all the color values in anything that can interpret aes(colour=foo) 解决方案是详细指定任何可以解释aes的颜色值(color = foo)

In my case, I had two calls to aes(), in ggplot() and geom_text() 就我而言,我在ggplot()和geom_text()中有两次调用aes()

1) ggplot(dat_foo, aes(x=xfoo_i, y=yfoo, colour=colourfoo, group=groupfoo)) 1) ggplot(dat_foo, aes(x=xfoo_i, y=yfoo, colour=colourfoo, group=groupfoo))

2) geom_text(data = label_data,aes(x = xfoo, y = yfoo, label=label_foo,colour=NULL) 2) geom_text(data = label_data,aes(x = xfoo, y = yfoo, label=label_foo,colour=NULL)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM