r - 组合 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 美学不是很熟悉。任何建议将不胜感激。谢谢!
解决方案
推荐阅读
- java - org.apache.jasper.JasperException:在第 [23] 行处理 JSP 页面 [/ch3/dataBean/Edit.jsp] 时发生异常
- django-2.0 - 当 AppDirs 在 Django 中设置为 True 时,我可以在哪里放置模板加载器 - Django-admin-tools
- jquery - 窗口大小调整导致使用 requestAnimationFrame 的滚动动画上的元素未对齐
- cpu - 有没有办法在所有进程中跟踪寄存器?
- angular - 从 Django Rest 框架将经过身份验证的用户传递给 SendBird
- javascript - ReactJS 使 prop 成为状态
- sql - 无法转换为类型别名
- c# - 用户表单上的动态按钮
- java - 如何告诉 Maven 将所有依赖项重新打包到单个 jar 文件中
- typescript - 打字稿返回空对象类型语法