【问题标题】:X-axis labels illegible. Display every other label on X-axis ggplot2X 轴标签难以辨认。在 X 轴 ggplot2 上显示每隔一个标签
【发布时间】:2019-07-28 17:50:52
【问题描述】:

我有以下情节,但 x 轴标签太小而无法阅读。如果我增加它们的大小,它们就会重叠且难以辨认

I have tried the code form the following link

但我得到一个错误''by'argument in'wrong sign'

我也尝试了以下链接 How to show every second R ggplot2 x-axis label value?

但作为初学者,我不能完全按照代码进行操作,也不确定标签是日期是否会增加复杂性。

我有 170 个样本的 100 个基因的 df + 一个“基因”列。然后我使用以下代码使数据变长:

mat_dataNew<-mat_data %>% gather(sample, expression, -Genes)
#log10-transform Counts data
mat_dataNew <- mutate(mat_dataNew, log10_NormCounts = log10(expression))

然后我绘制:

ggplot(mat_dataNew, aes(x=reorder(Genes, -log10_NormCounts), y=log10_NormCounts, colour=Group)) +
 geom_point(size=.5) +
theme(axis.text.x=element_text(angle=90, size=4)) +
 labs(x = "100 most abundant Genes", y = "Gene Expression (Log10 Normalised Counts)")

我尝试阅读有关 scale_x_continuous 但似乎找不到答案。您能否帮忙推荐一些代码,以便 x 轴标签清晰可见?我想也许最简单的选择是显示所有其他标签???

【问题讨论】:

标签: r ggplot2


【解决方案1】:

你需要scale_x_discrete,正如 Tung 在 cmets 中所说的那样。

scale_x_discrete 具有以下三个参数(其中包括documentation):

  1. limits - 决定 图表 的值及其顺序
  2. breaks - 决定将哪些值显示为轴上的刻度
  3. labels - 决定刻度线实际显示的标签

(在您的情况下,由于您的基因名称已经是因素,因此您不需要标签参数。)

limits 参数设置为所有基因,将breaks 参数设置为所有其他基因(SO ref),并将scale_x_discrete 添加到您的ggplot

ggplot(...+
scale_x_discrete(limits=Genes,breaks=Genes[seq(1,length(Genes),by=2)])+
...)

注意:您似乎正在重新排序 ggplot 中的 Genes 列,您需要对 limits 参数进行相同的重新排序才能使其正常工作。

【讨论】:

    猜你喜欢
    • 2020-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-04
    • 1970-01-01
    相关资源
    最近更新 更多