与ggarrange()合并时,如何同步两个直方图之间的条形宽度? [英] How can I synchronize the bar width between two histograms when merging with ggarrange()?

查看:38
本文介绍了与ggarrange()合并时,如何同步两个直方图之间的条形宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请在下面找到我的数据 w .

Please, find my data w below.

我生成了这两个直方图,图A 图B ,随后我使用 ggarrange()

I have produced these two histograms, Plot A and Plot B, which I subsequently merged using ggarrange()

如您所见,两个单独的直方图之间的条形宽度不匹配.可能是因为情节A中的 0个事件.

As you can see, the widths of the bars does not match between the two individual histograms. Its probably because of the 0 events in plot A.

如何分别使图A和图B中的条形之间的宽度同步?像这样:

How can I synchronize the width between the bars in plot A and plot B, respectively? Like this:

我使用了这些脚本

# Plot A
io <- ggplot() +

  geom_bar(mapping = aes(x = w$WHO[w$response==1]), stat = "count", alpha=0.2, colour="#1C73C2", fill="#ECF0F9") + 

  scale_y_continuous(name = "Number of progressions", breaks=yaks, sec.axis = sec_axis(~ . * 1 , name = "Progression rate per 100 person-yrs", breaks=yaks, labels=c("0","40","80","120","160","200","240","280","320","360"))) +
  scale_x_continuous(name="", breaks = c(1,2,3,4), labels =c("\nWHO-I\nn=32","\nWHO-II\nn=23","\nWHO-III\nn=16","\nUnknown\nn=5")) +
  coord_cartesian(ylim=c(0, 18)) +

  geom_point(mapping = aes(x = 1, y = 0.2677376), size=5, alpha=0.7, shape=18, colour="red") +
  geom_point(mapping = aes(x = 2, y = 3.2870709), size=5, shape=18,alpha=0.7, colour="red") +
  geom_point(mapping = aes(x = 3, y = 16.98), size=5, shape=18,alpha=0.7, colour="red") +

  annotate("text", x = c(1,2,3, 4) , y = c(0.2677376+1.3, 3.2870709+1.3, 16.98+1.3, 0.5),  label = c("5.4","65.7","339.6", "0 events"), col="red", fontface=2, cex=4)+ 

  theme(axis.text.y.right = element_text(color = "red", size = 11), 
        axis.title.y=element_text(color="darkgrey", size=11,face="bold", margin = margin(t = 0, r = 15, b = 0, l = 0)), 
        axis.text.y = element_text(color = "#1C73C2", size = 11), 
        axis.title.y.right=element_text(color="darkgrey", size=11,face="bold", margin = margin(t = 0, r = 0, b = 0, l = 15)),
        axis.text.x = element_text(color = "grey20", size = 11))

还有

# Plot B
yt <- ggplot()  +
  geom_bar(mapping = aes(x = w$WHO[w$Death==1]), stat = "count", alpha=0.2, colour="#1C73C2", fill="#ECF0F9")  +

  scale_y_continuous(name = "Number of deaths", breaks=yaks, sec.axis = sec_axis(~ . * 1 , name = "Mortality rate per 100 person-yrs", breaks=yaks, labels=c("0","5","10","15","20","25","30","35","40","45"))) +
  scale_x_continuous(name="", breaks = c(1,2,3,4), labels =c("\nWHO-I\nn=37","\nWHO-II\nn=29","\nWHO-III\nn=19","\nUnknown\nn=25")) +

  coord_cartesian(ylim=c(0, 18)) +

  geom_point(mapping = aes(x = 1, y = 3.329993), size=5,alpha=0.7, shape=18, colour="red") +
  geom_point(mapping = aes(x = 2, y = 12.424504), size=5,alpha=0.7, shape=18, colour="red") +
  geom_point(mapping = aes(x = 3, y = 17.23519), size=5, alpha=0.7,shape=18, colour="red") +
  geom_point(mapping = aes(x = 4, y = 4.549763), size=5, alpha=0.7, shape=18, colour="red") +

  annotate("text", x = c(1,2,3,4) , y = c(3.329993+1.3, 12.424504+1.3, 17.23519+1.3,4.549763+1.3 ),  label = c("8.3","31.1","43.1","11.4"), col="red", fontface=2, cex=4) +

  theme(axis.text.y.right = element_text(color = "red", size = 11), 
        axis.title.y=element_text(color="darkgrey", size=11,face="bold", margin = margin(t = 0, r = 15, b = 0, l = 0)), 
        axis.text.y = element_text(color = "#1C73C2", size = 11), 
        axis.title.y.right=element_text(color="darkgrey", size=11,face="bold", margin = margin(t = 0, r = 0, b = 0, l = 15)),
        axis.text.x = element_text(color = "grey20", size = 11))

最后

ggarrange(io, yt,labels = c("A", "B"), ncol = 1, nrow = 2, heights = 1.2) 

我的数据 w

w <- structure(list(WHO = c(1L, 3L, 2L, 2L, 2L, 3L, 2L, 3L,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1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 
1L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, NA, 1L)), class = "data.frame", row.names = c(NA, 
-111L))

推荐答案

您只需创建一个虚拟数据框即可在图A中获得第4列,这样您就可以完全透明了:

You can just create a dummy data frame to obtain a 4th column in your plot A, that you will turn fully transparent :

w2=data.frame(WHO=4,response=1,Death=NA)

,然后将图A的前两行更正为:

and then correct the two first rows of plot A as :

io <- ggplot(data = w[w$response==1,]) +

  geom_bar(mapping = aes(x = WHO), stat = "count", alpha=0.2, colour="#1C73C2", fill="#ECF0F9") + geom_bar(data=w2,mapping = aes(x = WHO),alpha=0,stat = "count") +

  scale_y_continuous(name = "Number of progressions", breaks=yaks, sec.axis = sec_axis(~ . * 1 , name = "Progression rate per 100 person-yrs", breaks=yaks, labels=c("0","40","80","120","160","200","240","280","320","360"))) +
  scale_x_continuous(name="", breaks = c(1,2,3,4), labels =c("\nWHO-I\nn=32","\nWHO-II\nn=23","\nWHO-III\nn=16","\nUnknown\nn=5")) +
  coord_cartesian(ylim=c(0, 18)) +

  geom_point(mapping = aes(x = 1, y = 0.2677376), size=5, alpha=0.7, shape=18, colour="red") +
  geom_point(mapping = aes(x = 2, y = 3.2870709), size=5, shape=18,alpha=0.7, colour="red") +
  geom_point(mapping = aes(x = 3, y = 16.98), size=5, shape=18,alpha=0.7, colour="red") +

  annotate("text", x = c(1,2,3, 4) , y = c(0.2677376+1.3, 3.2870709+1.3, 16.98+1.3, 0.5),  label = c("5.4","65.7","339.6", "0 events"), col="red", fontface=2, cex=4)+ 

  theme(axis.text.y.right = element_text(color = "red", size = 11), 
        axis.title.y=element_text(color="darkgrey", size=11,face="bold", margin = margin(t = 0, r = 15, b = 0, l = 0)), 
        axis.text.y = element_text(color = "#1C73C2", size = 11), 
        axis.title.y.right=element_text(color="darkgrey", size=11,face="bold", margin = margin(t = 0, r = 0, b = 0, l = 15)),
        axis.text.x = element_text(color = "grey20", size = 11))

最后,只需像以前一样使用 ggarrange 即可:

finally, just use ggarrange as before :

ggarrange(io, yt,labels = c("A", "B"), ncol = 1, nrow = 2, heights = 1.2) 

这篇关于与ggarrange()合并时,如何同步两个直方图之间的条形宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆