简体   繁体   English

将文本标签添加到ggplot2 scatterplot

[英]Adding text labels to ggplot2 scatterplot

Is there a good way easy way to add text labels to the circles on the graph? 有没有一种简单的方法可以在图表上的圆圈中添加文字标签? I haven't able to do it using the directlabels package because I get the error: 我无法使用directlabels包,因为我收到错误:

Error in direct.label.ggplot(p, "first.qp") : Need colour aesthetic to infer default direct labels." direct.label.ggplot(p,“first.qp”)出错:需要颜色审美来推断默认的直接标签。“

Here is the graph: 这是图表: 我现在拥有什么

And here is the code that I've been using: 这是我一直在使用的代码:

library(ggplot2)
library(directlabels)
#my data set:
oc <- read.csv("http://www.columbia.edu/~mad2200/oc.csv")
oc$percent_women <- oc$W_employment/(oc$M_employment+oc$W_employment)
oc$size <- oc$W_employment+oc$M_employment
p <- ggplot(oc, aes(M_w_earnings, W_w_earnings, label = as.character(Occupational.Group)))
p + geom_point(aes(size = size, colour=percent_women)) + scale_size_continuous(range=c(0,30)) + #scale_area()+
#geom_point(aes(colour = oc$percent_women)) + 
coord_equal() +
scale_colour_gradient(high = "red")+
ylim(700, 1700) +
xlim(700, 1700) +
geom_abline(slope=1) +
labs(title = "Income Disparity by Occupation and Gender") +
ylab("Women's Weekly Earnings in $") +
xlab("Men's Weekly Earnings in $")

Add geom_text(aes(label=Occupational.Group), size=3) to the plot. geom_text(aes(label=Occupational.Group), size=3)到绘图中。 You'll need to play with the size though. 你需要玩这个尺寸。

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM