2012-10-23 80 views
19

有沒有一種簡單的方法可以將文本標籤添加到圖上的圓圈?我無法使用直接標籤包,因爲我得到的錯誤:將文本標籤添加到ggplot2 scatterplot

direct.label.ggplot(p,「first.qp」)中的錯誤:需要顏色美學來推斷默認直接標籤。「

這裏是圖: What I have at the moment

這裏是我一直在使用的代碼:

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 $") 
+0

什麼問題。還可以查看'ggplot2'中的'geom_text()'以獲得文本註釋。 – Maiasaura

+0

@Maiasaura,我想他想詮釋Occipational.Group(給出p的定義)(我問過要標註什麼文字) – mnel

回答

28

添加geom_text(aes(label=Occupational.Group), size=3)的情節你需要雖與尺寸播放。

enter image description here

+0

非常感謝!這很好。我還在鬼混地添加geom_dl到情節中,並且我打開了標籤,但發現改變字體大小非常困難。這是一個更好的解決方案。 –