如何将自定义长注释geom_text放置在情节区域中,以制作甜甜圈情节? [英] How to fit custom long annotations geom_text inside plot area for a Donuts plot?

查看:154
本文介绍了如何将自定义长注释geom_text放置在情节区域中,以制作甜甜圈情节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的真实用例的精简版,我创建了一个甜甜圈情节,并希望将这些长标签贴在整个情节中,但在甜甜圈外面:



$ p $ library(ggplot2)
df < - data.frame(group = c(Cars,Trucks,Motorbikes),n = c(25,25,50),
label2 = c(Cars are blah blah blah,Trucks some of the best in town,Motorbikes are great if you ...))

df $ ymax = cumsum(df $ n)
df $ ymin = cumsum(df $ n)-df $ n
df $ ypos = df $ ymin + df $ n / 2
df $ hjust = c(0,0,1)
df
#> df
#group n label2 ymax ymin ypos hjust
#1汽车25汽车是等等等等25 0 12.5 0
#2卡车25卡车是城里最好的一些50 25 37.5 0
#3摩托车50如果你... 100 50 75.0 1

< - ggplot(df,aes(x =,y = n,fill = group) )+ geom_rect(aes_string(ymax =ymax,ymin =ymin,xmax =2.5,xmin =2.0))+
theme(axis.title = element_blank(),axis.text =元素_layout(),panel.background = element_rect(fill =white,color =grey50),
panel.grid = element_blank(),axis.ticks.length = unit(0,cm), axis.ticks.margin = unit(0,cm),
legend.position =none,panel.spacing = unit(0,lines),plot.margin = unit(c(0, 0,0,0),lines),complete = TRUE)+ geom_text(ggplot2 :: aes_string(label =label2,x =2.6,y =ypos,hjust =hjust))

pie <-bp + coord_polar(y,start = 0)+ scale_x_discrete(labels = df $ label2)
pie
pre>

但没有我所做的事情是我得到的:





我尝试降低 xmax =2.5,试图减小Donuts图的大小并相应地更新自定义文本的x,例如+0.1 x =2.6。然而,这导致整个剧情重新回到同样的情况。



我玩过各种不同的地块大小和边距,但找不到方向。如果我理解正确的话,你希望相对于整个绘图区缩小圆环,这样就可以有更多的空间了。

适合长注释?我根据样本数据得到以下结果。您可能希望调整实际用例的参数:

  library(dplyr); library(stringr)

ggplot(df%>%
mutate(label2 = str_wrap(label2,width = 10)),#change width来调整注释的宽度
aes (x =,y = n,fill = group))+
geom_rect(aes_string(ymax =ymax,ymin =ymin,xmax =2.5,xmin =2.0))+
expand_limits(x = c(2,4))+#在此处更改x轴范围限制
#不更改为主题
主题(axis.title = element_blank(),axis.text = element_blank(),
panel.background = element_rect(fill =white,color =grey50),
panel.grid = element_blank(),
axis.ticks.length =单位(0,cm),axis.ticks.margin =单位(0,cm),
legend.position =none,panel.spacing =单位(0,lines),
plot.margin = unit(c(0,0,0,0),lines),complete = TRUE)+
geom_text(aes_string(label =label2,x =3, y =ypos,hjust =hjust))+
coord_polar(y,start = 0)+
scale_x_discrete()


I have the following stripped-down version of my real use-case where I create a donuts plot and would like to have those long labels fitting inside the whole plot but outside the donuts:

library(ggplot2)
df <- data.frame(group = c("Cars", "Trucks", "Motorbikes"),n = c(25, 25, 50),
                 label2=c("Cars are blah blah blah", "Trucks some of the best in town", "Motorbikes are great if you ..."))

df$ymax = cumsum(df$n)
df$ymin = cumsum(df$n)-df$n
df$ypos = df$ymin+df$n/2
df$hjust = c(0,0,1)
df
#> df
#       group  n                          label2 ymax ymin ypos hjust
#1       Cars 25         Cars are blah blah blah   25    0 12.5     0
#2     Trucks 25 Trucks some of the best in town   50   25 37.5     0
#3 Motorbikes 50 Motorbikes are great if you ...  100   50 75.0     1

bp <- ggplot(df, aes(x="", y=n, fill=group)) + geom_rect(aes_string(ymax="ymax", ymin="ymin", xmax="2.5", xmin="2.0")) +
  theme(axis.title=element_blank(),axis.text=element_blank(),panel.background = element_rect(fill = "white", colour = "grey50"),
  panel.grid=element_blank(),axis.ticks.length=unit(0,"cm"),axis.ticks.margin=unit(0,"cm"),
  legend.position="none",panel.spacing=unit(0,"lines"),plot.margin=unit(c(0,0,0,0),"lines"),complete=TRUE) + geom_text(ggplot2::aes_string(label="label2",x="2.6",y="ypos",hjust="hjust"))

pie <- bp + coord_polar("y", start=0) + scale_x_discrete(labels=df$label2)
pie

but no matter what I do this is what I get:

I have tried lowering the xmax="2.5" in an attempt to decrease the size of the Donuts plot and updating the x of the custom text accordingly e.g. +0.1 x="2.6". However, this leads somehow to the whole plot re-scaling back to the same situation.

I have played with various plot sizes and margins but can't find the way ...

解决方案

If I understand correctly, you wish to shrink the donut relative to the entire plot area, so that there's more space to fit long annotations? I got the following to work based on the sample data. You may wish to tweak the parameters for your actual use case:

library(dplyr); library(stringr)

ggplot(df %>% 
          mutate(label2 = str_wrap(label2, width = 10)), #change width to adjust width of annotations
       aes(x="", y=n, fill=group)) + 
  geom_rect(aes_string(ymax="ymax", ymin="ymin", xmax="2.5", xmin="2.0")) +
  expand_limits(x = c(2, 4)) + #change x-axis range limits here
  # no change to theme
  theme(axis.title=element_blank(),axis.text=element_blank(),
        panel.background = element_rect(fill = "white", colour = "grey50"),
        panel.grid=element_blank(),
        axis.ticks.length=unit(0,"cm"),axis.ticks.margin=unit(0,"cm"),
        legend.position="none",panel.spacing=unit(0,"lines"),
        plot.margin=unit(c(0,0,0,0),"lines"),complete=TRUE) +
  geom_text(aes_string(label="label2",x="3",y="ypos",hjust="hjust")) +
  coord_polar("y", start=0) +
  scale_x_discrete()

这篇关于如何将自定义长注释geom_text放置在情节区域中,以制作甜甜圈情节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆