r - 按两组比较两个数值变量

标签 r ggplot2

如何通过两组在 x 轴上使用两个数值变量作为平均值和标准差来构建 ggplot2 图表?举例:左手为2组术前Hb值,右手为2组术后Hb值。

enter image description here

最佳答案

这与您要寻找的内容相似吗?

library(tidyverse)
library(reshape2)

# creating some nonsense sample data
x <- iris %>%
  select(-Petal.Width,-Sepal.Width) %>%
  melt(id.vars="Species") %>%
  group_by_at(colnames(select_if(.,negate(is.numeric)))) %>%
  summarise_all(mean,na.rm=T) %>%
  mutate(sd01=value-value/10,
         sd02=value+value/10) %>%
  rename(mean=value,
         groups=variable)

# plotting
ggplot(x,aes(Species,mean,color=Species))+
  geom_point(stat="identity")+
  geom_errorbar(aes(ymin=sd01,ymax=sd02))+
  facet_wrap(~groups, strip.position = "bottom")+
  theme(axis.ticks = element_blank(),
        strip.background = element_blank(),
        strip.text.x = element_text(),
        panel.spacing = unit(0, "lines"))+
  scale_y_continuous(expand=c(0,0))+
  annotate("segment", x = 0, y = 0, xend = 4, yend = 0)

enter image description here

编辑:使用geom_pointrange()使代码更短并改变错误栏的外观。

ggplot(x,aes(Species,mean,color=Species))+
  geom_pointrange(aes(ymin=sd01,ymax=sd02),stat="identity")+
  facet_wrap(~groups, strip.position = "bottom")+...

enter image description here

关于r - 按两组比较两个数值变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57816470/

相关文章:

r - 通过累积分布的中值添加垂直线(ggplot2)

r - 在 R 中绘制组距离

r - ggplot : y-axis (breaks) values from stacked proportional bar graph?

sqlSave 错误 : table not found

sql - SSMS 2016 中带有 R 脚本的存储过程错误

r - 在 ggplot2 直方图中设置中断

r - 按类别按日期计算累计产品

r - 我可以通过引用 get() R 全局环境变量吗?

R - ggplot2 中几何图形的奇怪顺序

r - 如何在 ggplot 中绘制线性回归的残差线?