本文介绍了使用geom_text在geom_point上组织文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个线条图,有些时间点难以单独用颜色区分,因此我想标记图上的时间点,但标签重叠(请参见下图)很难阅读标签。 剧情目前看起来像这样, 我想知道是否有办法堆叠标签或某种方式(脚本)可以确保它们不重叠。就像这样, $ b / p> 任何帮助将不胜感激。 以下是我用来生成图的代码, $ p $ require(ggplot2) require(plyr) require(reshape) #create示例数据 set.seed(666) dfn< - data.frame( Referral = seq(as.Date(2007-01-15),len = 26,by =23天), 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 (样本(30,26)], VISIT06 = seq(as.Date(2011-01-30),len = 24,by =6 day)[样本(30,26) )], Discharge = seq(as.Date(2012-03-30),len = 24,by =30 d ay)[sample(30,26)], Patient = factor(1:26,labels = LETTERS), openCase = rep(0:1,100)[sample(100,26) ]) #设置今天的数据,用于没有放电日期的情况 dfn $放电[is.na(dfn $放电)] 名称(mdfn)[4 ]< - 'Year'#重命名 #通过dfn中的'Referral'命令mdfn中的数据 mdfn $患者 (dfn $ Patient [order(dfn $ Referral)]),ordered = TRUE) #数据集的子集,以避免未关闭的情况出现'Discharge' mdfn2 #现在看起来的情节 ggplot(mdfn,aes(Year,Patient))+ geom_blank ()+ geom_line(data = mdfn [mdfn $ openCase == 0,],color =black)+ geom_line(data = mdfn [mdfn $ openCase == 1,],color = gray)+ geom_point(data = mdfn2,aes(color = Visit),size = 4,shape = 124)+ geom_text(data = mdfn2,mapping = aes(x = Year,y =患者, label = substr(Visit,1,7),color = Visit),size = 2, vjust = - 。4,hjust = - 。1,angle = 00) 解决方案您可以根据数值更改标签的垂直位置 关键是: y =(as .nu​​meric(Patient)+ 0.25 * as.numeric(Visit)%% 3)-0.12 目前产生: 根据Visit(%% 3)的值,可以增加或减少3个不同级别 每个级别由一个quar ter标签之间的距离(0.25) 第一个标签是0.12,低于水平线 第二个0.12以上 require(ggplot2) require (plyr) require(reshape)#创建示例数据 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天)[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)[样本(100,26)]) #设定今日没有出院日期的数据 dfn $出院[出院(dfn $出院)] mdfn< - melt(dfn,id = c('Patient','openCase'),variable_name =Visit)名称(mdfn)[4]< - 'Year'#重命名 #通过dfn中的'Referral'命令mdfn中的数据 mdfn $患者(dfn $ Patient [order(dfn $ Referral)]),ordered = TRUE) #数据集的子集,避免未关闭的病例出现'Discharge' mdfn2< - subset(mdfn,!(Visit ==Discharge&年份> as.Date(2014-01-01))) #现在看起来的情节 ggplot(mdfn,aes(Year,Patient))+ geom_blank ()+ geom_line(data = mdfn [mdfn $ openCase == 0,],color =black)+ geom_line(data = mdfn [mdfn $ openCase == 1,],color = gray)+ geom_point(data = mdfn2,aes(color = 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),color = Visit),size = 2, hjust = - 。1,angle = 00) I have a line plot with some time points that are hard to distinguish by the coloring alone and I would therefore like to label the time points on the plot, but the labels overlap (see plot below) in a way where it is hard to read the labels.The plot currently look like this,I wonder if there is a way to 'stack' the labels or some way (script) that can ensure they do not overlap. Something like this,Any help would be appreciated.Here is the code I used to produce the plot, require(ggplot2) require(plyr) require(reshape)# create sample dataset.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) 解决方案 You can change the vertical location of the label according to the numeric value of Visit.The key is: y=(as.numeric(Patient)+0.25*as.numeric(Visit)%%3)-0.12This currently produces:3 different levels according to values of Visit (%%3), which you can increase or decreaseeach level is separated by a quarter of the distance between y labels (0.25)the first label is 0.12 below the horizontal linethe second is 0.12 aboverequire(ggplot2)require(plyr)require(reshape)# create sample dataset.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 datedfn$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 dfnmdfn$Patient <- factor(mdfn$Patient,levels = (dfn$Patient[order(dfn$Referral)]),ordered = TRUE)# subset a dataset to avoid 'Discharge' for cases that are not closedmdfn2 <- subset(mdfn,!(Visit=="Discharge" & Year > as.Date("2014-01-01")))# the plot as it looks nowggplot(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) 这篇关于使用geom_text在geom_point上组织文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
06-05 21:50