数据图中数据的不一致对齐

huangapple go评论54阅读模式
英文:

Inconsistent alignment of data in plot

问题

I have a df like this

data <- data.frame(CAR.HEIGHT = c("LOW","MODERATE","WHOLE"),
OR = c(1.077,1.044,1.050),
up_ci = c(1.099,1.054,1.059),
LOW_ci = c(1.055,1.034,1.041),
N = c(41607,188529,230136))

I have created a forest plot using ggplot2. code included below.

library (ggplot2)
library(reshape2)
library(forcats)

pos.x <- 1.4

gg<- data %>%
mutate(CAR.HEIGHT = fct_relevel(CAR.HEIGHT,"WHOLE", "MODERATE", "LOW")) %>%

ggplot(aes(x= OR, y= CAR.HEIGHT,)) +

geom_point(size=6,shape=20) +

geom_vline(xintercept = 1, colour = 'red')+

geom_errorbar(aes(xmin= LOW_ci,xmax= up_ci),width=0.30) +

coord_cartesian(xlim = c(0.80,1.25), expand = FALSE,
clip = 'off') +

geom_text(color='Black',aes(label = 'OR' , x = pos.x-0.27, y = 3.9),
hjust = 1.3,size=3.3) +

geom_text(position = position_dodge(width = 0.3),color='Black',
aes(label = paste(round(OR,3)), x =pos.x-0.285,
y = CAR.HEIGHT),
hjust = 0,size=3.3) +

geom_text(color='Black',aes(label = '95% CI' , x = pos.x-0.25, y = 3.9),
hjust = 0,size=3.3) +

geom_text(position = position_dodge(width = 0.6),color='Black',
aes(label = paste("(",round(LOW_ci,2),', ',round(up_ci,2),")",sep=''),
x =pos.x-0.25, y = CAR.HEIGHT),
hjust = 0,size=3.3) +

geom_text(color='Black',aes(label = 'N' , x = pos.x-0.201, y = 3.9),
hjust = 0,size=3.3) +

geom_text(position = position_dodge(width = 0.6),color='Black',
aes(label = paste(round(N,3)), x =pos.x-0.234,
y = CAR.HEIGHT),
hjust = -1.21,size=3.3) +

xlab("TEST CAR")+ ylab("CAR HEIGHT")

However, for some reason, the one of the data in column N (41607) does not align in a straight line with the others.

数据图中数据的不一致对齐
I have changed the hjust severally, but the issue is still there. Any idea on how to navigate round this will be appreciated.

英文:

I have a df like this

data &lt;- data.frame(CAR.HEIGHT = c(&quot;LOW&quot;,&quot;MODERATE&quot;,&quot;WHOLE&quot;),
                     OR = c(1.077,1.044,1.050),
                     up_ci = c(1.099,1.054,1.059),
                     LOW_ci = c(1.055,1.034,1.041),
                     N = c(41607,188529,230136))

I have created a forest plot using ggplot2. code included below.

library (ggplot2)
library(reshape2)
library(forcats)

pos.x &lt;- 1.4

gg&lt;- data %&gt;%
    mutate(CAR.HEIGHT = fct_relevel(CAR.HEIGHT,&quot;WHOLE&quot;, &quot;MODERATE&quot;, &quot;LOW&quot;)) %&gt;%

    ggplot(aes(x= OR, y= CAR.HEIGHT,)) +
    
    geom_point(size=6,shape=20) +
    
    geom_vline(xintercept = 1, colour = &#39;red&#39;)+
    
    geom_errorbar(aes(xmin= LOW_ci,xmax= up_ci),width=0.30) +
    
    coord_cartesian(xlim = c(0.80,1.25), expand = FALSE,
                    clip = &#39;off&#39;) +
    
    geom_text(color=&#39;Black&#39;,aes(label = &#39;OR&#39; , x = pos.x-0.27, y = 3.9),
              hjust = 1.3,size=3.3) +
    
    geom_text(position = position_dodge(width = 0.3),color=&#39;Black&#39;,
              aes(label = paste(round(OR,3)), x =pos.x-0.285,
                  y = CAR.HEIGHT),
              hjust = 0,size=3.3) +
    
    geom_text(color=&#39;Black&#39;,aes(label = &#39;95% CI&#39; , x = pos.x-0.25, y = 3.9),
              hjust = 0,size=3.3) +
    
    geom_text(position = position_dodge(width = 0.6),color=&#39;Black&#39;,
              aes(label = paste(&quot;(&quot;,round(LOW_ci,2),&#39;, &#39;,round(up_ci,2),&quot;)&quot;,sep=&#39;&#39;),
                  x =pos.x-0.25, y = CAR.HEIGHT),
              hjust = 0,size=3.3) +
    
    geom_text(color=&#39;Black&#39;,aes(label = &#39;N&#39; , x = pos.x-0.201, y = 3.9),
              hjust = 0,size=3.3) +
    
    geom_text(position = position_dodge(width = 0.6),color=&#39;Black&#39;,
              aes(label = paste(round(N,3)), x =pos.x-0.234,
                  y = CAR.HEIGHT),
              hjust = -1.21,size=3.3) +
    
    xlab(&quot;TEST CAR&quot;)+ ylab(&quot;CAR HEIGHT&quot;)

However, for some reason, the one of the data in column N (41607) does not align in a straight line with the others.

数据图中数据的不一致对齐
I have changed the hjust severally, but the issue is still there. Any idea on how to navigate round this will be appreciated.

答案1

得分: 1

不要使用 hjust 来调整标签的位置。首先确定要将文本放置在哪个点上,使用 x 和 y 坐标来决定,然后使用 hjust 来相对于该点放置文本:使用 hjust = 0 进行左对齐,hjust = 0.5 进行居中对齐,hjust = 1 进行右对齐。

尽管您可以将 hjust 设置为其他值来移动字符串,但文本移动的量始终与字符串的长度相关,因此如果您有不同大小的字符串,即使它们的 hjust 值设置为相同的值,它们也会移动不同的距离。通常情况下,如果您正在使用 hjustvjust 来微调字符串的位置,那么您可能在不正确地使用它们。

只需使用您的 x 美学(或 position_nudge)来指定文本的位置和对齐方式。

请注意,如果要为表头添加文本注释,使用 annotate 比使用 geom_text 更容易且需要更少的代码。请注意,以下代码比原始代码要短得多:

data %&gt;%
  mutate(CAR.HEIGHT = fct_relevel(CAR.HEIGHT, "WHOLE", "MODERATE", "LOW")) %&gt;%
  ggplot(aes(OR, CAR.HEIGHT)) +
  geom_point(size = 6, shape = 20) +
  geom_vline(xintercept = 1, colour = 'red') +
  geom_errorbar(aes(xmin = LOW_ci, xmax = up_ci), width = 0.30) +
  annotate('text', pos.x - c(0.27, 0.22, 0.17), 3.9,
           label = c('OR', '95% CI', 'N'), size = 3.3, fontface = 2) +
  geom_text(aes(label = paste(round(OR,3)), x = pos.x - 0.27),
            hjust = 0.5, size = 3.3) +
  geom_text(aes(label = paste0("(", round(LOW_ci,2), ',', round(up_ci,2),")"),
                x = pos.x - 0.22),
            hjust = 0.5, size = 3.3) +
  geom_text(aes(label = paste(round(N,3)), x = pos.x - 0.17),
            size=3.3, hjust = 0.5) +
  labs(x = "TEST CAR", y = "CAR HEIGHT") +
  coord_cartesian(xlim = c(0.80,1.25), expand = FALSE, clip = 'off')
英文:

You should avoid using hjust to tweak a label's position. Decide on a point where you want the text to be using x and y co-ordinates, then decide how to place the text relative to that point using hjust: use hjust = 0 for left justified, hjust = 0.5 for centered and hjust = 1 for right-justified.

Although you can set hjust to other values to move the string about, the amount that the text moves is always related to the string's length, so if you have strings of different sizes, they will move different amounts even if their hjust is set to the same value. In general, if you are using hjust or vjust to tweak string position, you are probably using them incorrectly.

Simply use your x aesthetic (or position_nudge) to specfiy where you want the text and how you want it aligned.

Note that if you are going to add text annotations for the table headers, it is easier and requires less code if you do this using annotate instead of geom_text; note that the following code is much shorter than the original:

data %&gt;%
  mutate(CAR.HEIGHT = fct_relevel(CAR.HEIGHT,&quot;WHOLE&quot;, &quot;MODERATE&quot;, &quot;LOW&quot;)) %&gt;%
  ggplot(aes(OR, CAR.HEIGHT)) +
  geom_point(size = 6, shape = 20) +
  geom_vline(xintercept = 1, colour = &#39;red&#39;) +
  geom_errorbar(aes(xmin = LOW_ci, xmax = up_ci), width = 0.30) +
  annotate(&#39;text&#39;, pos.x - c(0.27, 0.22, 0.17), 3.9,
           label = c(&#39;OR&#39;, &#39;95% CI&#39;, &#39;N&#39;), size = 3.3, fontface = 2) +
  geom_text(aes(label = paste(round(OR,3)), x = pos.x - 0.27),
            hjust = 0.5, size = 3.3) +
  geom_text(aes(label = paste0(&quot;(&quot;, round(LOW_ci,2), &#39;,&#39; , round(up_ci,2),&quot;)&quot;),
                x = pos.x - 0.22),
            hjust = 0.5, size = 3.3) +
  geom_text(aes(label = paste(round(N,3)), x = pos.x - 0.17),
            size=3.3, hjust = 0.5) +
  labs(x = &quot;TEST CAR&quot;, y = &quot;CAR HEIGHT&quot;) +
  coord_cartesian(xlim = c(0.80,1.25), expand = FALSE, clip = &#39;off&#39;)

数据图中数据的不一致对齐

huangapple
  • 本文由 发表于 2023年3月9日 19:57:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75684286.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定