Directory:
- Initial pattern
- How to modify the width of bar bars
- How to modify the order of bar bars
- How do I add text to the bar
(For more information on legends, coordinate systems, etc. see: R, Ggplot2, shiny summary)
Initial pattern:
library(ggplot2)library(ggthemes)dtdata.frame(obj = c(‘A‘,‘D‘,‘B‘,‘E‘,‘C‘), val = c(2,15,6,9,7))p = ggplot(dt, aes(x = obj, y = val, fill = obj, group = factor(1))) + "identity") + theme_economist()p
How to modify the bar width:
library(ggplot2)library(ggthemes)dtdata.frame(obj = c(‘A‘,‘D‘,‘B‘,‘E‘,‘C‘), val = c(2,15,6,9,7))p = ggplot(dt, aes(x = obj, y = val, fill = obj, group = factor(1))) + "identity"0.5) + ## 修改柱条的宽度 theme_economist()p
How to modify the order of bars:
Library (GGPLOT2) library (ggthemes) dt = data.frame (obj = C (' A ',' D ',' B ',' E ',' C '), val = C (2, the,6,9,7)) DT$obj= Factor (dt$obj, Levels=c (' D ',' B ',' C ',' A ',' E '))# # Set the order of barsp = ggplot (DT, AES (x= obj,y= val, fill = obj, group = factor (1)) + Geom_bar (Stat="Identity", width =0.5) +# # Change the bar widthTheme_economist () p# # Special NOTE: Dt$obj is a factor type, the order of Ggplot2 is based on the order of this factor level,# # So we modify the order of the factor level to modify the order of the drawing, the specific situation can be output dt$obj.
How do I add text to the bar:
Library (GGPLOT2) library (ggthemes) dt = data.frame (obj = C (' A ',' D ',' B ',' E ',' C '), val = C (2, the,6,9,7)) DT$obj= Factor (dt$obj, Levels=c (' D ',' B ',' C ',' A ',' E '))# # Set the order of barsp = ggplot (DT, AES (x= obj,y= val, fill = obj, group = factor (1)) + Geom_bar (Stat="Identity", width =0.5) +# # Change the bar widthTheme_economist () + Geom_text (AES (label = val, vjust =-0.8, Hjust =0.5, color = obj), show_guide = FALSE) +# # Displays the number on the barYlim (min (dt$val,0)*.1, Max (dt$val)*.1)# # Increase the Y-axis range, prevent the digital display is not completeP
Reprint please indicate the source, thank you! (Original link: http://blog.csdn.net/bone_ace/article/details/47267981)
ggplot2--Column Chart