R绘制横状条图并排序

R绘制横状条图并排序及一页多图

所用数据详情

institution
# A tibble: 10 x 2
   Institution                                     Articles
   <chr>                                              <dbl>
 1 Harvard Medical School                               177
 2 University of Texas MD Anderson Cancer Center        131
 3 Mayo Clinic                                          119
 4 Massachusetts General Hospital                       118
 5 Cleveland Clinic Foundation                          110
 6 Brigham and Women's Hospital                          93
 7 Seoul National University College of Medicine         88
 8 VA Medical Center                                     88
 9 Samsung Medical Center, Sungkyunkwan University       85
10 SungKyunKwan University, School of Medicine           78

所用绘制代码

 pacman::p_load(ggplot2,readr,forcats)
  institution <- read_csv("institution.csv")
  
  ggplot(institution,aes(x=reorder(Institution,Articles), y=Articles)) +  #reorder调整排列顺序
    geom_bar(stat = "identity", fill="steelblue4", color="black", width = 0.5, position = position_dodge(0.3))+#调整条形宽度以及条形距离 )+ 
    coord_flip()+
    geom_text(aes(label=Articles), hjust=-0.2, color="black")+
    xlab("Institution") + ylab("Articles")#可以通过color、size等自行调整标签属性
    
  # colours()[grep('blue', colours())]查看调整颜色

所得图像为
在这里插入图片描述

#一页多图
pacman::p_load(ggplot2,readr,forcats,showtext,cowplot)
institution <- read_csv("institution.csv")

#图一
plot.institution <- ggplot(institution,aes(x=reorder(Institution,Articles), y=Articles)) +  #reorder调整排列顺序
  geom_bar(stat = "identity", fill="steelblue4", color="black", width = 0.5, position = position_dodge(0.3))+#调整条形宽度以及条形距离 )+ 
  coord_flip()+
  geom_text(aes(label=Articles), hjust=-0.2, color="black")+
  xlab("Institution") + ylab("Articles")#可以通过color、size等自行调整标签属性
#图二
institute <- read_csv("institute.csv")

plot.institute <-ggplot(institute,aes(x=reorder(Institute,Articles), y=Articles)) +  #reorder调整排列顺序
  geom_bar(stat = "identity", fill="steelblue4", color="black", width = 0.5, position = position_dodge(0.3))+#调整条形宽度以及条形距离 )+ 
  coord_flip()+
  geom_text(aes(label=Articles), hjust=-0.2, color="black")+
  xlab("Institute") + ylab("Articles")#可以通过color、size等自行调整标签属性
# 组合方法一
p <- cowplot::plot_grid(plot.institution, plot.institute, nrow = 1, labels = LETTERS[1:2])#将2幅图组合成一幅图,按照一行两列排列,标签分别为A、B。(LETTERS[1:4] 意为提取26个大写英文字母的前四个:A、B、C、D)
p
# 组合方法二
p2 <- ggpubr::ggarrange(plot.institution, plot.institute, nrow = 1, labels = c('A', 'B'), font.label = list(color = 'red'))

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用R语言绘制横向柱状图可以使用ggplot2包来实现。首先,确保你已经安装了ggplot2包,如果没有安装可以通过以下命令安装: ```R install.packages("ggplot2") ``` 然后,加载ggplot2包: ```R library(ggplot2) ``` 接下来,我们使用ggplot函数创建一个基本的图形,并使用geom_bar函数绘制柱状图。通过设置geom_bar函数中的参数"width"为1,可以将柱状图设置为横向显示。以下是一个示例代码: ```R # 创建数据框 data <- data.frame(Category = c("A", "B", "C", "D"), Value = c(10, 20, 15, 12)) # 使用ggplot函数创建图形,并使用geom_bar函数绘制横向柱状图 ggplot(data, aes(x = Category, y = Value)) + geom_bar(stat = "identity", width = 1, fill = "blue") + coord_flip() + xlab("Category") + ylab("Value") + ggtitle("Horizontal Bar Chart") ``` 在这个示例中,我们创建了一个包含Category和Value两列的数据框。然后,使用ggplot函数创建图形,并使用aes函数指定x和y轴的变量。接着,使用geom_bar函数绘制横向柱状图,其中参数stat设置为"identity"表示直接使用数据中的值作为柱子的高度。通过设置width参数为1,可以控制柱子的宽度。使用coord_flip函数可以将横向柱状图转为纵向显示。最后,使用xlab、ylab和ggtitle函数设置x轴标签、y轴标签和图形标题。 运行上述代码,即可得到一个横向柱状图。你可以根据自己的数据和需求进行相应的修改和调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值