您好,登錄后才能下訂單哦!
這篇文章主要介紹了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軸怎么互換”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。