千家信息网

ggplot2笛卡尔坐标系x、y轴怎么互换

发表于:2025-01-19 作者:千家信息网编辑
千家信息网最后更新 2025年01月19日,这篇文章主要介绍了ggplot2笛卡尔坐标系x、y轴怎么互换的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇ggplot2笛卡尔坐标系x、y轴怎么互换文章都会有所收获,下面
千家信息网最后更新 2025年01月19日ggplot2笛卡尔坐标系x、y轴怎么互换

这篇文章主要介绍了ggplot2笛卡尔坐标系x、y轴怎么互换的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇ggplot2笛卡尔坐标系x、y轴怎么互换文章都会有所收获,下面我们一起来看看吧。

library('ggplot2')library('reshape2')A = c("A","B","C","D","E")B = c(90,34,56,99,15)C = c(50,20,24,70,14)dat = data.frame(A,B,C)names(dat) = c("type","sample1","sample2")dat = melt(dat,variable.name="Sample",value.name = "Num")head(dat)p = ggplot(dat, aes(x = type,y = Num,fill = Sample))+    geom_bar(stat ="identity",width = 0.6,position = position_dodge(width=0.8))+          scale_fill_manual(values = c("red","blue"))+  labs(x = "",y = "", title = "test")+   geom_text(aes(label = dat$Num),position=position_dodge(width = 0.9),size = 5,vjust = -0.25)+    guides(fill = guide_legend(reverse = F))+  theme(plot.title = element_text(size = 25,face = "bold", vjust = 0.5, hjust = 0.5),        legend.title = element_blank(),        legend.text = element_text(size = 18, face = "bold"),        legend.position = 'right',        legend.key.size=unit(0.8,'cm'))p=p+coord_flip()p


不过由此可知,进行坐标互换之后,原来的设置中关于文字图层中文字位置需要进行调整,具体调整vjust和hjust即可。譬如:

library('ggplot2')library('reshape2')A = c("A","B","C","D","E")B = c(90,34,56,99,15)C = c(50,20,24,70,14)dat = data.frame(A,B,C)names(dat) = c("type","sample1","sample2")dat = melt(dat,variable.name="Sample",value.name = "Num")head(dat)p = ggplot(dat, aes(x = type,y = Num,fill = Sample))+    geom_bar(stat ="identity",width = 0.6,position = position_dodge(width=0.8))+          scale_fill_manual(values = c("red","blue"))+  labs(x = "",y = "", title = "test")+   geom_text(aes(label = dat$Num),position=position_dodge(width = 0.9),size = 5,vjust = 0.5,hjust=-0.25)+    guides(fill = guide_legend(reverse = F))+  theme(plot.title = element_text(size = 25,face = "bold", vjust = 0.5, hjust = 0.5),        legend.title = element_blank(),        legend.text = element_text(size = 18, face = "bold"),        legend.position = 'right',        legend.key.size=unit(0.8,'cm'))p=p+coord_flip()p

关于"ggplot2笛卡尔坐标系x、y轴怎么互换"这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对"ggplot2笛卡尔坐标系x、y轴怎么互换"知识都有一定的了解,大家如果还想学习更多知识,欢迎关注行业资讯频道。

0