R图中的锚文本注释
Anchor text annotation in R plot
编辑* 解决方案附在这个问题的底部
我有一个带有注释的图表,可以将客户更改为客户。我需要文本以最大值右对齐,这样任何客户端名称长度都不会覆盖图形的其他区域。这是代码和产品。只希望文本始终在箭头旁边结束,并向左延伸。
x <- c(1,1,1,1,1,1,2,2,2,2,2,2)
y <- c(0,0,0,0,0,0,0,0.33,0.17,0.16,0.14,0.2)
z<-data.frame(cbind(x,y))
client.name = "x"
client.year = 2015
ggplot<-
ggplot(z,aes(x = x,y = y,fill = y))+
geom_bar(stat = "identity",
fill = c("white","white","white","white","white","white",
"white","#c00000","#ed7d31","#ffc000","#92d050","#00b050"),width = .3)+
theme(legend.position = "none") +
theme(axis.ticks = element_blank()) +
theme(panel.grid = element_blank()) +
theme(axis.title = element_blank()) +
theme(axis.text = element_blank()) +
# theme(panel.background = element_blank()) +
theme(plot.margin = unit(c(0, 0, 0, 0), "cm"))+
geom_segment(aes(x = 1.77, y = .9, xend = 2.16, yend = .9),size=1.3,
arrow = arrow(angle = 20, length = unit(0.25, "inches"), ends = "first", type = "open"),linetype="solid")+
annotate("text", x = 2, y = c(0.167,0.415,0.58,0.73,0.9),
label = c("Disparaging","Unhappy","Ambivalent","Happy","Delighted"),
colour="white", fontface="bold", size=10) +
annotate("text", x = 1, y =.9, label = paste(client.name,client.year,paste0(.9, "%")),
fontface = "bold", hjust=0, size=10)
ggplot
这是客户端名称较长的结果,手动调整为位于箭头旁边
这里是客户端名称尽可能小的结果,"x"。
我可以通过使用每个名称的值将文本放到我想要的位置,但需要自动调整到任何给定名称的箭头最右边(奖励积分,我什至可以有一个很长的名字换行!?不知道怎么办)。
有什么想法吗?
解决方案:
正如@baptiste 所指出的, hjust 是这里的关键选项。我知道是这种情况,但不知道 0:1 之外的 h 值会根据字符串长度更改锚点的位置。你可以看到我使用 -1 来获得正确的定位,而我本应该操纵 X。我通过将 hjust 设置为 1(右对齐)并将 X 调整到所需位置来解决我的问题。
感谢观看。
有趣的问题,这只是部分解决方案。加分题,试试这个方法
annotate("text", x = 1, y =.9, label = paste("verylong\nclientname", client.year,paste0(.9, "%")), fontface = "bold", hjust=0, size=10)
插入换行符的方式和位置 \n
算法更复杂。您可能会说 10 个字符或更多是一个长客户端名称。然后,也许您可以对每个客户端名称 运行 nchar()
并按照以下伪代码设置测试:ifelse(nchar > 10,在位置 11 插入换行符,不理会)
编辑关于客户姓名、日期和百分比的位置
现在你让我思考!
你为什么不定义 anchor <- .9
# 使用箭头上的当前终点?
然后您将您的客户名称等放在 x = anchor - .1
或向左水平调整?在你当前的 MWE 数据中,x 主要是 1 和 2,它可能不太有效,但对于真实数据,我认为这可能会使注释紧贴箭头的末端。
如果你想要右对齐的文本,你应该使用 hjust=1
lab = strwrap(paste(client.name,client.year,paste0(.9, "%")),10)
annotate("text", x = 1.77, y =.9, label = paste(lab, collapse="\n"),
fontface = "bold", hjust=1, size=10)
编辑* 解决方案附在这个问题的底部
我有一个带有注释的图表,可以将客户更改为客户。我需要文本以最大值右对齐,这样任何客户端名称长度都不会覆盖图形的其他区域。这是代码和产品。只希望文本始终在箭头旁边结束,并向左延伸。
x <- c(1,1,1,1,1,1,2,2,2,2,2,2)
y <- c(0,0,0,0,0,0,0,0.33,0.17,0.16,0.14,0.2)
z<-data.frame(cbind(x,y))
client.name = "x"
client.year = 2015
ggplot<-
ggplot(z,aes(x = x,y = y,fill = y))+
geom_bar(stat = "identity",
fill = c("white","white","white","white","white","white",
"white","#c00000","#ed7d31","#ffc000","#92d050","#00b050"),width = .3)+
theme(legend.position = "none") +
theme(axis.ticks = element_blank()) +
theme(panel.grid = element_blank()) +
theme(axis.title = element_blank()) +
theme(axis.text = element_blank()) +
# theme(panel.background = element_blank()) +
theme(plot.margin = unit(c(0, 0, 0, 0), "cm"))+
geom_segment(aes(x = 1.77, y = .9, xend = 2.16, yend = .9),size=1.3,
arrow = arrow(angle = 20, length = unit(0.25, "inches"), ends = "first", type = "open"),linetype="solid")+
annotate("text", x = 2, y = c(0.167,0.415,0.58,0.73,0.9),
label = c("Disparaging","Unhappy","Ambivalent","Happy","Delighted"),
colour="white", fontface="bold", size=10) +
annotate("text", x = 1, y =.9, label = paste(client.name,client.year,paste0(.9, "%")),
fontface = "bold", hjust=0, size=10)
ggplot
这是客户端名称较长的结果,手动调整为位于箭头旁边
这里是客户端名称尽可能小的结果,"x"。
我可以通过使用每个名称的值将文本放到我想要的位置,但需要自动调整到任何给定名称的箭头最右边(奖励积分,我什至可以有一个很长的名字换行!?不知道怎么办)。
有什么想法吗?
解决方案: 正如@baptiste 所指出的, hjust 是这里的关键选项。我知道是这种情况,但不知道 0:1 之外的 h 值会根据字符串长度更改锚点的位置。你可以看到我使用 -1 来获得正确的定位,而我本应该操纵 X。我通过将 hjust 设置为 1(右对齐)并将 X 调整到所需位置来解决我的问题。
感谢观看。
有趣的问题,这只是部分解决方案。加分题,试试这个方法
annotate("text", x = 1, y =.9, label = paste("verylong\nclientname", client.year,paste0(.9, "%")), fontface = "bold", hjust=0, size=10)
插入换行符的方式和位置 \n
算法更复杂。您可能会说 10 个字符或更多是一个长客户端名称。然后,也许您可以对每个客户端名称 运行 nchar()
并按照以下伪代码设置测试:ifelse(nchar > 10,在位置 11 插入换行符,不理会)
编辑关于客户姓名、日期和百分比的位置 现在你让我思考!
你为什么不定义 anchor <- .9
# 使用箭头上的当前终点?
然后您将您的客户名称等放在 x = anchor - .1
或向左水平调整?在你当前的 MWE 数据中,x 主要是 1 和 2,它可能不太有效,但对于真实数据,我认为这可能会使注释紧贴箭头的末端。
如果你想要右对齐的文本,你应该使用 hjust=1
lab = strwrap(paste(client.name,client.year,paste0(.9, "%")),10)
annotate("text", x = 1.77, y =.9, label = paste(lab, collapse="\n"),
fontface = "bold", hjust=1, size=10)