英文:
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 <- 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"))
I have created a boxplot using this code:
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))
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 <- 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')
答案2
得分: 1
您可以使用repel=TRUE
选项吗?
或者您可以使用ggplot
与geom_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("#009E73", "#D55E00", "#CC79A7")) +
theme_bw() +
theme(legend.position = "top") +
scale_x_discrete(limits=c("LOW", "MODERATE", "HIGH")) +
geom_point() +
ggrepel::geom_label_repel(aes(label=Number), force=1.5, fill=hsv(0,0,1,alpha=0.5))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论