ggplot2--Column Chart __r

Source: Internet
Author: User
Tags ggplot
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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.