Different font faces and sizes within label text entries in ggplot2

后端 未结 3 435
清歌不尽
清歌不尽 2020-12-14 19:52

I am building charts that have two lines in the axis text. The first line contains the group name, the second line contains that group population. I build my axis labels as

3条回答
  •  误落风尘
    2020-12-14 20:33

    I also think that I could not to make the graph by only using ggplot2 features.

    I would use grid.text and grid.gedit.

    require(ggplot2)
    Treatment <- rep(c('T','C'), each=2)
    Gender <- rep(c('Male','Female'), 2)
    Response <- sample(1:100, 4)
    test_df <- data.frame(Treatment, Gender, Response)
    
    xbreaks <- levels(test_df$Gender)
    xlabels <- paste(xbreaks,'\n',c('',''))
    
    hist <- ggplot(test_df, aes(x=Gender, y=Response, fill=Treatment,
      stat="identity"))
    hist + geom_bar(position = "dodge") + 
      scale_y_continuous(limits = c(0, 100), name = "") +
      scale_x_discrete(labels=xlabels, breaks = xbreaks) + 
      opts(axis.text.x = theme_text(face='bold', size=12))
    grid.text(label="POP1", x = 0.29, y = 0.06)
    grid.text(label="POP2", x = 0.645, y = 0.06)
    grid.gedit("GRID.text", gp=gpar(fontsize=8))
    

    Different font faces and sizes within label text entries in ggplot2

    Please try to tune a code upon according to your environment (e.g. the position of sub-axis labels and the fontsize).

提交回复
热议问题