0

I am drawing two scatter plots with labels in the same graph. I know how to do one scatter plot with label. But I am struggling to overlay another scatter plots with labels but the each dot is the pair of the first scatter plot. In this case, can you help me how to do? I did as below, but this does not work....

library("ggplot2")
library("ggrepel")

df <- read.table("a.txt", , row.names = 1, header=T)
    AF_2    Beta_2  AF  Beta
1   0.30    -0.35   0.01    -0.35
2   0.50    0.05    0.23    0.24
3   0.60    0.05    0.46    0.02
4   0.20    0.39    0.24    0.24
5   0.01    -0.28   0.54    -0.45
6   0.20    -0.01   0.24    -0.04

ggplot(df, aes(x = AF, y = Beta), label = rownames(df)) + 
  xlab("Allele frequency") + ylab("Beta") +
  ylim(-0.5, 1) +
  geom_point() + 
  geom_label_repel(aes(label = rownames(df))) + 
  geom_point(aes(x = AF_2, y = Beta, color = "red"))

Thank you in advance!

*** I edited my codes above, thanks to the suggestions by @r2evans

user10345633
  • 105
  • 4
  • 1
    Please [make this question reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by including a small representative dataset in a plain text format - for example the output from `dput(df)`, if that is not too large. – neilfws Feb 08 '22 at 05:06
  • 1
    Your last call to `geom_point` is wrong, it's assigning `df` to the `mapping=` and the `aes(.)` to the `data=` argument. Swap those. I have a habit of always being explicit with `data=`, using `geom_*(data=df, aes(...))` so it stands out. (You should not need to redefine `data=df` here, though, since it's the same data being used.) – r2evans Feb 08 '22 at 05:13
  • 1
    Also, `xlab=` and `ylab=` don't go inside the call to `ggplot`. You can use `labs(x=, y=)`, or there are other ways to set those axis labels. – r2evans Feb 08 '22 at 05:14
  • 1
    Assigning a `label=` aesthetic to `geom_point` does not make sense. Do you mean to use `geom_text` or such, or should you delete `label=` there? Also, while this may work, the use of row names in tidyverse is mixed, many packages/functions intentionally remove them; the recommendation: if you need to keep them, then assign them to a column with `tibble::rownames_to_column`. – r2evans Feb 08 '22 at 05:17
  • 1
    Last point ... you say "overlay" and "each dot is the pair", suggesting that points from `Beta, AF` should be tied/associated somehow with `Beta_2, AF_2`, but it's not clear what you mean. If you can find a napkin-sketch or somehow describe what you think that should look like, it would help. – r2evans Feb 08 '22 at 05:20
  • Thank you so much for your comments. I want to draw two plots using the same labels without labeling 1,2,3,4,5,6 twice in the same graph. – user10345633 Feb 08 '22 at 05:35
  • 1
    If you make the changes I recommended in my first few comments, then it plots 6 black dots with labels and 6 red dots without labels. Do you mean you want the labels that point to the black dots to also have a connecting line to the paired red dots? (BTW, unless I've done more work like editing the question substantially or posting an answer, then here on SO I do not get notification unless you @ping me in the comment. If you mean to reach out to me, please ping me.) – r2evans Feb 08 '22 at 05:42
  • 1
    @r2evans - Yes, I want the labels that point to the black dots to also have a connecting line to the paired red dots. – user10345633 Feb 08 '22 at 05:51
  • 1
    `geom_label_repel` does not do that, nor does anything natively within `ggplot2`. I think one method would be to draw `geom_segment`s between each pair, so you'd have six lines between the paired red and black dots; and then you can repel labels to the centroids of each of those segments. – r2evans Feb 08 '22 at 05:52
  • 1
    Thank you so much for your suggestions. I will do it using geom_segments. – user10345633 Feb 08 '22 at 05:55

0 Answers0