Multirow axis labels with nested grouping variables

前端 未结 6 1964
后悔当初
后悔当初 2020-11-22 16:42

I would like the levels of two different nested grouping variables to appear on separate lines below the plot, and not in the legend. What I have right now is this code:

6条回答
  •  感情败类
    2020-11-22 17:05

    @agstudy already answered this question and I'm going to use it myself, but if you'd accept something uglier, but simpler, this is what I came with before his answer:

    data <- read.table(text = "Group Category Value
        S1 A   73
        S2 A   57
        S1 B   7
        S2 B   23
        S1 C   51
        S2 C   87", header=TRUE)
    
    p <- ggplot(data=data, aes(x=Category, y=Value, fill=Group))
    p + geom_bar(position = 'dodge') +
      geom_text(aes(label=paste(Value, "%")), position=position_dodge(width=0.9),   vjust=-0.25) +
      geom_text(colour="darkgray", aes(y=-3, label=Group),  position=position_dodge(width=0.9), col=gray) +
      theme(legend.position = "none", 
        panel.background=element_blank(),
        axis.line = element_line(colour = "black"),
        axis.line.x = element_line(colour = "white"),
        axis.ticks.x = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank()) +
      annotate("segment", x = 0, xend = Inf, y = 0, yend = 0)
    

    Which will give us:

    enter image description here

提交回复
热议问题