【问题标题】:Labeling X-axis Values by Group按组标记 X 轴值
【发布时间】:2015-01-11 19:17:27
【问题描述】:

我有以下数据集,其中包含以下代码,可在 ggplot2 中生成我想要的图形。我想按组在 x 轴上标记 PTID,以便显示 ptid 已经显示的方式以及它们所在的组。

     PTID      Group    variable   value
1   subject1    Group 1  Bio.V3.B   43
2   subject2    Group 1  Bio.V3.B   7410
3   subject3    Group 2  Bio.V3.B   13227
4   subject4    Group 2  Bio.V3.B   4832
5   subject5    Group 3  Bio.V3.B   205
6   subject6    Group 3  Bio.V3.B   24899
1   subject1    Group 1  Bio.V3.C   496
2   subject2    Group 1  Bio.V3.C   5932
3   subject3    Group 2  Bio.V3.C   24093
4   subject4    Group 2  Bio.V3.C   85
5   subject5    Group 3  Bio.V3.C   9503
6   subject6    Group 3  Bio.V3.C   18249

ggplot(data=meltedwk28v3, aes(x=PTID, y=value, fill=variable)) + 
    geom_bar(width= .5, colour="black", stat="identity",  #barwidth
             position=position_dodge(width=.7),  #gapwidth
             size=.2) +                        # Thinner lines
    scale_fill_hue(name="Bio-V3 Clade") +      # Set legend title
    xlab("PTID") + ylab("MFI") + # Set axis labels
    ggtitle("Wk28 Bio-V3 MFI") +  # Set title
    theme_classic() + theme(axis.text.x = element_text(angle=90,     hjust=0, vjust=0)) #Themes

谢谢!

【问题讨论】:

    标签: r ggplot2 bar-chart


    【解决方案1】:

    一种可能性是使用 'PTID' 和 'Group' 之间的 interaction 作为 x 变量:

    ggplot(data = df, aes(x = interaction(PTID, Group, sep = " "), y = value, fill = variable)) + 
      geom_bar(width= 0.5, colour="black", stat = "identity",
               position = position_dodge(width = 0.7),
               size = 0.2) +
      xlab("PTID") + ylab("MFI") +
      ggtitle("Wk28 Bio-V3 MFI") +
      theme_classic() +
      theme(axis.text.x = element_text(angle = 90, hjust = 0, vjust = 0))
    

    【讨论】:

    • 有关更复杂的标签,请参见例如here
    • @AwesomeeExpress,如果你觉得这个答案有用的话,接受并投票可能是个好主意。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-02
    • 1970-01-01
    • 1970-01-01
    • 2020-10-16
    • 2018-05-19
    • 1970-01-01
    相关资源
    最近更新 更多