Get the App
SLTechnology News&Howtos  ›  Development  › 

How to interchange x and y axes of ggplot2 Cartesian coordinate system

Shulou Source: shulou.com Published: 2022-06-01 04:56:15 10月03日 Update

This article mainly introduces the ggplot2 Cartesian coordinate system x, y axis how to exchange the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that after reading this ggplot2 Cartesian coordinate system x, y axis exchange article will have something to gain, let's take a look.

Library ('ggplot2') library (' reshape2') A = c ("A", "B", "C", "D", "E") B = c (9014) dat = data.frame (A 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= 25pint 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 camera cm') p=p+coord_flip () p

However, it can be seen that after the coordinate exchange, the original settings need to adjust the position of the text in the text layer, specifically adjust vjust and hjust. For example:

Library ('ggplot2') library (' reshape2') A = c ("A", "B", "C", "D", "E") B = c (9014) dat = data.frame (A 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 = 5Covjust = 0.5 Hjust=-0.25) + guides (fill = guide_legend (reverse = F)) + theme (plot.title = element_text (size= 25 bold = "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 on "how the x and y axes of the ggplot2 Cartesian coordinate system are interchanged" ends here. Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to exchange the x and y axes of ggplot2 Cartesian coordinate system". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Tags: Coordinates coordinate system Descartes knowledge content text article adjustment from which we can know value location easy to operate article easy to understand more thus look industry information information channel Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Shulou Tech Info Xiaomi MySQL Linux