如何在ggplot2散点图中使重要节点更大

时间:2017-03-19 09:53:59

标签: r ggplot2 scatter-plot

美好的一天。

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

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

enter image description here

我一直在寻找解决方案,但到目前为止,我没有运气。你能和我分享一下你的经历吗?我还是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);

感谢您的时间。

1 个答案:

答案 0 :(得分: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"))