移动绘图上的标签远离线。

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

Moving the labels on a plot away from the line

问题

我有一个数据框如下;

TEST1 <- data.frame(
    Number = c(46.68, 46.45, 48.35, 44.63, 45.65, 46.46, 45.14, 45.69, 47.10),
    House.Height = c("LOW", "LOW", "LOW", "MODERATE", "MODERATE", "MODERATE", "HIGH", "HIGH", "HIGH")
)

我已经使用以下代码创建了一个箱线图:

ggboxplot(TEST1, x = "House.Height", y = "Number", fill = "House.Height", palette = c("#009E73", "#D55E00", "#CC79A7"), order = c("LOW", "MODERATE", "HIGH"), sort.by.groups = TRUE,
          label = TRUE, label.pos = "out", lab.vjust = -2
) +
scale_x_discrete(labels = c("low", "moderate", "high")) +
xlab("House Height")+ ylab("Number") + 
labs(fill = "House"
) + 
scale_y_continuous(breaks = seq(44,50)) +
coord_cartesian(ylim = c(44, 50))

然而,从结果中我可以看到,在"LOW"柱状图中的一个标签(46.45)位于线上,你几乎看不到44.63和45.14中的点。

有没有办法将它们向下移动,以使它们更可见。我认为vjust会起作用,但显然它没有。我还尝试过使用不同的y轴刻度,但没有成功。任何帮助将不胜感激。

英文:

I have a dataframe like this;

TEST1 &lt;- data.frame(
    Number = c(46.68, 46.45, 48.35, 44.63, 45.65, 46.46, 45.14, 45.69, 47.10),
    House.Height= c(&quot;LOW&quot;, &quot;LOW&quot;, &quot;LOW&quot;, &quot;MODERATE&quot;, &quot;MODERATE&quot;, &quot;MODERATE&quot;, &quot;HIGH&quot;, &quot;HIGH&quot;, &quot;HIGH&quot;))

I have created a boxplot using this code:

    ggboxplot(TEST1, x = &quot;House.Height&quot;, y = &quot;Number&quot;, fill = &quot;House.Height&quot;, palette = c(&quot;#009E73&quot;, &quot;#D55E00&quot;, &quot;#CC79A7&quot;), order = c(&quot;LOW&quot;, &quot;MODERATE&quot;, &quot;HIGH&quot;), sort.by.groups = TRUE,
          label = TRUE, label.pos = &quot;out&quot;, lab.vjust = -2
) +
    scale_x_discrete(labels = c(&quot;low&quot;, &quot;moderate&quot;, &quot;high&quot;)) +
    xlab(&quot;House Height&quot;)+ ylab(&quot;Number&quot;) + 
    labs(fill = &quot;House&quot;
    ) + 
    scale_y_continuous(breaks = seq(44,50)) +
    coord_cartesian(ylim = c(44, 50)) 

However, I can see from the outcome that one of the labels (46.45) in the "LOW" bar is on the line, and you can barley see the dots in the 44.63 and 45.14.
移动绘图上的标签远离线。

Is there a way to move them downwards so that they can be more visible. I thought vjust would have it, but obviously, it did not. I have also tried using various scales for the yaxis without any success. Any help will be appreciated.

答案1

得分: 1

我认为使用ggplot中的geom_boxplot更容易实现这一目标,因为您可以简单地使用geom_text来进行调整,而且使用nudge_x和nudge_y非常直观。我还重新排列了绘图前的因子水平,并将您的调色板放在scale_fill_manual中。

TEST1$House.Height <- factor(TEST1$House.Height, levels = c('LOW', 'MODERATE', 'HIGH'))

ggplot(TEST1, aes(x = House.Height, y = Number))+
  geom_boxplot(aes(fill = House.Height)) +
  scale_x_discrete(labels = c("low", "moderate", "high")) +
  xlab("House Height")+ ylab("Number") + 
  labs(fill = "House") + 
  scale_y_continuous(breaks = seq(44,50)) +
  coord_cartesian(ylim = c(44, 50)) +
  scale_fill_manual(values = c("#009E73", "#D55E00", "#CC79A7")) +
  geom_text(aes(label = Number, x = House.Height, y = Number), nudge_x = 0.5, size = 3) +
  theme_classic() +
  theme(legend.position = 'top')

移动绘图上的标签远离线。

英文:

I think it would be easier to achieve this using geom_boxplot within ggplot because then you can simply use geom_text which is very straightforward to adjust using nudge_x and nudge_y. I also re-arranged the factor levels prior to plotting and put your palette in scale_fill_manual.

TEST1$House.Height &lt;- factor(TEST1$House.Height, levels = c(&#39;LOW&#39;, &#39;MODERATE&#39;, &#39;HIGH&#39;))

ggplot(TEST1, aes(x = House.Height, y = Number))+
  geom_boxplot(aes(fill = House.Height)) +
  scale_x_discrete(labels = c(&quot;low&quot;, &quot;moderate&quot;, &quot;high&quot;)) +
  xlab(&quot;House Height&quot;)+ ylab(&quot;Number&quot;) + 
  labs(fill = &quot;House&quot;) + 
  scale_y_continuous(breaks = seq(44,50)) +
  coord_cartesian(ylim = c(44, 50)) +
  scale_fill_manual(values = c(&quot;#009E73&quot;, &quot;#D55E00&quot;, &quot;#CC79A7&quot;)) +
  geom_text(aes(label = Number, x = House.Height, y = Number), nudge_x = 0.5, size = 3) +
  theme_classic() +
  theme(legend.position = &#39;top&#39;)

移动绘图上的标签远离线。

答案2

得分: 1

您可以使用repel=TRUE选项吗?

或者您可以使用ggplotgeom_label_repel来获得更好的效果:

英文:

You could use the repel=TRUE option?

移动绘图上的标签远离线。

Or you could use ggplot with geom_label_repel to get a nicer outcome:

ggplot(TEST1 , aes(x=House.Height, y=Number, fill=House.Height)) + geom_boxplot() + theme_bw() + 
  scale_fill_manual(values = c(&quot;#009E73&quot;, &quot;#D55E00&quot;, &quot;#CC79A7&quot;)) + 
  theme_bw()  +
  theme(legend.position = &quot;top&quot;) + 
  scale_x_discrete(limits=c(&quot;LOW&quot;, &quot;MODERATE&quot;, &quot;HIGH&quot;)) + 
  geom_point() + 
  ggrepel::geom_label_repel(aes(label=Number), force=1.5, fill=hsv(0,0,1,alpha=0.5))

移动绘图上的标签远离线。

huangapple
  • 本文由 发表于 2023年2月8日 19:51:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385405.html
匿名

发表评论

匿名网友

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

确定