2012-05-07 72 views
5

我有一些線條圖,有些時間點難以單獨用顏色區分,因此我想標記圖上的時間點,但標籤重疊(請參見下圖)以難以閱讀標籤的方式。使用geom_text整理geom_point上的文本

該地塊目前這個樣子,

current plot

我不知道是否有辦法「堆」的標籤或某種方式(腳本),可以確保它們不重疊。事情是這樣的,

 - - >

任何幫助,將不勝感激。

這裏是我用來製造陰謀的代碼,

require(ggplot2) 
require(plyr) 
require(reshape) 

# create sample data 
set.seed(666) 
dfn <- data.frame(
Referral = seq(as.Date("2007-01-15"), len= 26, by="23 day"), 
VISIT01 = seq(as.Date("2008-06-15"), len= 24, by="15 day")[sample(30, 26)], 
VISIT02 = seq(as.Date("2008-12-15"), len= 24, by="15 day")[sample(30, 26)], 
VISIT03 = seq(as.Date("2009-01-01"), len= 24, by="15 day")[sample(30, 26)], 
VISIT04 = seq(as.Date("2009-03-30"), len= 24, by="60 day")[sample(30, 26)], 
VISIT05 = seq(as.Date("2010-11-30"), len= 24, by="6 day")[sample(30, 26)], 
VISIT06 = seq(as.Date("2011-01-30"), len= 24, by="6 day")[sample(30, 26)], 
Discharge = seq(as.Date("2012-03-30"), len= 24, by="30 day")[sample(30, 26)], 
Patient = factor(1:26, labels = LETTERS), 
openCase = rep(0:1, 100)[sample(100, 26)]) 

# set today's data for cases that do not have an Discharge date 
dfn$Discharge[ is.na(dfn$Discharge) ] <- as.Date("2014-01-30") 

mdfn <- melt(dfn, id=c('Patient', 'openCase'), variable_name = "Visit") 
names(mdfn)[4] <- 'Year' # rename 

# order data in mdfn by 'Referral' in dfn 
mdfn$Patient <- factor(mdfn$Patient,levels = 
(dfn$Patient[order(dfn$Referral)]),ordered = TRUE) 

# subset a dataset to avoid 'Discharge' for cases that are not closed 
mdfn2 <- subset(mdfn,!(Visit=="Discharge" & Year > as.Date("2014-01-01"))) 

# the plot as it looks now 
ggplot(mdfn, aes(Year, Patient)) + 
    geom_blank() + 
    geom_line(data = mdfn[mdfn$openCase == 0,], colour = "black") + 
    geom_line(data = mdfn[mdfn$openCase == 1,], colour = "grey") + 
    geom_point(data = mdfn2, aes(colour = Visit), size = 4, shape = 124) + 
    geom_text(data=mdfn2, mapping=aes(x=Year, y=Patient, 
    label=substr(Visit, 1, 7), colour=Visit), size=2, 
    vjust=-.4, hjust=-.1, angle = 00) 
+1

缺少的功能,丟失的數據,不可重複的。 –

+0

我不知道該怎麼做,但是你真的需要標籤嗎?已經有一個提供這些信息的傳說。 –

+1

[智能點標籤在R中的位置]的可能重複(http://stackoverflow.com/questions/7611169/intelligent-point-label-placement-in-r) – joran

回答

11

您可以根據訪問的數值更改標籤的垂直位置。

的關鍵是:

y=(as.numeric(Patient)+0.25*as.numeric(Visit)%%3)-0.12 

這目前生產:根據(%% 3)訪問的值,這可以增加或減少
每個水平通過四分之一分離
3個不同層次Y標誌(0.25)
第一個標籤是0.12水平線
第二下面之間的距離爲0.12以上

enter image description here enter image description here

require(ggplot2) 
require(plyr) 
require(reshape) 
# create sample data 
set.seed(666) 
dfn <- data.frame(
    Referral = seq(as.Date("2007-01-15"), len= 26, by="23 day"), 
    VISIT01 = seq(as.Date("2008-06-15"), len= 24, by="15 day")[sample(30, 26)], 
    VISIT02 = seq(as.Date("2008-12-15"), len= 24, by="15 day")[sample(30, 26)], 
    VISIT03 = seq(as.Date("2009-01-01"), len= 24, by="15 day")[sample(30, 26)], 
    VISIT04 = seq(as.Date("2009-03-30"), len= 24, by="60 day")[sample(30, 26)], 
    VISIT05 = seq(as.Date("2010-11-30"), len= 24, by="6 day")[sample(30, 26)], 
    VISIT06 = seq(as.Date("2011-01-30"), len= 24, by="6 day")[sample(30, 26)], 
    Discharge = seq(as.Date("2012-03-30"), len= 24, by="30 day")[sample(30, 26)], 
    Patient = factor(1:26, labels = LETTERS), 
    openCase = rep(0:1, 100)[sample(100, 26)]) 

# set today's data for cases that do not have an Discharge date 
dfn$Discharge[ is.na(dfn$Discharge) ] <- as.Date("2014-01-30") 

mdfn <- melt(dfn, id=c('Patient', 'openCase'), variable_name = "Visit") 
names(mdfn)[4] <- 'Year' # rename 

# order data in mdfn by 'Referral' in dfn 
mdfn$Patient <- factor(mdfn$Patient,levels = 
    (dfn$Patient[order(dfn$Referral)]),ordered = TRUE) 

# subset a dataset to avoid 'Discharge' for cases that are not closed 
mdfn2 <- subset(mdfn,!(Visit=="Discharge" & Year > as.Date("2014-01-01"))) 

# the plot as it looks now 
ggplot(mdfn, aes(Year, Patient)) + 
    geom_blank() + 
    geom_line(data = mdfn[mdfn$openCase == 0,], colour = "black") + 
    geom_line(data = mdfn[mdfn$openCase == 1,], colour = "grey") + 
    geom_point(data = mdfn2, aes(colour = Visit), size = 4, shape = 124) + 
    geom_text(data=mdfn2, mapping=aes(x=Year, y=(as.numeric(Patient)+0.25*as.numeric(Visit)%%3)-0.12, 
            label=substr(Visit, 1, 7), colour=Visit), size=2, 
      hjust=-.1, angle = 00) 
+0

優雅,令人印象深刻。現在我只需要弄清楚三個時間點彼此靠近時要做什麼,並避免將標籤放在線上。謝謝。 –

+0

文本不再在線。如果您需要更多不同的高度級別,請增加%%後面的數字並減小移位的大小(當前爲0.25),這可能需要較小的文本。 –

+0

如果這不是實際數據,並且這不適用於實際數據,請考慮在匿名後提供實際數據:http://stackoverflow.com/a/10458688/742447 –