# NOT RUN {
# get the data
d <- rd("Employee")
# --------------------------------------------------------
# bar chart from tabulating the data for a single variable
# --------------------------------------------------------
# for each level of Dept, display the frequencies
BarChart(Dept)
# short name
# bc(Dept)
# save the values output by BarChart into the myOutput list
myOutput <- BarChart(Dept)
# display the saved output
myOutput
# just males with salaries larger than 75,000 USD
BarChart(Dept, rows=(Gender=="M" & Salary > 85000))
# rotate and offset the axis labels, sort categories by frequencies
BarChart(Dept, rotate_x=45, offset=1, sort="-")
# set bars to a single color of blue with some transparency
BarChart(Dept, fill="blue", trans=0.3)
# progressive (sequential) color scale of blues
BarChart(Dept, fill="blues")
# viridis palate
BarChart(Dept, fill="viridis")
# change the theme just for this analysis, as opposed to style()
BarChart(Dept, theme="darkgreen")
# set bar color to hcl custom hues with chroma and luminance
# at the values provided by the default hcl colors from
# the getColors function, which defaults to h=240 and h=60
# for the first two colors on the qualitative scale
bc(Gender, fill=c(hcl(h=180,c=100,l=55), hcl(h=0,c=100,l=55)))
# or set to unique colors via color names
BarChart(Gender, fill=c("palegreen3","tan"))
# darken the colors with an explicit call to getColors,
# do a lower value of luminance, set to l=25
BarChart(Dept, fill=getColors(l=25), trans=0.4)
# column proportions instead of frequencies
BarChart(Gender, stat="proportion")
# map value of tabulated count to bar fill
BarChart(Dept, fill=(count))
# viridis palette
BarChart(Dept, fill=(count.v))
# data with many values of categorical variable Make and large labels
myd <- Read("Cars93")
# perpendicular labels
bc(Make, rotate_x=90, data=myd)
# manage size of horizontal value labels
bc(Make, horiz=TRUE, label_max=4, data=myd)
# read y variable, Salary
# display bars for values of count <= 0 in a different color
# than values above
BarChart(Dept, Salary, stat="dev", sort="+", fill_split=0)
# ----------------------------------------------------
# bar chart from tabulating the data for two variables
# ----------------------------------------------------
# at each level of Dept, show the frequencies of the Gender levels
BarChart(Dept, by=Gender)
# Trellis (facet) plot
BarChart(Dept, by1=Gender)
# at each level of Dept, show the row proportions of the Gender levels
# i.e., 100% stacked bar graph
BarChart(Dept, by=Gender, stack100=TRUE)
# at each level of Gender, show the frequencies of the Dept levels
# do not display percentages directly on the bars
BarChart(Gender, by=JobSat, fill="reds", values="off")
# specify two fill colors for Gender
BarChart(Dept, by=Gender, fill=c("deepskyblue", "black"))
# display bars beside each other instead of stacked, Female and Male
# the levels of Dept are included within each respective bar
# plot horizontally, display the value for each bar at the
# top of each bar
BarChart(Gender, by=Dept, beside=TRUE, horiz=TRUE, values_position="out")
# horizontal bar chart of two variables, put legend on the top
BarChart(Gender, by=Dept, horiz=TRUE, legend_position="top")
# for more info on base R graphic options, enter: help(par)
# for lessR options, enter: style(show=TRUE)
# here fill is set in the style function instead of BarChart
# along with the others
style(fill=c("coral3","seagreen3"), lab_color="wheat4", lab_cex=1.2,
panel_fill="wheat1", main_color="wheat4")
BarChart(Dept, by=Gender,
legend_position="topleft", legend_labels=c("Girls", "Boys"),
xlab="Dept Level", main="Gender for Different Dept Levels",
value_labels=c("None", "Some", "Much", "Ouch!"))
style()
# -----------------------------------------------------------------
# multiple bar charts tabulated from data across multiple variables
# -----------------------------------------------------------------
# bar charts for all non-numeric variables in the data frame called d
# and all numeric variables with a small number of values, < n_cat
# BarChart(one_plot=FALSE)
d <- rd("Mach4", quiet=TRUE)
# all on the same plot, bar charts for 20 6-pt Likert scale items
# default scale is divergent from "browns" to "blues"
BarChart(m01:m20, horiz=TRUE, values="off", sort="+")
# }
# NOT RUN {
# custom scale with explicit call to getColors, HCL chroma at 50
clrs <- getColors("greens", "purples", c=50)
BarChart(m01:m20, horiz=TRUE, values="off", sort="+", fill=clrs)
# custom divergent scale with pre-defined color palettes
# with implicit call to getColors
BarChart(m01:m20, horiz=TRUE, values="off", fill=c("aquas", "rusts"))
# ----------------------------
# can enter many types of data
# ----------------------------
# generate and enter integer data
X1 <- sample(1:4, size=100, replace=TRUE)
X2 <- sample(1:4, size=100, replace=TRUE)
BarChart(X1)
BarChart(X1, by=X2)
# generate and enter type double data
X1 <- sample(c(1,2,3,4), size=100, replace=TRUE)
X2 <- sample(c(1,2,3,4), size=100, replace=TRUE)
BarChart(X1)
BarChart(X1, by=X2)
# generate and enter character string data
# that is, without first converting to a factor
Travel <- sample(c("Bike", "Bus", "Car", "Motorcycle"), size=25, replace=TRUE)
BarChart(Travel, horiz=TRUE)
# ----------------------------
# bar chart directly from data
# ----------------------------
# include a y-variable, here Salary, in the data table to read directly
d <- read.csv(text="
Dept, Salary
ACCT,51792.78
ADMN,71277.12
FINC,59010.68
MKTG,60257.13
SALE,68830.06", header=TRUE)
BarChart(Dept, Salary)
# specify two variables for a two variable bar chart
# also specify a y-variable to provide the counts directly
# when reading y values directly, must be a summary table,
# one row of data for each combination of levels with
# a numerical value of y
# use lessR pivot function to get summary table
d <- Read("Employee")
a <- pivot(d, mean, Salary, c(Dept,Gender))
BarChart(Dept, Salary_mean, by=Gender, data=a)
# do so just with BarChart, display bars in grayscale
# How does average salary vary by gender across the various departments?
BarChart(Dept, Salary, by=Gender, stat="mean", data=d, fill="grays")
# -----------
# annotations
# -----------
d <- rd("Employee")
# Place a message in the center of the plot
# \n indicates a new line
BarChart(Dept, add="Employees by\nDepartment", x1=3, y1=10)
# Use style to change some parameter values
style(add_trans=.8, add_fill="gold", add_color="gold4", add_lwd=0.5)
# Add a rectangle around the message centered at <3,10>
BarChart(Dept, add=c("rect", "Employees by\nDepartment"),
x1=c(2,3), y1=c(11, 10), x2=4, y2=9)
# }
Run the code above in your browser using DataLab