Adding more x-axix labels in ggplot

2019-07-25 08:40发布

问题:

I have a function like this:

plotMeansDouble <- function(data, labX)
{
  #xlabs <- paste(levels(stats::reorder(data$type, data$score,mean)),"\n(N=",levels(stats::reorder(data$N, data$score,mean)),"/",levels(stats::reorder(data$TN, data$score,mean)),")",sep="")
  ggplot(data, aes(x=microstyle, y=difficulty, ymax = Upper.CI, group= course, color=course)) + 
  geom_errorbar(aes(ymin=Lower.CI, ymax = Upper.CI ), width=.1, position=position_dodge(.2)) + 
  geom_line(, position=position_dodge(.2)) + 
  geom_text(aes(y=Upper.CI,label = pointlabel, vjust=-1),position=position_dodge(.2)) + 
  geom_point(size=3, shape=21, position=position_dodge(.2))+
  labs(x = labX, y = "Score") + 
  theme_bw()+ 
  theme(panel.grid.major = element_blank(), panel.border = element_blank(),axis.text=element_text(size=14), axis.title=element_text(size=18),axis.text.x=element_text(size=16, angle=40, vjust=.8, hjust=1.01)) #+ scale_x_discrete(labels=xlabs)
}

This code plot my graph like this:

In this plot I want to plot the relationship between Type and Score for two courses, so far so good. But now I would like to add a second x-axis lables below A, B and C respectively to show the number of observations for each type. Note that in the code I commented the scale_x_discrete. I know this function allows me to add something under each level. But the problem is that I have two courses DSP and RP. So I would like to add the number of observations for both two courses under x labels A,B,C, preferably colored with green and yellow to represent two courses, which does not seem to be possible with scale_x_discrete.

I think a solution could be add two additional x-axis under the current one, each with labels of the two course. Is it possible to achieve this with ggplot2?

回答1:

You can use geom_text to achieve this. The following code is strongly influenced by this question. Note that because there no sample data in your question, I made my own reproducible example.

# load ggplot
require(ggplot2)
require(grid)
# creating sample data
set.seed(42)
df <- data.frame(Type = LETTERS[1:3], 
                 Score = runif(6), 
                 course = letters[1:2])
# data for text labels
text.a <- data.frame(Type = LETTERS[1:3], 
                      Score = -Inf,
                      course = 'a',
                      text = paste0('N=', 1:3))
text.b <- data.frame(Type = LETTERS[1:3], 
                     Score = -Inf,
                     course = 'b',
                     text = paste0('N=', 2:4))
# plotting commands
p <- ggplot(df, aes(Type, Score, color=course, group=course)) + 
  geom_point() +
  geom_line() +
  geom_text(data=text.a, aes(label = text), vjust=3, show_guide  = FALSE) +  # adding text for first course
  geom_text(data=text.b, aes(label = text), vjust=4.5, show_guide = FALSE) + # adding text for second course
  theme(plot.margin = unit(c(1,1,2,1), "lines")) + # making enough room 
  scale_x_discrete(name='\n\n\nType') # pushing down the legend

# turns clipping off
gt <- ggplot_gtable(ggplot_build(p))
gt$layout$clip[gt$layout$name == "panel"] <- "off"
grid.draw(gt)



标签: r ggplot2