Directory:Initial pattern How to modify the width of a bar how to change the order of bars how to add text to a bar
(For more on the legend, coordinate system, etc. see: R, Ggplot2, shiny summary)
Initial pattern:
Library (GGPLOT2)
Library (ggthemes)
dt = data.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))) +
Geom_bar (stat = "identity") +
theme_economist ()
P
How to modify the width of a column bar:
Library (GGPLOT2)
Library (ggthemes)
dt = data.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))) +
Geom_bar (stat = "Identity", Width = 0.5) +
## Modify the width of the bar
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,15,6,9,7))
dt$ obj = Factor (dt$obj, Levels=c (' D ', ' B ', ' C ', ' A ', ' E ')) # # Set the order of the bars
p = ggplot (DT, AES (x = obj, y = val, fill = obj, gr OUP = Factor (1)) +
Geom_bar (stat = "Identity", Width = 0.5) + # # Modify the width of the bar
theme_economist ()
p
# # Special Note : Dt$obj is the factor type, ggplot2 the order of the drawing is according to the order of the factor level,
# # So we modify the order of the factor level can modify the order of the mapping, the specific situation can be exported dt$obj.
How to add text to a column bar:
Library (GGPLOT2)
Library (ggthemes)
dt = data.frame (obj = C (' A ', ' D ', ' B ', ' E ', ' C '), val = C (2,15,6,9,7))
dt$ obj = Factor (dt$obj, Levels=c (' D ', ' B ', ' C ', ' A ', ' E ')) # # Set the order of the bars
p = ggplot (DT, AES (x = obj, y = val, fill = obj, gr OUP = Factor (1))) +
Geom_bar (stat = "Identity", Width = 0.5) + # # Modify the width of the bar
theme_economist () +
Geom_text (a ES (label = val, vjust = -0.8, hjust = 0.5, color = obj), show_guide = FALSE) + # # Displays the number Ylim on the bar (
min (dt$val, 0) *1. 1, Max (dt$val) *1.1) # # Increase the Y-axis range to prevent digital display not complete
p
Reprint please indicate the source, thank you. (Original link: http://blog.csdn.net/bone_ace/article/details/47267981)