简体   繁体   English

在ggplot中水平对齐多字轴文本标签

[英]align horizontally multi-word axis text label in ggplot

I have the following error_bar plot and I would like to align horizontally Latin America with Asia and Costa Rica with India in the axis-text.我有以下 error_bar 图,我想在轴文本中将拉丁美洲与亚洲和哥斯达黎加与印度水平对齐。

edit 1:编辑1:

I'm looking for an axis text in a tabular form: that is a Region column and country column.我正在寻找表格形式的轴文本:即区域列和国家/地区列。 The reason is that I have much more information to represent.原因是我有更多的信息要表达。

误差条图

tg <- ToothGrowth
setDT(tg)
aa <- tg[,.(mean=mean(len),ci95=1.96*sd(len)),supp]
aa[,country:=c("India","Costa Rica")]
aa[,region:= c("Asia","Latin America")]


ggplot(aa, aes(y=paste(region,country), x=mean)) + 
    geom_errorbar(aes(xmin=mean-ci95, xmax=mean+ci95)) +
     geom_point()

Try this approach:试试这个方法:

library(data.table)
library(datasets)
library(ggplot2)
#Code
tg <- ToothGrowth
setDT(tg)
aa <- tg[,.(mean=mean(len),ci95=1.96*sd(len)),supp]
aa[,country:=c("India","Costa Rica")]
aa[,region:= c("Asia","Latin America")]

#Plot
ggplot(aa, aes(y=paste(region,country), x=mean)) + 
  geom_errorbar(aes(xmin=mean-ci95, xmax=mean+ci95)) +
  geom_point()+
  theme(axis.text.y = element_text(angle=90,hjust=0.5))

Output:输出:

在此处输入图片说明

Update:更新:

#Code 2
ggplot(aa, aes(y=paste(region,'\n',country), x=mean)) + 
  geom_errorbar(aes(xmin=mean-ci95, xmax=mean+ci95)) +
  geom_point()+
  theme(axis.text.y = element_text(angle=90,hjust=0.5))

Output:输出:

在此处输入图片说明

Update 2: A tabular form can be reached with ggtext :更新 2:可以使用ggtext表格形式:

library(data.table)
library(datasets)
library(ggplot2)
library(ggtext)
library(glue)
library(dplyr)
#Code
tg <- ToothGrowth
setDT(tg)
aa <- tg[,.(mean=mean(len),ci95=1.96*sd(len)),supp]
aa[,country:=c("India","Costa Rica")]
aa[,region:= c("Asia","Latin America")]

#Format data
aa %>% 
  mutate(color = c("#009E73", "#009E73"),
         name = glue("<i style='color:{color}'>{region}</i> ({country})")) -> aa

#Code 2
ggplot(aa, aes(y=name, x=mean)) + 
  geom_errorbar(aes(xmin=mean-ci95, xmax=mean+ci95)) +
  geom_point()+
  theme(axis.text.y = element_markdown())

Output:输出:

在此处输入图片说明

You can difference by column using colors.您可以使用颜色按列进行区分。

Update 2: You can use facets to reach something close to what you want:更新 2:您可以使用方面来达到接近您想要的目标:

#Code 3
ggplot(aa, aes(y=region, x=mean)) + 
  geom_errorbar(aes(xmin=mean-ci95, xmax=mean+ci95)) +
  geom_point()+
  facet_wrap(.~country,scales = 'free_y',strip.position='left',ncol = 1)+
  theme(strip.text.y.left = element_text(angle = 0))

Output:输出:

在此处输入图片说明

And formating facets you can get this:和格式化方面你可以得到这个:

#Code 4
ggplot(aa, aes(y=region, x=mean)) + 
  geom_errorbar(aes(xmin=mean-ci95, xmax=mean+ci95)) +
  geom_point()+
  facet_wrap(.~country,scales = 'free_y',strip.position='left',ncol = 1)+
  theme(strip.text.y.left = element_text(angle = 0),
        strip.background.y = element_blank())

Output:输出:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM