I am trying to add labels to my scatter plot based on another columns variables.
ggplot(differentrace, aes(x=DifferentRace, y=Parents)) + geom_point() + geom_text(label=rownames(differentrace))
My table is
Parents Different Race
1 1 UK born 2.32
2 Both UK born 2.58
3 Neither 2.24
This is my output, instead of the 1,2,3 labels I would like the labels to be 1 UK born, Both UK born, or Neither. Also I would like to offset the labels slightly to not obstruct the data points.
Thanks
R doesn't consider your Parents
column as row names. Instead, it's just another column! So here's the code: ggplot(differentrace, aes(x=DifferentRace, y=Parents)) + geom_point() + ggrepel::geom_text_repel(aes(label = Parents))
. I used ggrepel::geom_text_repel
to move the text labels away from the points.
Additionally, you have to wrap label = Parents
in an aes()
call because you want each point to be labelled with the corresponding Parent
from the same dataframe; if you don't use aes()
, R doesn't know what Parent
refers to.