ggplot2--Column Chart

Source: Internet
Author: User
Tags ggplot

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

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.