使用geom_text在geom_point上组织文本

前端之家收集整理的这篇文章主要介绍了使用geom_text在geom_point上组织文本前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个线图,其中一些时间点难以通过单独的颜色来区分,因此我想在图上标记时间点,但标签重叠(见下图),其中难以阅读标签.

情节目前看起来像这样,

我想知道是否有办法“堆叠”标签或某种方式(脚本)可以确保它们不重叠.像这样的东西,

任何帮助,将不胜感激.

这是我用来制作情节的代码,

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"),VISIT03  = seq(as.Date("2009-01-01"),VISIT04  = seq(as.Date("2009-03-30"),by="60 day")[sample(30,VISIT05  = seq(as.Date("2010-11-30"),by="6 day")[sample(30,VISIT06  = seq(as.Date("2011-01-30"),Discharge = seq(as.Date("2012-03-30"),by="30 day")[sample(30,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)

解决方法

您可以根据“访问”的数值更改标签的垂直位置.

关键是:

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

这目前产生:
根据访问(%% 3)的值,可以增加或减少3个不同的级别
每个级别由y个标签之间的距离的四分之一(0.25)分隔
第一个标签在水平线以下0.12
第二个是0.12以上

require(ggplot2)
require(plyr)
require(reshape)
# create sample data
set.seed(666)
dfn <- data.frame(
  Referral  = seq(as.Date("2007-01-15"),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,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,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,shape = 124) + 
  geom_text(data=mdfn2,y=(as.numeric(Patient)+0.25*as.numeric(Visit)%%3)-0.12,angle = 00)
原文链接:https://www.f2er.com/html/231758.html

猜你在找的HTML相关文章