将文本标签添加到ggplot2散点图 [英] Adding text labels to ggplot2 scatterplot

查看:1757
本文介绍了将文本标签添加到ggplot2散点图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种简单的方法将图形标签添加到图形中的圆圈上?我无法使用directlabels包进行操作,因为我收到错误消息:



Direct.label.ggplot(p,first.qp)中的错误:需要颜色审美来推断默认的直接标签。



以下是图:



以下是我一直使用的代码:

  library(ggplot2)
library(directlabels)
#my数据集:
oc < - read.csv( http://www.columbia.edu/~mad2200/oc.csv)
oc $ percent_women < - oc $ W_employment /(oc $ M_employment + oc $ W_employment)
oc $ size< ; - $ $ 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 ,color = percent_women))+ scale_size_continuous(range = c(0,30))+ #scale_area()+
#geom_point(aes(color = oc $ percent_women))+
coord_equal()+
scale_colour_gradient(高=红)+
ylim(700,1700)+
xlim(700,1700)+
geom_abline(斜率= 1)+
实验室(title =按职业和性别分列的收入差距) +
ylab(女性每周收入)+
xlab(男性每周收入$)


解决方案添加 geom_text(aes(label = Occupational.Group),size = 3)情节。尽管你需要使用尺寸。




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:

Error in direct.label.ggplot(p, "first.qp") : Need colour aesthetic to infer default direct labels."

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. You'll need to play with the size though.

这篇关于将文本标签添加到ggplot2散点图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆