点图:如何根据数据中的值更改点图的点大小,并使所有x轴值成为整数

时间:2019-04-15 10:21:02

标签: r ggplot2

我已经为数据制作了一个散点图,但需要帮助进行最后的修饰。有点关于stackoverflow的问题,还没有看到任何直接回答我的查询的帖子。

我的点图代码是:

ggplot()+
geom_dotplot(mapping = aes(x= reorder(Description, -p.adjust), y=Count, fill=-p.adjust),
             data = head(X[which(X$p.adjust < 0.05),], n = 15), binaxis = 'y', dotsize = 2,
            method = 'dotdensity', binpositions = 'all', binwidth = NULL)+
  scale_fill_continuous(low="black", high="light grey") +
  labs(y = "Associated genes", x = "wikipathways", fill = "p.adjust") +
  theme(axis.text=element_text(size=8)) +
  ggtitle('') +
  theme(plot.title = element_text(2, face = "bold", hjust = 1),
        legend.key.size = unit(2, "line")) +
  theme(panel.background = element_rect(fill = 'white', colour = 'black'))+
  coord_fixed(ratio = 0.5)+
  coord_flip()

让我们说X是类似的东西:

   Description   p.adjust  Count   GeneRatio  
1  DescriptionA  0.001     3       3/20
2  DescriptionB  0.002     2       2/20
3  DescriptionC  0.003     5       5/20
4  DescriptionD  0.004     10      10/20

要完成此绘图,我需要进行两次编辑。

我想使用基于GeneRatio的点的大小,并基于此大小创建辅助键。 ggplot2,dotplots是否可能?

接下来,我想将X轴值保留为整数。我想避免使用诸如scale_x_continuous(limits = c(2,10))之类的东西,因为此绘图代码是用于各​​种大小的多个数据集的函数的一部分。因此,包含限制/范围将无法正常工作。

我们将不胜感激。

1 个答案:

答案 0 :(得分:1)

如果您可以切换到geom_point图表而不是geom_dotplot,则可以根据变量轻松调整点大小。似乎还幸运地纠正了您的轴问题。

ggplot(x)+
  geom_point(mapping = aes(x= reorder(Description, -p.adjust), y=Count, fill=-p.adjust, size=GeneRatio),
               data = head(x[which(x$p.adjust < 0.05),], n = 15), binaxis = 'y', #dotsize = 2,
               method = 'dotdensity', binpositions = 'all', binwidth = NULL)+
  scale_fill_continuous(low="black", high="light grey") +
  labs(y = "Associated genes", x = "wikipathways", fill = "p.adjust") +
  theme(axis.text=element_text(size=8)) +
  ggtitle('') +
  theme(plot.title = element_text(2, face = "bold", hjust = 1),
    legend.key.size = unit(2, "line")) +
  theme(panel.background = element_rect(fill = 'white', colour = 'black'))+
  coord_fixed(ratio = 0.5)+
  coord_flip()