组合的条形图修改R中的x轴刻度标签

时间:2018-06-11 18:03:17

标签: r ggplot2

我的x轴刻度标签出现的顺序出现问题。我创建了一个包含组合数据的图表。在x轴上,我标记了所使用的样品,但是对于前三个数据集(六个,八个,十二个星期),每个条柱使用两个样品。在最后一组(终点)中,AbxFMT_2901应该对应于前三个数据集的AbxFMT_2901 / 2902,因为在终点我只有一个样本(2901)而丢失了另一个样本(2902)。该标签应该只出现在终点部分,它应该是第一个。enter image description here

这是我用来生成图表的代码:

df = bind_rows(six_wks_huCD45_AbxFMT, eight_wks_huCD45_AbxFMT, twelve_wks_huCD45_AbxFMT, terminal_data_huCD45_PBL_AbxFMT)

df$orderweeks = factor(df$weeks, levels = c('six weeks', 'eight weeks', 'twelve weeks', 'terminal point'))
df$Sample. <- as.character(df$Sample.)
df$ordersample <- factor(df$Sample., levels = unique(df$Sample.))

df_plot <- ggplot(df, aes(x = df$ordersample, y =df$`huCD45 Cell count`, group = weeks, fill = weeks))+ geom_bar(position = "dodge", stat = "identity") + facet_grid(.~orderweeks) 

df_plot + labs(x = "Samples", y= "huCD45 cell count", title = "PBL Analysis of Abx+FMT Treatment") + scale_fill_discrete(breaks = c("six weeks", "eight weeks", "twelve weeks", "terminal point")) + theme(axis.text.x = element_text(angle = 90))

这是数据帧的示例:

Sample. huCD45 Cell count          weeks     orderweeks      ordersample
1  AbxFMT_2901/2902             860.0      six weeks      six weeks AbxFMT_2901/2902
2  AbxFMT_2903/2904            3736.0      six weeks      six weeks AbxFMT_2903/2904
3  AbxFMT_2905/2906            7137.0      six weeks      six weeks AbxFMT_2905/2906
4  AbxFMT_2907/2908            2183.0      six weeks      six weeks AbxFMT_2907/2908
5  AbxFMT_2909/2910            7485.0      six weeks      six weeks AbxFMT_2909/2910
6  AbxFMT_2911/2912            3509.0      six weeks      six weeks AbxFMT_2911/2912
7  AbxFMT_2901/2902           28296.0    eight weeks    eight weeks AbxFMT_2901/2902
8  AbxFMT_2903/2904           18084.0    eight weeks    eight weeks AbxFMT_2903/2904
9  AbxFMT_2905/2906           27602.0    eight weeks    eight weeks AbxFMT_2905/2906
10 AbxFMT_2907/2908           16706.0    eight weeks    eight weeks AbxFMT_2907/2908
11 AbxFMT_2909/2910           22571.0    eight weeks    eight weeks AbxFMT_2909/2910
12 AbxFMT_2911/2912           18731.0    eight weeks    eight weeks AbxFMT_2911/2912
13 AbxFMT_2901/2902           39975.0   twelve weeks   twelve weeks AbxFMT_2901/2902
14 AbxFMT_2903/2904           19042.0   twelve weeks   twelve weeks AbxFMT_2903/2904
15 AbxFMT_2905/2906           68493.0   twelve weeks   twelve weeks AbxFMT_2905/2906
16 AbxFMT_2907/2908           23533.0   twelve weeks   twelve weeks AbxFMT_2907/2908
17 AbxFMT_2909/2910           50678.0   twelve weeks   twelve weeks AbxFMT_2909/2910
18 AbxFMT_2911/2912           43465.0   twelve weeks   twelve weeks AbxFMT_2911/2912
19      AbxFMT_2901            1318.0 terminal point terminal point      AbxFMT_2901
20 AbxFMT_2903/2904            4565.5 terminal point terminal point AbxFMT_2903/2904

我尝试过使用scale_x_discrete和连续的各种方法,但它没有用。任何建议都非常感谢。谢谢!!

1 个答案:

答案 0 :(得分:1)

我会改变两件事:

df$ordersample <- factor(df$Sample., levels = sort(unique(df$Sample.)))

+ facet_grid(.~orderweeks, scales = 'free_x')