Course of week 11th: bar chart, dot chart, pie chart, and Histogram

Source: Internet
Author: User
Tags border color rcolorbrewer

1.1 Column chart

 
Setwd ("E: // program files // rstudio // Temp") # Set the working directory install. packages ("rcolorbrewer") # If the Library (rcolorbrewer) is not installed # load the package citysales <-read.csv ("citysales.csv") # Read the data barplot (. matrix (citysales [,]), # convert to matrix beside = true, # if true, the columns are arranged side by side. TEXT = citysales $ city, # legend text args. legend = List (bty = "N", horiz = true), # legend parameter: no border; horizontal Col = Brewer. pal (5, "set1"), # Call the five colors border = "white" in the set1 canvas, # The Bar boundary is white ylim = C (0,100 ), # Y axis range ylab = "sales revenue (1,000's of USD)", # Y axis label main = "sales figures") # Main title box (bty = "L ") # specify the border of the entire coordinate image as "L"

1.2 Stacked column chart

Citysales <-read.csv ("citysales.csv") barplot (. matrix (citysales [, 2: 4]), Legend. TEXT = citysales $ city, argS. legend = List (bty = "N", horiz = true), Col = Brewer. pal (5, "set1"), border = "white", ylim = C (0,200), # note that the range should be adequate, otherwise, the graphic stack exceeds the axis range ylab = "sales revenue (1,000's of USD)", main = "sales figures ")

Here, the beside parameter is set to the default value false, which is the result of horizontal stacking.

1.3 Display percentage with stacked Effect

Citysalesperc <-read.csv ("citysalesperc.csv") PAR (MAR = C (5, 4, 4, 8), XPD = T) # Mar specifies the edge margin of the image (bottom left and top right); XPD if true, all plovers is clipped to the figure regionbarplot (. matrix (citysalesperc [, 2: 4]), Col = Brewer. pal (5, "set1"), border = "white", ylab = "sales revenue (1,000's of USD)", main = "percentage sales figures ") # In the data, the total percentage of each product in five cities is 100%, so the stack height is the same as that of legend ("right", # legend drawn on the right side of legend = citysalesperc $ city, # legend text information bty = "N", inset = C (-0.3, 0), # legend distance from graph fill = Brewer. pal (5, "set1") # fill color of the legend, consistent with that in the bar chart

1.4 Horizontal Column chart

Barplot (. matrix (citysales [,]), beside = true, horiz = true, # horizontally place legend. TEXT = citysales $ city, argS. legend = List (bty = "N"), Col = Brewer. pal (5, "set1"), border = "white", xlim = C (0,100), xlab = "sales revenue (1,000's of USD )", main = "sales figures") # except for specifying horiz = true, other parameters are the same as in other cases.

1.5 Displays the horizontal bar chart of the stacked percentage.

 
PAR (MAR = C (5, 4, 4, 8), XPD = T) barplot (. matrix (citysalesperc [, 2: 4]), horiz = true, # horizontal Col = Brewer. pal (5, "set1"), border = "white", xlab = "percentage of sales", main = "perecentage sales figures ") # horiz = true and beside = false (default) # note the particularity of this data: in the data, the total percentage of each product in five cities is 100%, therefore, the stacking width is the same as the legend ("right", legend = citysalesperc $ city, bty = "N", inset = C (-0.3, 0), fill = Brewer. pal (5, "set1 "))

1.6 Adjust the width, interval, and color of the column chart.

 
Barplot (. matrix (citysales [, 2: 4]), beside = true, Legend. TEXT = citysales $ city, argS. legend = List (bty = "N", horiz = T), Col = C ("# e5562a", "# 491a5b", "#8c6ca8", "# bd1b8a ", "#7cb6e4"), # Set the color of the column border = false, Space = C (), #0 indicates the spacing between the columns, 5 indicates the spacing between groups ylim = C (0,100), ylab = "sales revenue (1,000's of USD)", main = "sales figures ")

1.7 Effect Comparison

 
Barplot (. matrix (citysales [, 2: 4]), beside = T, Legend. TEXT = citysales $ city, argS. legend = List (bty = "N", horiz = T), ylim = C (0,100), ylab = "sales revenue (1,000's of USD )", main = "sales figures ")

No column color or spacing parameter is specified. Use gray scale to differentiate the columns. Each group has one column width.

1.8Display data at the top of the column

 
X <-barplot (. matrix (citysales [, 2: 4]), beside = true, Legend. TEXT = citysales $ city, argS. legend = List (bty = "N", horiz = true), Col = Brewer. pal (5, "set1"), border = "white", ylim = C (0,100), ylab = "sales revenue (1,000's of USD )", main = "sales figures") Y <-. matrix (citysales [, 2: 4]) text (X, Y + 2, labels =. character (y) # specify coordinates for the first two parameters, and labels specify text values

1.9Mark data next to a horizontal column

Y <-barplot (. matrix (citysales [, 2: 4]), beside = true, horiz = true, Legend. TEXT = citysales $ city, argS. legend = List (bty = "N"), Col = Brewer. pal (5, "set1"), border = "white", xlim = C (0,100), xlab = "sales revenue (1,000's of USD )", main = "sales figures") x <-. matrix (citysales [, 2: 4]) # horizontal column text (x + 2, Y, labels =. character (x) # specify coordinates for the first two parameters

1.10 Mark the column

 
Rain <-read.csv ("cityrain.csv") Y <-barplot (as. Matrix (Rain [1,-1]), # take 1st rows, 2 ~ Five columns (horiz = T, # Col = "white", yaxt = "N", # Y Axis Main = "rainfall in January ", xlab = "rainfall (MM)") # assign a bar chart to Y. Here, the Y value is the vertical coordinate X of the center of each column <-0.5 * Rain [1, -1] # abscissa text (X, Y, colnames (Rain [-1]) # The added text is the column name of each column.

1.11 Labeling Error

 
Sales <-T (. matrix (citysales [,-1]) # transpose colnames (sales) <-citysales [, 1] # convert city to column x <-barplot (sales, beside = t, legend. TEXT = rownames (sales), argS. legend = List (bty = "N", horiz = T), Col = Brewer. pal (3, "set2"), border = "white", ylim = C (0,100), ylab = "sales revenue (1,000's of USD )", main = "sales figures") # assign the column marker to X, and X is the abscissa arrows (X0 = x, Y0 = Sales * 0.95, # arrow start X1 = X, y1 = Sales * 1.05, # arrow end angle = 90, # arrow vertical code = 3, # arrow style, here are 2 arrows length = 0.04, # arrow width LWD = 0.4) # arrow Length

2Point chart

Install. packages ("reshape") # The system will prompt you to install and load the plyr package Library (reshape) Sales <-melt (citysales) # change a wide table to a narrow table: Sales $ color [sales [, 2] = "producta"] <-"red" sales $ color [sales [, 2] = "productb"] <-"blue" sales $ color [sales [, 2] = "productc"] <-"Violet" # dotchart (sales [, 3], labels = Sales $ city, groups = Sales [, 2], # by product group Col = Sales $ color, PCH = 19, # The Symbol Main = "sales figures", xlab = "sales revenue (1,000 s of USD) ") # both the dotchart function and the melt function are in the reshape package.

3.1Pie Chart

Browsers <-read. table ("browsers.txt", header = true) # Read data browsers <-browsers [Order (browsers [, 2]),] # Sort the data boxes by the second column. Pie (browsers [, 2], # data labels = browsers [, 1], # tag clockwise = true, # clockwise radius = 1, # radius Col = Brewer. pal (7, "set1"), # Call the color palette border = "white", # The border color of each block. Main = "percentage share of Internet browser usage ")

3.2Percentage on pie chart

Browsers <-read. table ("browsers.txt", header = true) browsers <-browsers [Order (browsers [, 2]),] pielabels <-sprintf ("% s = % 3.1f % s ", browsers [, 1], 100 * browsers [, 2]/sum (browsers [, 2]), "%") # generate the browser name and the corresponding percentage, this is a bit similar to the printf function in C language. Pie (browsers [, 2], labels = pielabels, # Call the previously generated Tag Information clockwise = true, radius = 1, Col = Brewer. pal (7, "set1"), border = "white", cex = 0.8, # Main = "percentage share of Internet browser usage ")

3.3Add a chartExample

Browsers <-read. table ("browsers.txt", header = true) browsers <-browsers [Order (browsers [, 2]),] pielabels <-sprintf ("% s = % 3.1f % s ", browsers [, 1], 100 * browsers [, 2]/sum (browsers [, 2]), "%") pie (browsers [, 2], labels = Na, clockwise = true, Col = Brewer. pal (7, "set1"), border = "white", radius = 0.7, cex = 0.8, main = "percentage share of Internet browser usage") legend ("bottomright ", legend = pielabels, # Call the previously generated label information bty = "N", fill = Brewer in the legend. pal (7, "set1 "))

4.1 Histogram

 
Air <-read.csv ("airpollution.csv") hist (Air $ nitrogen. oxides, xlab = "nitrogen oxide concentrations", main = "Distribution of nitrogen oxide concentrations ")

4.2Display with Probability Density

 
Hist (Air $ nitrogen. oxides, freq = false, # Turn off the frequency. The vertical axis shows the probability of xlab = "nitrogen oxide concentrations", main = "Distribution of nitrogen oxide concentrations ")

4.3Add breaks

 
Hist (Air $ nitrogen. oxides, breaks = 20, # specify the number of columns. The larger the breaks, the narrower the column. xlab = "nitrogen oxide concentrations", main = "Distribution of nitrogen oxide concentrations ")

4.4Specify the breaks range

 
Hist (Air $ nitrogen. oxides, breaks = C (0,100,200,300,400,500,600), # If the breaks value is a vector, specify the breakpoint xlab = "nitrogen oxide concentrations" for each column ", main = "Distribution of nitrogen oxide concentrations ")

4.5Beautify with color

 
Hist (Air $ respirable. particle, prob = true, # The vertical axis shows Col = "black" with probability, # The column is black border = "white", # The column boundary is white xlab = "respirable particle concentrations ", main = "Distribution of respirable particle concentrations ")

4.6Beautify with lines

 
PAR (yaxs = "I", las = 1) # yaxs sets the style of the Y axis scale range. I indicates that the style is displayed within the original data range. Las specifies the style of the Axis label, 1 indicates horizontal hist (Air $ respirable. particle, prob = true, Col = "black", border = "white", xlab = "respirable particle concentrations", main = "Distribution of respirable concentrations ") box (bty = "L") grid (Nx = Na, # No vertical line ny = NULL, # use the system default lty = 1, # Line Style LWD = 1, # line width Col = "gray") # Line color

4.7 Id Density Function

PAR (yaxs = "I", las = 1) hist (Air $ respirable. particle, prob = true, Col = "black", border = "white", xlab = "respirable particle concentrations", main = "Distribution of respirable concentrations ") box (bty = "L") lines (density (Air $ respirable. particle, Na. rm = T), # density Col = "red", LWD = 4) # Add the probability density curve grid (Nx = Na, NY = NULL, lty = 1, LWD = 1, Col = "gray ")

4.8 A set of histograms

 
Panel. hist <-function (x ,...) # customize a histogram function {Par (USR = C (PAR ("USR") [], 0, 1.5) hist (x, prob = true, add = true, Col = "black", border = "white")} plot (IRIS [, 1:4], main = "relationships between characteristics of Iris flowers", PCH = 19, col = "blue", cex = 0.9, diag. panel = panel. hist) # panel is used for diagonal graphs. hist Function

4.9 Scatter Plot + Histogram

# Set up the layout first layout (matrix (C (,), byrow = true), widths = C (), heights = C (), true) # Make scatterplot PAR (MAR = C (5.1, 4.1, 0.1, 0) plot (Air $ respirable. participant ~ Air $ nitrogen. oxides, PCH = 19, Col = "black", xlim = C (0,600), ylim = C (), xlab = "nitrogen oxides concentrations ", ylab = "respirable particle concentrations") # plot histogram of X variable in the top row PAR (MAR = C (0, 4.1,) hist (Air $ nitrogen. oxides, breaks = seq (0,600,100), ann = false, # Do not annotate (such as title, axis label) axes = false, # do not coordinate axis Col = "black ", border = "white") # plot histogram of Y variable to the right of the scatterplot yhist <-Hist (Air $ respirable. participant, breaks = seq (, 10), plot = false)  # Here, The hist object is stored in yhist. yhist $ density stores the density value PAR (MAR = C (5.1, 0, 0.1, 1) of each column )) barplot (yhist $ density, horiz = true, # Space = 0, # the spacing between the column and each group is 0 axes = false, Col = "black ", border = "white ")

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.