0

I have used ggplot2 to create the image below. I want to explore viewing the data by themes (foreign, economic, etc) as individual facets of 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. How can I plot the labels?

enter image description here

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

2 回答 2

2

我摆脱了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))

在此处输入图像描述

于 2012-10-15T01:59:51.797 回答
0

我遇到了类似的问题,并不是真正的错误。但没有很好的记录

就像你的黑客

“我摆脱了color=dat3$cols它,它似乎工作”

解决方案是详细指定任何可以解释 aes(color=foo) 的颜色值

就我而言,我在 ggplot() 和 geom_text() 中有两次调用 aes()

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)

于 2014-11-12T06:24:14.947 回答