两个不同数据集之间的散点图

时间:2017-06-07 20:39:40

标签: r ggplot2 scatter-plot

我有两个不同的数据集。两个不同的数据框架。我想为此生成散点图。

Dataset1:

MT  NUM
D   103
M   67
D   90
W   456
MM  78
M   434

Dataset2:

MT  NUM
M   13
M   6
MM  9
W   45
D   7

Scatterplot应如下所示: enter image description here

我不知道如何生成情节。谁能告诉我怎么做?谢谢

我使用了以下代码:但情节看起来与我想要的完全不同。

library(reshape)
hh <- melt(list(p1 = Dataset1, p2 = Dataset2), id.vars = "MT")

ggplot(hh, aes(MT, value, colour = L1)) + geom_point() +
  scale_colour_manual("Dataset", values = c("p1" = "blue", "p2" = "red"))

1 个答案:

答案 0 :(得分:1)

不确定这是您想要的,因为您显示的图表与您从所编写的代码中获得的图表有很大不同。

library(ggplot2)
df <- merge(df1, df2, by = 'MT')
ggplot(df) +
  geom_point(aes(NUM.x, NUM.y), color = '#0087E9', size = 5) +
  theme_minimal() +
  theme(axis.text = element_text(color = 'black', size = 16),
        axis.line = element_line(color = 'black'))

数据

df1 <- structure(list(MT = structure(c(1L, 2L, 1L, 4L, 3L, 2L), .Label = c("D", 
"M", "MM", "W"), class = "factor"), NUM = c(103L, 67L, 90L, 456L, 
78L, 434L)), .Names = c("MT", "NUM"), class = "data.frame", row.names = c(NA, 
-6L))

df2 <- structure(list(MT = structure(c(2L, 2L, 3L, 4L, 1L), .Label = c("D", 
"M", "MM", "W"), class = "factor"), NUM = c(13L, 6L, 9L, 45L, 
7L)), .Names = c("MT", "NUM"), class = "data.frame", row.names = c(NA, 
-5L))