使用ggplot在R中的多个数据框中绘制单个图形中的多个散点图?
Multiple scatter plots in a single figure from multiple data frame in R using ggplot?
我想使用来自两个数据框的数据生成一个图形,显示该单个图形上的所有散点图(即,回归 Data1 的 A 列与 Data2 的 A 列)。图中的每个图都应显示 R 方和 p 值。我更想知道如何在从多个数据帧中获取数据时使用 ggplot
的 fact_wrap
函数。
我尝试了几种方法但没有成功。
library(tidyverse)
Data1=data.frame(A=runif(20, min = 0, max = 100), B=runif(20, min = 0, max = 250), C=runif(20, min = 0, max = 300))
Data2=data.frame(A=runif(20, min = -10, max = 50), B=runif(20, min = -5, max = 150), C=runif(20, min = 5, max = 200))
#method-1: using plot functions
par(mfrow=c(3,1))
plot(Data1$A, Data2$A)
abline(lm(Data1$A ~ Data2$A))
plot(Data1$B, Data2$B)
abline(lm(Data1$B ~ Data2$B))
plot(Data1$C, Data2$C)
abline(lm(Data1$C ~ Data2$C))
dev.off()
#method-2: using ggplot
ggplot()+
geom_point(aes(Data1$A,Data2$A))
我想要下图这样的图
您可以制作一个地块列表,然后使用 grid.arrange() 函数。
sc_plots = list()
sc_plots$sc1 = ggplot() + ...
sc_plots$sc2 = ggplot() + ...
grid.arrange(sc_plots$sc1, sc_plots$sc2,
ncol = 3)
最难的部分是整理数据。一旦完成,情节就非常简单了。
library(tidyverse)
Data1=data.frame(A=runif(20, min = 0, max = 100), B=runif(20, min = 0, max = 250), C=runif(20, min = 0, max = 300))
Data2=data.frame(A=runif(20, min = -10, max = 50), B=runif(20, min = -5, max = 150), C=runif(20, min = 5, max = 200))
data <- Data1 %>%
#add columns to indicate the source and the observation number
mutate(source = "Data1",
obs = row_number()) %>%
#bind to Data2 with the same new columns
bind_rows(Data2 %>% mutate(source = "Data2", obs = row_number())) %>%
#tidy the data so we've got a column for Data1 and Data2 and an indicator for the series (A, B, C)
gather(A, B, C, key = series, value = value) %>%
spread(key = source, value = value)
#create a separate data frame for annotations, finding the "top left" corner of each series
annotations <- data %>%
group_by(series) %>%
summarise(x = min(Data1),
y = max(Data2)) %>%
mutate(label = c("P = 0.6", "P = 0.5", "P = 0.9"))
#plot the data, faceting by series
data %>%
ggplot(aes(Data1, Data2))+
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
facet_grid(series~., scales = "free") +
#add the annotations with adjustments to the horiz & vert placement
geom_text(data = annotations, aes(x = x, y = y, label = label, hjust = 0, vjust = 1),
color = "red", fontface = "italic")
@Jordo82,这是我尝试在图形上插入文本时得到的结果。有没有办法 free-up Y-axis 添加的文本不依赖于 y-scale 而它出现在每个图的左上角。我使用 annotate_custom
的原因是它不依赖于 y-scale 但缺点是我只会使用标签中的第一个文本。我的真实价值观彼此如此不同 - 请参阅附图的 Y-scale。
我在编辑放置坐标时使用了您的代码
annotate("text", -1.5, 800, label = c("P = 0.6", "P = 0.5", "P = 0.9", "P = 0.9"),
color = "red", fontface = "italic")
我想使用来自两个数据框的数据生成一个图形,显示该单个图形上的所有散点图(即,回归 Data1 的 A 列与 Data2 的 A 列)。图中的每个图都应显示 R 方和 p 值。我更想知道如何在从多个数据帧中获取数据时使用 ggplot
的 fact_wrap
函数。
我尝试了几种方法但没有成功。
library(tidyverse)
Data1=data.frame(A=runif(20, min = 0, max = 100), B=runif(20, min = 0, max = 250), C=runif(20, min = 0, max = 300))
Data2=data.frame(A=runif(20, min = -10, max = 50), B=runif(20, min = -5, max = 150), C=runif(20, min = 5, max = 200))
#method-1: using plot functions
par(mfrow=c(3,1))
plot(Data1$A, Data2$A)
abline(lm(Data1$A ~ Data2$A))
plot(Data1$B, Data2$B)
abline(lm(Data1$B ~ Data2$B))
plot(Data1$C, Data2$C)
abline(lm(Data1$C ~ Data2$C))
dev.off()
#method-2: using ggplot
ggplot()+
geom_point(aes(Data1$A,Data2$A))
我想要下图这样的图
您可以制作一个地块列表,然后使用 grid.arrange() 函数。
sc_plots = list()
sc_plots$sc1 = ggplot() + ...
sc_plots$sc2 = ggplot() + ...
grid.arrange(sc_plots$sc1, sc_plots$sc2,
ncol = 3)
最难的部分是整理数据。一旦完成,情节就非常简单了。
library(tidyverse)
Data1=data.frame(A=runif(20, min = 0, max = 100), B=runif(20, min = 0, max = 250), C=runif(20, min = 0, max = 300))
Data2=data.frame(A=runif(20, min = -10, max = 50), B=runif(20, min = -5, max = 150), C=runif(20, min = 5, max = 200))
data <- Data1 %>%
#add columns to indicate the source and the observation number
mutate(source = "Data1",
obs = row_number()) %>%
#bind to Data2 with the same new columns
bind_rows(Data2 %>% mutate(source = "Data2", obs = row_number())) %>%
#tidy the data so we've got a column for Data1 and Data2 and an indicator for the series (A, B, C)
gather(A, B, C, key = series, value = value) %>%
spread(key = source, value = value)
#create a separate data frame for annotations, finding the "top left" corner of each series
annotations <- data %>%
group_by(series) %>%
summarise(x = min(Data1),
y = max(Data2)) %>%
mutate(label = c("P = 0.6", "P = 0.5", "P = 0.9"))
#plot the data, faceting by series
data %>%
ggplot(aes(Data1, Data2))+
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
facet_grid(series~., scales = "free") +
#add the annotations with adjustments to the horiz & vert placement
geom_text(data = annotations, aes(x = x, y = y, label = label, hjust = 0, vjust = 1),
color = "red", fontface = "italic")
@Jordo82,这是我尝试在图形上插入文本时得到的结果。有没有办法 free-up Y-axis 添加的文本不依赖于 y-scale 而它出现在每个图的左上角。我使用 annotate_custom
的原因是它不依赖于 y-scale 但缺点是我只会使用标签中的第一个文本。我的真实价值观彼此如此不同 - 请参阅附图的 Y-scale。
我在编辑放置坐标时使用了您的代码
annotate("text", -1.5, 800, label = c("P = 0.6", "P = 0.5", "P = 0.9", "P = 0.9"),
color = "red", fontface = "italic")