1

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"))
beginner
  • 1,059
  • 8
  • 23
  • 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 Jun 07 '17 at 20:41
  • 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 Jun 07 '17 at 20:49
  • You haven't given us any code to generate Dataset1 and Dataset2. That's the final piece to have a reproducible example. – Joy Jun 07 '17 at 21:04
  • There are just data frames. Why you need code for that. – beginner Jun 07 '17 at 21:06
  • 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 Jun 07 '17 at 22:08

1 Answers1

1

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))
GGamba
  • 13,140
  • 3
  • 38
  • 47
  • This looks fine. But I need different colours for the two data frame values. And also legend showing the colors. – beginner Jun 07 '17 at 21:52
  • 2
    Each point takes its `x` from `df1` and its `y` from `df2`, so there is no such thing as 'two data frames' anymore. I assumed that's what you wanted, based on the displayed graph. Evidently it's wrong, pls explain better (edit the question) what you want. – GGamba Jun 07 '17 at 21:59
  • Ok. I got the plot I need with some changes. Nothing major one. Thank you !! – beginner Jun 08 '17 at 12:36