首页 > 解决方案 > 组合 barplot 和 ggplot 并调整其大小

问题描述

我有两个想要合并的地块。我的数据如下所示:

Year<-rep(2001:2005, each = 5)
name<-c("John","Ellen","Mark","Randy","Luisa")
Name<-c(rep(name,5))
Value<-sample(seq(0,25,by=1),25)
mydata<-data.frame(Year,Name,Value)

这是第一个条形图:

tot<-aggregate(mydata$Value,list(mydata$Year),FUN=sum)
tot_y<-tot$x
tot_x<-tot$Group.1

tot_barplot <- ggplot(tot, aes(x=tot_x,y=tot_y)) + 
  geom_bar(stat = "identity",fill="#73D055FF") +
  scale_y_continuous(limits = c(0, 125), breaks = seq(0, 125, by = 25)) +
  #xlab("Pathways") + 
  #ylab("N° of species") +
  theme(axis.line = element_blank(),
        axis.text.x=element_blank(), 
        axis.text.y=element_text(size=14,margin=margin(l=10),colour="black"),
        axis.ticks = element_blank(),
        axis.title=element_blank(),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        panel.background = element_blank()) 
tot_barplot

这是第二个情节:

p <- ggplot(mydata, aes(x=Year, y=Name, size = Value)) +
  geom_point(aes(fill = Value, 
                 alpha = I(as.numeric(Value > 0))),  shape=21, colour = "black") +
  scale_fill_viridis_c(option = "D", direction = -1,limits = c(1, 25), breaks=seq(1, 25, 5))+
  scale_size_area(guide = "none") +
  ylab("Name") + 
  theme(axis.line = element_blank(),
        axis.text.x=element_text(size=11,margin=margin(b=10),colour="black"),
        axis.text.y=element_text(size=13,margin=margin(l=10),colour="black",
                                 face="italic"),
        axis.ticks = element_blank(),
        axis.title=element_text(size=18,face="bold"),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        legend.text = element_text(size=14),
        legend.title = element_text(size=18))
p

我像这样组合它们:

grid.arrange(arrangeGrob(tot_barplot,p,nrow=2))

现在我想重新调整条形图的大小以使其更好地适合第二个图(想象原始数据产生更宽的条形图,其中条形图从图例上方开始Name并在图例上方结束Value)。我希望 barplot 的条正好位于点线和 的上方Year,但我对 ggplot 美学不是很熟悉。任何建议将不胜感激。谢谢!

标签: rggplot2

解决方案


推荐阅读