【问题标题】:How to make significant nodes bigger in ggplot2 scatter plot如何在ggplot2散点图中使重要节点更大
【发布时间】:2017-08-10 15:26:15
【问题描述】:

美好的一天。

我正在使用 ggplot2 绘制 REVIGO 生成的结果的散点图。

我想在散点图中增加重要节点的大小(附图)。例如,log10_p_value 为 -12.5 的节点应该是最大的(橙色框),但在图中并非如此。

我一直在寻找解决方案,但到目前为止我还没有运气。你能和我分享你的经验吗?我还是 ggplot2 的新手。我包括了下面的代码。

library( ggplot2 )
library( scales )

names11 <- c("term_ID","description","frequency_%","plot_X","plot_Y","plot_size","log10_p_value","uniqueness","dispensability");
data11 <- rbind(c("GO:0009628","response to abiotic stimulus", 0.312, 5.840,-1.260, 5.190,-14.1565,0.454,0.000),
c("GO:0019538","protein metabolic process",12.328,-4.835,-0.256, 6.788,-2.4535,0.765,0.000),
c("GO:0030154","cell differentiation", 0.281,-0.789,-6.684, 5.146,-3.4926,0.536,0.000),
c("GO:0016049","cell growth", 0.035,-1.034, 6.318, 4.241,-3.2145,0.717,0.073),
c("GO:0009719","response to endogenous stimulus", 0.113, 5.811, 1.214, 4.750,-2.8560,0.471,0.353),
c("GO:0006950","response to stress", 4.119, 6.192,-0.112, 6.312,-11.1427,0.415,0.494));

one.data11 <- data.frame(data11);
names(one.data11) <- names11;
one.data11 <- one.data11 [(one.data11$plot_X != "null" & one.data11$plot_Y != "null"), ];
one.data11$plot_X <- as.numeric( as.character(one.data11$plot_X) );
one.data11$plot_Y <- as.numeric( as.character(one.data11$plot_Y) );
one.data11$plot_size <- as.numeric( as.character(one.data11$plot_size) );
one.data11$log10_p_value <- as.numeric( as.character(one.data11$log10_p_value) );
one.data11$frequency <- as.numeric( as.character(one.data11$frequency) );
one.data11$uniqueness <- as.numeric( as.character(one.data11$uniqueness) );
one.data11$dispensability <- as.numeric( as.character(one.data11$dispensability) );

# --------------------------------------------------------------------------

p11 <- ggplot( data = one.data11 );
p11 <- p11 + geom_point( aes( plot_X, plot_Y, colour = log10_p_value, size = log10_p_value), alpha = I(0.6) ) + scale_size_area();
# Change the gradient colour
p11 <- p11 + scale_colour_gradient( low = "navyblue", high = "blue", limits = c( min(one.data11$log10_p_value), 0) );
p11 <- p11 + geom_point( aes(plot_X, plot_Y, size = log10_p_value), shape = 21, fill = "transparent", colour = I (alpha ("black", 0.6) )) + scale_size_area();
# Adjust the plot scale size
p11 <- p11 + scale_size( range=c(3, 18)) + theme_bw();
ex11 <- one.data11 [ one.data11$dispensability < 0.15, ];
# Adjust position of the plot text label with the vjust and hjust aesthetics
# 0 (right/bottom); 1 (top/left) ; ("left", "middle", "right", "bottom", "center", "top")
# Inward always aligns text towards the center
# Outward aligns it away from the center
p11 <- p11 + geom_text( data = ex11, aes(plot_X, plot_Y, label = description), colour = I(alpha("black", 0.85)), size = 4, check_overlap = TRUE, vjust = "middle", hjust = "inward");
p11 <- p11 + labs (y = "semantic space x", x = "semantic space y");
p11 <- p11 + theme(legend.key = element_blank()) ;
one.x_range = max(one.data11$plot_X) - min(one.data11$plot_X);
one.y_range = max(one.data11$plot_Y) - min(one.data11$plot_Y);
p11 <- p11 + xlim(min(one.data11$plot_X)-one.x_range/10,max(one.data11$plot_X)+one.x_range/10);
p11 <- p11 + ylim(min(one.data11$plot_Y)-one.y_range/10,max(one.data11$plot_Y)+one.y_range/10);

# --------------------------------------------------------------------------

# Output the plot to screen
p11;

ggsave("scaterPlot11.tiff", dpi=300);

感谢您的宝贵时间。

【问题讨论】:

    标签: r ggplot2 scatter-plot


    【解决方案1】:

    您可以使用函数abs() 将大小的值设为正数。

    size  = abs(log10_p_value)
    

    根据下面的评论,使用rev() 可能更好更简单。

    size = rev(log10_p_value)
    

    这只是反转您的缩放适合大小。

    您仍然可以使用rev(),但您还需要更改scale_size_continuous()。举个例子:

    df <- data.frame(x = 1:10, y = -10:-1)
    
    ggplot(data = df) +
      geom_point(aes(x = x,
                     y = y,
                     size = rev(y))) +
      scale_size_continuous(breaks = c(-10, -7.5, -5, -2.5), 
                            labels = c("-2.5", "-5", "-7.5", "-10"))
    

    【讨论】:

    • 它有效,感谢您的建议。请问,我怎样才能仍然像图中(橙色框)所示的图例而不是“abs(log10_p_value)”和正值?
    • 试试rev() 而不是abs()
    • rev() 没有用,我以前做过。 abs() 确实如此,只需要弄清楚如何让图例显示如图(橙色框)所示。
    • 我还在想这件事有点简单。我修改了我的答案。您还需要更改比例。
    • 感谢您的建议和解决方案,不胜感激!我有 20 个散点图要调整;它们每个都有不同的 log10_p_value 大小。这是一个挑战。
    猜你喜欢
    • 1970-01-01
    • 2010-11-26
    • 2023-03-09
    • 2019-07-19
    • 1970-01-01
    • 2017-02-18
    • 2014-09-18
    • 2022-01-20
    • 2010-12-21
    相关资源
    最近更新 更多