1
votes

I have two different datasets. Two different data frames. I would like to generate scatterplot for this.

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 should look something like following: enter image description here

I don't have any idea how to generate the plot. Can anyone tell me how to do this? Thank you

I used following code: But the plot looks completely different from what I want.

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
Hi Raju! There are many ways to do this that aren't too hard, but it will help us to know what your data structures are. Are these two different data frames? named vectors? Please give us some sample code to generate the data structures you're working with -- even if it's only a few rows long, like you did with the data. - Joy
Yes, they are two different data frames. And I posted the code above which I tried. I also got a plot with that. But that is not what I want. I need a plot which looks like above. - beginner
You haven't given us any code to generate Dataset1 and Dataset2. That's the final piece to have a reproducible example. - Joy
There are just data frames. Why you need code for that. - beginner
If you are plotting MT on the x-axis, those values are categorical not numeric. So perhaps a bar/column chart is more appropriate. It's also not clear how you are trying to specify colour; where is L1 coming from, for example. - neilfws

1 Answers

1
votes

Not sure that's what you want, as the graph you show differs a lot from what you obtain from the code you wrote.

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'))

Data

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))