首页 > 解决方案 > 如何预先制作如下图?

问题描述

我可以使用 ggplot2 中的以下代码或任何其他绘图方法来重现下面指示的绘图吗?如何显示数据点/样本以反映组变量和基因 “OXC”的表达水平?谢谢!

 dad <- data.frame(OYC = rnorm(50),
                   OXC = rnorm(50),
                   FG  = runif(50, min = 60, max = 300),
                   GTT = runif(50, min= 0, max = 20),
                   group = rep(c("Aaa", "Bbb", "Ccc", "Ddd"), time = c(15, 12, 8,15)))
 row.names(dad) <- paste("sample_", 1:50)
 
 library(ggplot2)
 dad %>%
   ggplot(aes(x=FG, y= GTT, Color = OXC)) + 
   geom_point() + 
   theme(axis.title=element_text(size = 12,face="bold", colour = "blue"),
         axis.text = element_text(size = 12),
         title=element_text(size= 12,face="bold"))+
   labs(y= "ggt", x = "FG")

在此处输入图像描述

标签: rggplot2

解决方案


你的意思是这样的吗?我已经使用了您分享的数据。关键是shape在你的aes(). 它将创建形状而不是公共点。之后,如果您想要额外的自定义,您可以使用scale_shape_manual()以定义不同的形状。这里为您提供一些选择。color数字色标也可以添加fill美学元素。这里的代码:

library(ggplot2)
library(dplyr)
#Plot
dad %>%
  ggplot(aes(x=FG, y= GTT, color = OXC, shape=factor(group))) + 
  geom_point() + 
  theme(axis.title=element_text(size = 12,face="bold", colour = "blue"),
        axis.text = element_text(size = 12),
        title=element_text(size= 12,face="bold"))+
  labs(y= "ggt", x = "FG")

输出:

在此处输入图像描述

如果你想设置不同的颜色,你可以使用这个:

#Plot 2
dad %>%
  ggplot(aes(x=FG, y= GTT, color = OXC, shape=factor(group))) + 
  geom_point() + 
  theme(axis.title=element_text(size = 12,face="bold", colour = "blue"),
        axis.text = element_text(size = 12),
        title=element_text(size= 12,face="bold"))+
  labs(y= "ggt", x = "FG")+
  scale_color_gradient2(low = 'red',mid = 'green',high = 'yellow')

输出:

在此处输入图像描述

以及一些级别的定制:

#Plot 3
dad %>%
  ggplot(aes(x=FG, y= GTT, color = OXC, shape=factor(group))) + 
  geom_point(size=3) + 
  theme(axis.title=element_text(size = 12,face="bold", colour = "blue"),
        axis.text = element_text(size = 12),
        title=element_text(size= 12,face="bold"))+
  labs(y= "ggt", x = "FG")+
  scale_shape_manual(values = c('circle','square','triangle','diamond'))+
  scale_color_gradient2(low = 'red',mid = 'yellow',high = 'blue')

输出:

在此处输入图像描述


推荐阅读