R图-基于其他数据帧中存在的点图中条目的颜色(使用循环)
我有两个数据框,一个包含个人类别,另一个包含某些字符的值,如下所示: df1:R图-基于其他数据帧中存在的点图中条目的颜色(使用循环),r,plot,ggplot2,R,Plot,Ggplot2,我有两个数据框,一个包含个人类别,另一个包含某些字符的值,如下所示: df1: individuals V1 V2 HG097 -0.0181 -0.0818 HG099 -0.0188 -0.0808 HG100 -0.021 -0.0753 HG101 -0.0196 -0.0804 HG1941 -0.0206 0.0174 HG1942 -0.031 0.0075 HG1944 -0.0291
individuals V1 V2
HG097 -0.0181 -0.0818
HG099 -0.0188 -0.0808
HG100 -0.021 -0.0753
HG101 -0.0196 -0.0804
HG1941 -0.0206 0.0174
HG1942 -0.031 0.0075
HG1944 -0.0291 0.0454
HG1945 -0.0245 -0.0128
HG1947 -0.0184 -0.0065
HG1950 0.006 0.0167
NA18542 -0.0296 0.0899
NA18543 -0.0318 0.1012
NA18544 -0.0305 0.096
NA18545 -0.0317 0.1068
NA18546 -0.0315 0.1016
NA18547 -0.0332 0.098
df2:
GR1 GR2 GR3 GR4
HG097 HG100 HG1944 NA18543
HG099 HG1941 HG1945 NA18544
HG101 HG1947 NA18542 NA18545
现在,在从df1
绘制V1 v.s V2
时,我想根据df2
中的个体所属的组对点进行着色。那么,如何为此设置循环
df1 <- read.table("data_file", header =T)
df2 <- read.table("persons_group_file", header =T)
plot(df1$V1, df1$V2, col=...............)
df1这里不需要循环,需要merge
和melt
。我将如何解决这个问题:将df2转换为长格式,然后与df1合并。使用ggplot进行绘图是很简单的
请注意,并非所有个人都已在示例数据中分配了组
library(reshape2)
library(ggplot2)
#Convert df2 from wide format to long format
mergedat <- melt(df2,measure.vars=colnames(df2))
#Merged the data into df1:
plotdat <- merge(df1, mergedat, by.x="individuals",by.y="value", all.x=T)
#head(plotdat)
# individuals V1 V2 variable
# 1 HG097 -0.0181 -0.0818 GR1
# 2 HG099 -0.0188 -0.0808 GR1
# 3 HG100 -0.0210 -0.0753 GR2
# 4 HG101 -0.0196 -0.0804 GR1
# 5 HG1941 -0.0206 0.0174 GR2
# 6 HG1942 -0.0310 0.0075 <NA>
#plotting
p1 <- ggplot(plotdat, aes(x=V1,y=V2,color=variable)) + geom_point()
p1
library(重塑2)
图书馆(GG2)
#将df2从宽格式转换为长格式
mergedat这里不需要循环,您需要merge
和melt
。我将如何解决这个问题:将df2转换为长格式,然后与df1合并。使用ggplot进行绘图是很简单的
请注意,并非所有个人都已在示例数据中分配了组
library(reshape2)
library(ggplot2)
#Convert df2 from wide format to long format
mergedat <- melt(df2,measure.vars=colnames(df2))
#Merged the data into df1:
plotdat <- merge(df1, mergedat, by.x="individuals",by.y="value", all.x=T)
#head(plotdat)
# individuals V1 V2 variable
# 1 HG097 -0.0181 -0.0818 GR1
# 2 HG099 -0.0188 -0.0808 GR1
# 3 HG100 -0.0210 -0.0753 GR2
# 4 HG101 -0.0196 -0.0804 GR1
# 5 HG1941 -0.0206 0.0174 GR2
# 6 HG1942 -0.0310 0.0075 <NA>
#plotting
p1 <- ggplot(plotdat, aes(x=V1,y=V2,color=variable)) + geom_point()
p1
library(重塑2)
图书馆(GG2)
#将df2从宽格式转换为长格式
mergedat我将避免使用循环来执行此操作。如果数据格式正确,则可以轻松地将其传递到ggplot2进行打印
#First do some data wrangling to get data into correct format
#load required libraries
library(tidyr)
library(dplyr)
#Convert df2 from wide format to long format
tall_df <- tidyr::gather(df2)
#Incorporate that data into df1
merged_df <- dplyr::full_join(df1, tall_df, by = c("individuals" = "value"))
#Then pass this data to ggplot2 to print:
library(ggplot2)
g = ggplot(merged_df, aes(x = V1, y=V2)) + geom_point() + aes(colour = key)
g
#首先进行一些数据争用,以将数据转换为正确的格式
#加载所需的库
图书馆(tidyr)
图书馆(dplyr)
#将df2从宽格式转换为长格式
我会避免使用循环来做这件事。如果数据格式正确,则可以轻松地将其传递到ggplot2进行打印
#First do some data wrangling to get data into correct format
#load required libraries
library(tidyr)
library(dplyr)
#Convert df2 from wide format to long format
tall_df <- tidyr::gather(df2)
#Incorporate that data into df1
merged_df <- dplyr::full_join(df1, tall_df, by = c("individuals" = "value"))
#Then pass this data to ggplot2 to print:
library(ggplot2)
g = ggplot(merged_df, aes(x = V1, y=V2)) + geom_point() + aes(colour = key)
g
#首先进行一些数据争用,以将数据转换为正确的格式
#加载所需的库
图书馆(tidyr)
图书馆(dplyr)
#将df2从宽格式转换为长格式
tall_df为什么不使用ggplot时会有ggplot2标记?如果可以使用ggplot2完成任务,则可以。为什么不使用ggplot时会有ggplot2标记?如果可以使用ggplot2完成任务,则可以。