欢迎光临
我们一直在努力

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)
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。