如何解决如何将y轴标签放置在2行ggplot上
我正在尝试在2条线上绘制第二个图的y轴。在第一个绘图中使用'\ n'效果很好,但在第二个绘图中使用它会使文本出现在奇怪的位置(可能是因为斜体)。
p1 <- ggplot(data = new_data) +
geom_line(mapping = aes(x = Date,y = Proportion,group = Species,colour = Species)) +
scale_colour_manual(values=c(Golden_Trevally="goldenrod2",Red_Snapper="firebrick2",Sharksucker_Remora="darkolivegreen3",Juvenile_Remora="aquamarine2")) +
xlab("Date (2014-2018)") +
ylab("Total Presence \n Per Month ") +
theme(axis.text.x = element_text(angle = 90,vjust = 0.5,hjust = 1)) +
theme(legend.position="top") +
labs(colour = "Hitchhiker Species")
new_data_counts <- new_data %>% select(Date,Count)
new_data_counts <- new_data_counts[!duplicated(new_data_counts),]
p2 <- ggplot(data = new_data_counts) +
geom_bar(mapping = aes(x = Date,y = Count),stat = 'identity') +
xlab("Date (2014-2018)") +
ylab("Total Number of "~italic(\nM.alfredi)~" Encounters") +
theme(axis.text.x = element_text(angle = 90,hjust = 1))
grid.arrange(p1,p2)
解决方法
您可以尝试以下方法:
geom_line(mapping = aes(x = Date,y = Proportion,group = Species,colour = Species)) +
scale_colour_manual(values=c(Golden_Trevally="goldenrod2",Red_Snapper="firebrick2",Sharksucker_Remora="darkolivegreen3",Juvenile_Remora="aquamarine2")) +
xlab("Date (2014-2018)") +
ylab("Total Presence \n Per Month ") +
theme(axis.text.x = element_text(angle = 90,vjust = 0.5,hjust = 1)) +
theme(legend.position="top") +
labs(colour = "Hitchhiker Species")
new_data_counts <- new_data %>% select(Date,Count)
new_data_counts <- new_data_counts[!duplicated(new_data_counts),]
p2 <- ggplot(data = new_data_counts) +
geom_bar(mapping = aes(x = Date,y = Count),stat = 'identity') +
labs(x="Date (2014-2018)",y=expression(atop(paste("Total Number of"),paste(italic("M.alfredi")," Encounters")))) +
theme(axis.text.x = element_text(angle = 90,hjust = 1))
grid.arrange(p1,p2)
,
在\n
中M.alfredi
和p2
之间需要空格。由于没有可复制的示例,这是我对第二个情节的建议,
p2 <- ggplot(data = new_data_counts) +
geom_bar(mapping = aes(x = Date,stat = 'identity') +
xlab("Date (2014-2018)") +
ylab("Total Number of "~italic(\n M.alfredi)~" Encounters") +
theme(axis.text.x = element_text(angle = 90,hjust = 1))
它应该可以解决您的问题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。