散点图与组 [英] Scatter plot with groups
问题描述
我想做一个散点图或不同组的点图. 我可以使它适用于单个图,但是由于ggplot2需要x轴的坐标.那就是我已经遇到麻烦了. 这是我的结构:
Hi I want to do a scatterplot or point plot of different groups. I can get it work for individual plots but as ggplot2 need coordinates for the x-axes. thats were I already get into trouble. This is my structure:
# A tibble: 2 x 33
gene_id gene N1 N2 N3 N4 N5 N6 N7 T1 T2 T3 T4
<chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 ENSMUSG000… RNS 198 182 206 183 177 194 193 173 191 167 200
2 ENSMUSG000… RNS2 199 198 216 252 273 159 164 159 162 151 199
# ... with 20 more variables: T5 <dbl>, T6 <dbl>, T7 <dbl>, T8 <dbl>, T9 <dbl>,
# T10 <dbl>, T11 <dbl>, T12 <dbl>, T13 <dbl>, T14 <dbl>, M1 <dbl>, M2 <dbl>,
# M3 <dbl>, M4 <dbl>, M5 <dbl>, M6 <dbl>, M7 <dbl>, M8 <dbl>, M9 <dbl>, M10 <dbl>
我想首先将所有个人紧挨着绘制,然后将N,T和M分组在一起,并以不同的颜色绘制它们.我很确定一旦绘图本身可以工作,我就可以分配颜色.
I would like to plot first all individuals next to each other as well as group the N, T and Ms together and plot them in different colors. I am pretty sure I can assign the colors once the plotting itself works.
ggplot(a, aes(y=a[,3],x=1))+
geom_point()
在这种情况下,我的起步率很低.;;;
I start in this case very low in the porcess.;;;
感谢您的帮助!
推荐答案
您需要将列名转换为变量.我会使用您发布的数据,但是不确定是否期望这样的绘图
You need to transform the column names into variables. I use the data that you published but I am not sure if you expect a plot like this
data <- read.csv('tabla.csv')
data
初始数据
gene_id gene N1 N2 N3 N4 N5 N6 N7 T1 T2 T3 T4
1 ENSMUSG000… RNS 198 182 206 183 177 194 193 173 191 167 200
2 ENSMUSG000… RNS2 199 198 216 252 273 159 164 159 162 151 199
融合数据
library(reshape)
newData <- melt(data, id=c("gene_id", "gene"))
newData
您会得到一个这样的数据框
You get a data frame like this
gene_id gene variable value
1 ENSMUSG000… RNS N1 198
2 ENSMUSG000… RNS2 N1 199
3 ENSMUSG000… RNS N2 182
4 ENSMUSG000… RNS2 N2 198
5 ENSMUSG000… RNS N3 206
使用每个组的名称(N,T或M)创建一个新的列
Create a new colum with the name of each group (N, T or M)
count = 0
for(value in newData$variable){
# To get the group of each value
if(startsWith(value, 'N')){
group <- 'N'
}else if(startsWith(value, 'T')){
group <- 'T'
}else if(startsWith(value, 'M')){
group <- 'M'
}
# Storing the group name into a vector
if(count == 0){
vectorGroup <- group
}else{
vectorGroup <- c(vectorGroup, group)
}
count <- count + 1
}
newData$group <- vectorGroup
最后绘制散点图
library(ggplot2)
ggplot(newData, aes(x = gene, y = value, color= group)) + theme_minimal() + geom_point(width = 0.5) + scale_fill_brewer(palette = 'Paired')
您会得到这样的情节
You get a plot like this
这篇关于散点图与组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!